SpringCloud - 断路器监控Hystrix Dashboard使用详解2(配合Turbine对集群做监控)
作者:hangge | 2020-07-23 08:10
上一篇文章演示了如何使用 Hystrix Dashboard 进行单个实例的监控。但实际使用中,如果只能看到单个应用内的服务信息, 显然还是不够的。本文将引入 Turbine,通过它来汇集多个服务的监控信息,并将聚合后的信息提供给 Hystrix Dashboard 来集中展示和监控。
(3)接着在主类上添加 @EnableDiscoveryClient(激活 Eureka 中的 DiscoveryClient 实现) 和 @EnableTurbine 注解(开启 Turbine):
(4)编辑项目的 application.properites 文件,加入 Eureka 和 Turbine 的相关配置:
二、配合 Turbine 对集群做监控
1,系统架构
我们在上一节的基础上做一些扩展,通过引入 Turbine 来聚合 ribbon-consume 服务的监控信息(这次我们启动两个实例),并输出给 Hystrix Dashboard 来进行展示。2,搭建 Turbine 项目
(1)首先创建一个标准的 Spring Boot 工程,编辑 pom.xml 文件,添加相关依赖:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
(3)接着在主类上添加 @EnableDiscoveryClient(激活 Eureka 中的 DiscoveryClient 实现) 和 @EnableTurbine 注解(开启 Turbine):
@SpringBootApplication @EnableDiscoveryClient @EnableTurbine public class TurbineApplication { public static void main(String[] args) { SpringApplication.run(TurbineApplication.class, args); } }
(4)编辑项目的 application.properites 文件,加入 Eureka 和 Turbine 的相关配置:
(1)turbine.cluster-name-expression="default" 表示指定集群名称为 default:
- 当服务数量非常多的时候,可以启动多个 Turbine 服务来构建不同的聚合集群,而该参数可以用来区分这些不同的聚合集群。
- 同时该参数值可以在 Hystrix 仪表盘中用来定位不同的聚合集群,只需在 Hystrix Stream 的 URL 中通过 cluster 参数来指定。
- 默认情况下会以 host 来区分不同的服务,这会使得在本地调试的时候,本机上的不同服务聚合成一个服务来统计。
#为服务命名
spring.application.name=turbine
#指定服务注册中心的地址
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
#设置服务端口
server.port=8989
#指定需要收集监控信息的服务名
turbine.app-config=RIBBON-CONSUMER
#指定集群名称
turbine.cluster-name-expression="default"
#让同一主机上的服务通过主机名与端口号的组合进行区分
turbine.combine-host-port=true
3,运行测试
(1)我们分别启动 eureka-server、HELLO-SERVICE、RIBBON-CONSUMER、Turbine 以及 Hystrix Dashboard。并在 Hystrix Dashboard 中开启对 http://localhost:8989/turbine.stream 的监控,可以看到如下页面:
注意:虽然我们启动了两个 RIBBON-CONSUMER,但监控页面中依然只是展示了一个监控图,这是由于这两个实例是同一服务,而对于集群来说我们关注的是服务集群的高可用性,所以 Turbine 会将相同服务作为整体来看待,并汇总成一个监控图。(不过 Hosts 属性这里显示的是 2,说明它其实是有两个实例)
全部评论(0)