首先,你要确保每一个hystrix客户端全部的设置如这一篇中所描述的跑通:微服务设计指导-hystrix的监控。且每一个hystrix的配置都是如这篇所讲的那几步保留不动。
然后开始我们使用一个组件叫turbine的东西,来集成各个集群内的hystrix端。
turbine收集hystrix的原理如下:
其于以上原理,我们一般会在一个系统里制作一个单独的Turbine的Spring Boot Application。
maven-pom.xml文件
4.0.0
org.sky.demo.timeout
TimeoutServiceDemo
0.0.1-SNAPSHOT
TurbineDemo
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
commons-lang
commons-lang
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.cloud
spring-cloud-starter-hystrix
org.springframework.cloud
spring-cloud-starter-hystrix-dashboard
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-netflix-turbine
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.boot
spring-boot-starter-log4j2
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
org.slf4j
slf4j-log4j12
com.squareup.okhttp3
okhttp
org.springframework.boot
spring-boot-starter-test
test
org.ow2.asm
asm
org.aspectj
aspectjweaver
com.google.guava
guava
com.alibaba
fastjson
可以看到,它和标准的hystrix端所要依赖差不多,它也要actuator,只是它多了:
- spring-cloud-starter-netflix-turbine这一项,由于turbine默认用的是eurka作服务自动注册与发现的,因此我们在此例中使用的是nacos,应必须把eureka给exclude掉。
application.yml文件
server:
port: 9992
spring:
application:
name: TurbineDemo
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
feign:
hystrix:
enabled: true
hystrix:
dashboard:
proxy-stream-allow-list: "localhost" #hystrix dashboard用,如果不加这条hystrix启动后也会出现hystrix.dashboard.proxyStreamAllowList
turbine:
app-config: AdvancedIndex,AdvancedIndex2
aggregator:
cluster-config: default
cluster-name-expression: new String('default')
combine-host-port: true
management: #hystrix dashboard用,actuator暴露端口
endpoints:
web:
exposure:
include: "hystrix.stream,turbine.stream"
cors:
allowed-origins: "*"
allowed-methods: "*"
可以看到,大体上差不多,但是有几项,需要高度注意,这是网上的那些错误的例子所不具备的知识:
- turbine.app-config,这后面是一个以逗号分隔的需要被并且可以在服务注册中心里的hystrix端的spring.aplication.name;
- turbine.aggregator.cluster-config,你可以保持为default,该功能是用来聚合多个“服务群”的即指定聚合哪些集群,多个使用","分割,默认为default;
- turbine.aggregator.cluster-name-expression后的值特别的有意思,如果是在eureka里还真不会碰到问题如果你打一个“default“,但是在nacos里,你必须写成new String('default'),否则直接会报错,这是让turbine保持在服务自动注册发现的“domain namespace”去搜索那些hystrix用的;
- combine-host-port开成true,可以让同一主机上的服务通过主机名与端口号的组合来进行区分,默认情况下会以host来区分不同的服务,这会使得在本机调试的时候,本机上的不同服务聚合成一个服务来统计;
制作一个spring boot的启动类把turbine启动起来
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
@EnableTurbine
@SpringBootApplication
@EnableHystrixDashboard
public class TurbineDemo {
public static void main(String[] args) {
SpringApplication.run(TurbineDemo.class);
}
}
这边一定不能忽略两样东西(网上在这块地方100%竟然都是漏了)
- @EnableTurbine
- @EnableHystrixDashboard
特别是@EnableHystrixDashboard,如果不指定,你也就不可能在Turbine访问时打开Turbine的内置hystrix dashboard了。
使用Turbine
把Turbine应用,启动起来。
然后通过浏览器访问:http://turbine应用ip:port/hystrix,可以得到以下界面
然后在以下截图的方框内填入:http://turbine应用的ip:端口/turbine.stream,点[Monitor Stream]按钮,然后记得你要访问那些个被监控的hystrix端过一会(nacos里服务自动发现+turbine.stream收集延时)后,你会看到如下效果。
在如下截图内,我们有两个hystrix端,都被turbine收集到了,如果是不同服务比如说3个服务,每个服务分在3个spring boot实例中,你是可以得到3*2(一个熔断被开启一个熔断没被触发)共6个仪表盘的。



