Spring Cloud Hystrix Dashboard仪表盘
一、前言
Hystrix提供了Hystrix Dashboard来实时监控Hystrix的运行情况,通过Hystrix Dashboard反馈的实时信息,可以帮助我们快速发现系统中存在的问题,从而及时地采取应对措施。Spring Cloud对Hystrix Dashboard进行了整合,这里将介绍如何使用Hystrix Dashboard监控单个和多个Hystrix实例。
二、监控单个Hystrix实例
参考 《Spring Cloud Hystrix服务容错》 项目。
2.1 配置被监控方
order-server 项目中:
2.1.1 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.1.2 修改 application.yml,开放端口
management:
endpoints:
web:
exposure:
include: "*"
2.2 配置监控方
2.2.1 新建一个名为 hystrix-dashboard 项目
添加如下依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
2.2.2 新建 application.yml
server:
port: 9300
spring:
application:
name: Hystrix-Dashboard
hystrix:
dashboard:
proxy-stream-allow-list: "localhost"
2.2.3 开启监控功能
在启动类上添加 @EnableHystrixDashboard 注解。
@EnableHystrixDashboard
@SpringBootApplication
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
2.3 测试
2.3.1 启动项目
启动,浏览器访问: http://localhost:9300/hystrix :

2.3.2 监控设置
我们以监控 order-server 为例,在监控界面添加监控信息:
需要监控的服务地址 http://localhost:8900/actuator/hystrix.stream
delay: 请求间隔时间
title: 监控名称
注意:调用一个服务接口(该接口必须实现@HystrixCommand注解),或者调用了服务降级或调用了服务熔断,此时需要监控的服务地址才会出现数据。
点击 monitor stream
批量访问 order-server 服务的下单接口。
最终效果如下:


通过批量访问下单接口,发现图中实心圆和曲线发生了变化。那我们如何根据这两个图形查看监控信息呢?
实心圆:通过颜色的变化代表实例的健康程度,健康度从绿色>黄色>橙色>红色递减。其大小也会根据实例的请求流量发生变化,流量越大实心圆越大。
曲线:用来记录间隔时间内流量的相对变化,通常可以观察到流量的上升和下降趋势。
这张图上面的指标代表什么含义,我们可以参考官方给的图例

三、Turbine集群监控
使用Turbine实现对Hystrix的集群监控的思路是:Turbine从Eureka服务注册中心通过服务名Order-Web获取服务实例,然后Hystrix Dashboard对Turbine进行监控,这样就实现了Hystrix Dashboard同时对多个Hystrix(Ribbon-consumer)实例同时进行监控的功能。
创建一个Spring Boot项目
3.1 导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>
3.2 配置application.yml
server:
port: 8700
spring:
application:
name: Trubine
eureka:
instance:
instance-id: trubine
prefer-ip-address: off
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka01:9001/eureka/ ,http://eureka02:9002/eureka/
turbine:
app-config: Order-Web
cluster-name-expression: new String('default')
combine-host-port: true
上面配置了Eureka-Server的集群地址,并且指定了端口号为8700,应用名称为Turbine。剩下的配置为Turbine的配置,含义如下:
turbine.app-config指定了需要收集监控信息的服务名,这里为Order-Web;
turbine.cluster-name-expression参数指定了集群名称为default, 当服务数量非常多的时候,可以启动多个Turbine 服务来构建不同的聚合集群, 而该参数可以用来区分这些不同的聚合集群,同时该参数值可以在Hystrix仪表盘中用来定位不同的聚合集群,只需在Hystrix Stream的URL中通过cluster参数来指定(即Cluster via Turbine(custom cluster));
turbine.combine-host-port参数设置为七rue, 可以让同一主机上的服务通过主机名与端口号的组合来进行区分, 默认情况下会以host来区分不同的服务,这会使得在本地调试的时候,本机上的不同服务聚合成一个服务来统计。
3.3 配置启动
在启动类上面增加@EnableTurbine、@EnableEurekaClient注解。
@EnableTurbine
@EnableEurekaClient
@SpringBootApplication
public class TurbineApplication {
public static void main(String[] args) {
SpringApplication.run(TurbineApplication.class, args);
}
}
3.4 测试
启动好这些服务后,访问Eureka-server的 http://localhost:9001/ 可看到这些实例:

说明服务都已启动成功,我们观察Turbine的控制台,可以看到如下日志信息:

这时候我们访问Hystrix-dashboard的地址: http://localhost:9300/hystrix ,在页面的地址栏输入: http://localhost:8700/turbine.stream


可以看到Hosts的值已经是3了,并且由于两个Hystrix实例的名称都是Order-Web,所以Turbine 会将相同名称的服务作为整体来看待,汇总成一个监控图。
浙公网安备 33010602011771号