Spring Cloud 应用篇 之 Hystrix Turbine(断路器聚合监控)的基本搭建
在讲解了 Hystrix Dashboard 之后,我们知道了,Hystrix Dashboard 实现的是单节点模式,一次只能监控一个服务,当有很多服务的时候,这样单节点模式去监控,就需要打开多个浏览器窗口,显然这是很麻烦的。这个时候,我们就可以用到 Spring Cloud 的另一个组件 Turbine,它可以聚合多个服务的 Hystrix Dashboard 的数据用以显示。
(一)简介
Turbine 可以聚合多个服务的 Hystrix Dashboard 的数据在一个浏览器窗口中显示,关于 Turbine 的搭建也是非常简单。
(二)搭建环境
1. 创建一个 module(spring-cloud-hystrix-dashboard-turbine)
创建步骤可以参考之前的文章(这个环境也可以在 spring-cloud-hystrix-dashboard 的基础进行改造,为了防止代码产生混乱,就新建一个 module)
2. Hystrix Dashboard 是个独立的服务,可以不注册到 eureka server,但是 Turbine 是聚合多个服务,通过服务实例 id 聚合,所以它要注册到 eureka server 中,获取服务列表,用以聚合监控。pom 文件导入以下依赖:
-
<dependency>
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-actuator</artifactId>
-
</dependency>
3. 启动类,添加注解 @EnableTurbine
-
@SpringBootApplication
-
@EnableHystrixDashboard
-
@EnableTurbine
-
public class HystrixDashboardTurbineApplication {
-
public static void main(String[] args) {
-
SpringApplication.run(HystrixDashboardTurbineApplication.class, args);
-
}
-
}
4. 配置文件:
-
server:
-
port: 8581
-
spring:
-
application:
-
name: spring-cloud-hystrix-dashboard-turbine
-
turbine:
-
# 配置 Eureka 中的 serviceId 列表,指定要监控的服务
-
app-config: SPRING-DEMO-SERVICE-FEIGN,SPRING-DEMO-SERVICE-RIBBON
-
aggregator:
-
cluster-config: default
-
# 指定集群名称
-
cluster-name-expression: "'default'"
-
eureka:
-
client:
-
service-url:
-
defaultZone: http://localhost:8761/eureka/
-
management:
-
endpoints:
-
web:
-
exposure:
-
include: '*'
-
endpoint:
-
health:
-
show-details: ALWAYS
5. 依次启动 eureka server、spring-demo-service、spring-demo-service-feign、spring-demo-service-ribbon、spring-demo-service-ribbon、spring-cloud-hystrix-dashboard-turbine,访问 http://localhost:8581/hystrix,界面和 Hystrix Dashboard 的界面一致
输入 http://localhost:8581/turbine.stream(在此之前为了不出现 Loading... 状态,提前访问被监控的服务接口,调用一次即可),点击 Monitor Stream
可以看到 spring-demo-service-ribbon 和 spring-demo-service-feign 两个服务的 Hystrix Dashboard 的数据已经被聚合到这一个界面中。