正文
1. 概述
为了更好的监控Hystrix的性能,Spring Cloud提供Hystrix dashboard和Turbin来达到这个目的。
Hystrix dashboard可以实时监控Hystrix的运行情况。但是Hystrix dashboard只能对单台进行监控。但是在实际系统中,通常不止一个服务,为了方便监控,我们需要将多个Hystrix dashboard的数据汇总在一个dashboard中展示出来, 这个工具就是Turbine。
本文演示Hystrix dashboard和Turbine的用法
2. Hystrix dashboard
Hystrix dashboard可以实时监控Hystrix的运行情况。但是Hystrix dashboard只能对单台进行监控。
2.1. 相关工程
相关的工程说明
-
cloud-registration-center:注册中心
-
cloud-service-hystrix: 作为服务方的工程
-
cloud-consumer-hystrix:通过Hystrix调用cloud-service-hystrix的接口
本节使用的工程和
Spring cloud系列十一 @Feign集成的Hystrix进行个性化配置及集成原理
相同
其中cloud-registration-center和cloud-service-hystrix完全相同,请参考上一篇文章
2.2. 配置Hystrix日志监控
以下的配置都在工程cloud-consumer-hystrix中。
为了在服务中添加Hystrix dashboard的支持,我们对cloud-consumer-hystrix进行改造(这个工程中在
Spring cloud系列十一 @Feign集成的Hystrix进行个性化配置及集成原理
中已经讲过,本节略),这里只列出变更的内容:
在pom.xml中增加Hystrix dashboard的依赖jar
<!-- hystrix dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId>
</dependency>
<!-- 如果提示 Unable to connect to Command Metric Stream. 则需要引入以下包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在启动类上增加@EnableCircuitBreaker注解,必须需要加上这个注解
@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient // 配置本应用将使用服务注册和服务发现
@EnableCircuitBreaker // 启动断路器,如果要监控hystrix的流必须开启此注解,即使fegin已经通过属性
public class HystrixFeignCloudConsumerApplication {
….
}
测试
启动服务
-
启动工程cloud-registration-center:
-
启动工程cloud-service-hystrix的HystrixCloudServiceApplication的启动类
-
启动工程cloud-consumer-hystrix的HystrixFeignCloudConsumerApplication 的启动类
访问
http://127.0.0.1:12082/hystrix.stream
会不断刷新如下信息
ping:
data: {"type":"HystrixCommand","name":"IMyHystrixClient#simpleHystrixClientCall(long)","group":"cloud-hystrix-service"….
如果没有以上打印信息,
请先执行监控的URL
,如本例中
http://127.0.0.1:12082//hystrix-feign/simple
2.3. 启动Hystrix-dashboard界面监控
上面的日志信息不够直观,借助Hystrix-dashboard可对监控进行图形化展示
@EnableHystrixDashboard
在启动类上增加@EnableHystrixDashboard,开启Hystrix dashboard功能
@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient // 配置本应用将使用服务注册和服务发现
@EnableCircuitBreaker // 启动断路器,如果要监控hystrix的流必须开启此注解,即使fegin已经通过属性
@EnableHystrixDashboard // 开启dashboard,通过图形化的方式监控: 查看 http://127.0.0.1:12082/hystrix.stream
public class HystrixFeignCloudConsumerApplication {
…
}
测试
在浏览器中输入:
http://127.0.0.1:12082//hystrix
,此时会得到如下界面
在第一个空格中输入
http://127.0.0.1:12082/hystrix.stream
,点击”Monitor Stream”,进入监控界面
当前你不断刷新
http://127.0.0.1:12082//hystrix-feign/simple
时,以下界面也会相应变化出现
注意:如果进入这个界面没有数据,请刷新这个界面。