前言
上一篇SpringCloud(三):Hystrix服务降级与熔断介绍了 Hystrix 断路器原理、常用配置以及一个配置实例。本篇介绍Hystrix仪表盘组件 Hystrix Dashboard,用于实时监控HystrixCommand方法的执行情况。
Hystrix单实例监控
创建 cloud-hystrix-dashboard 模块,引入相关依赖如下:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
修改项目配置文件 application.yaml 如下:
server:
port: 9001
spring:
application:
name: cloud-hystrix-dashboard
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
# 设置Eureka Server交互的地址查询服务和注册服务地址
defaultZone: http://localhost:7001/eureka/, http://localhost:7002/eureka/
instance:
instance-id: dashboard9001
# 显示IP地址
prefer-ip-address: true
hystrix:
dashboard:
proxy-stream-allow-list: "localhost"
在主启动类上添加@EnableHystrixDashboard
注解,启用仪表盘监控。
@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
public class HystrixDashboardMainApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardMainApplication.class, args);
}
}
启动该模块,访问http://localhost:9001/hystrix,看到如下页面:
界面中给出了监控集群和单个实例的地址。
- 集群实例:https://turbine-hostname:port/turbine.stream
- 单实例:https://hystrix-app:port/actuator/hystrix.stream
由于本地不支持https,因此如果是本地地址应填入http。需要主义被监控的服务需要开启 Actuator 的 hystrix.stream端点,配置信息如下:
management:
endpoints:
web:
exposure:
include: 'hystrix.stream' #暴露hystrix监控端点
启动 cloud-eureka-server 和 cloud-provider-payment 模块,在页面中填入如下信息:
进入监控页面,由于此时没有调用 HystrixCommand 方法,界面没有信息:
多次访问http://localhost:8001/provider/testProperties/1:
此时跳转到监控页面:
Hystrix图表解释
图中小球大小表示流量大小,小球不同颜色对应不同健康状况,曲线表示流量随时间变化情况。
Hystrix集群监控
在 cloud-hystrix-dashboard 引入添加如下依赖,以启用turbine:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
在 application.yaml 中添加对turbine的支持:
turbine:
# true 同一主机上的服务通过host和port的组合来进行区分,默认为true
# false 以host来区分不同的服务,这会使得在本地调试的时候,本机上的不同服务聚合成一个服务来统计,监控中host集群数会为1了因为本地host是一样的
combine-host-port: true
#turbine.app-config: 配置Euraka中的serviceId列表,表明监控哪些服务
app-config: cloud-payment-service
# 1. 省略clusterNameExpression时,此时turbine.aggregator.clusterConfig需要配置想要监控的应用名称,需要监控应用名称必须大写
# 2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default
# 3. 当clusterNameExpression: metadata[‘cluster‘]时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: consumer,则需要配置,同时turbine.aggregator.clusterConfig: consumer
cluster-name-expression: "'default'"
在主启动类加入@EnableTurbine
注解,开启Turbine功能。
@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
@EnableTurbine
public class HystrixDashboardMainApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardMainApplication.class, args);
}
}
启动 eureka-server、hystrix-dashboard 和两个 payment-service,访问eureka注册中心http://localhost:7001/,看到服务信息如下:
访问http://localhost:9001/hystrix添加监控端端点地址:
调用几次接口,得到如下界面:
在 Hosts中可以看到主机数为2,此时实现了对集群的监控。