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 文件导入以下依赖:

 
  1. <dependency>

  2. <groupId>org.springframework.cloud</groupId>

  3. <artifactId>spring-cloud-starter-netflix-turbine</artifactId>

  4. </dependency>

  5. <dependency>

  6. <groupId>org.springframework.cloud</groupId>

  7. <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>

  8. </dependency>

  9. <dependency>

  10. <groupId>org.springframework.boot</groupId>

  11. <artifactId>spring-boot-starter-actuator</artifactId>

  12. </dependency>

3. 启动类,添加注解 @EnableTurbine

 
  1. @SpringBootApplication

  2. @EnableHystrixDashboard

  3. @EnableTurbine

  4. public class HystrixDashboardTurbineApplication {

  5.  
  6. public static void main(String[] args) {

  7.  
  8. SpringApplication.run(HystrixDashboardTurbineApplication.class, args);

  9. }

  10. }

4. 配置文件:

 
  1. server:

  2. port: 8581

  3.  
  4. spring:

  5. application:

  6. name: spring-cloud-hystrix-dashboard-turbine

  7.  
  8. turbine:

  9. # 配置 Eureka 中的 serviceId 列表,指定要监控的服务

  10. app-config: SPRING-DEMO-SERVICE-FEIGN,SPRING-DEMO-SERVICE-RIBBON

  11. aggregator:

  12. cluster-config: default

  13. # 指定集群名称

  14. cluster-name-expression: "'default'"

  15.  
  16. eureka:

  17. client:

  18. service-url:

  19. defaultZone: http://localhost:8761/eureka/

  20.  
  21.  
  22. management:

  23. endpoints:

  24. web:

  25. exposure:

  26. include: '*'

  27. endpoint:

  28. health:

  29. 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 的界面一致

Spring Cloud 应用篇 之 Hystrix Turbine(断路器聚合监控)的基本搭建

输入 http://localhost:8581/turbine.stream(在此之前为了不出现 Loading... 状态,提前访问被监控的服务接口,调用一次即可),点击 Monitor Stream

Spring Cloud 应用篇 之 Hystrix Turbine(断路器聚合监控)的基本搭建

可以看到 spring-demo-service-ribbon 和 spring-demo-service-feign 两个服务的 Hystrix Dashboard 的数据已经被聚合到这一个界面中。