Hystrix Dashboard是一款针对Hystrix进行实时监控的工具,我们可以在直观地看到各 HystrixCommand 和 HystrixObservableCommand 实例的实时数据,比如请求响应时间、请求成功率等等。帮助我们快速发现系统中存在的问题。
一、准备工作
1.1、创建启动一个注册中心,一个服务消费者,一个服务提供者。
请参考:Spring Cloud Hystrix 断路器组件1:熔断器
- piao-server:注册中心服务端,端口2000。
- piao-client:客户端服务提供者,端口2010。
- piao-ribbon:客户端服务消费者,端口2005。
根据我们的参考文章,加入了断路器的代码。这里我们还需要改造消费者应用,在加入端点依赖和配置。
1.2、添加端点依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
1.3、添加配置,暴露所有信息
management.endpoints.web.exposure.include=*
或者只暴露断路器端点信息。
management.endpoints.web.exposure.include=hystrix.stream
1.4、查看端点信息
访问地址:http://localhost:2005/actuator
访问地址:http://localhost:2005/actuator/hystrix.stream
因为监控的实例本身还没有调用任何服务,所以监控端点也没记录任何信息。
二、创建 Hystrix Dashboard 监控
2.1、创建 piao-dashboard 项目。
我们这里使用IDEA创建,文件-》新建-》项目-》下一步到如下的页面:
然后在点下一步,直到完成。
2.2、修改依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.piao</groupId>
<artifactId>piao-dashboard</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>piao-dashboard</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>
<dependencies>
<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-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.3、添加配置
编辑项目的 application.properites 文件,添加如下相关配置:
#服务注册中心端口号
server.port=2081
#指定服务名称
spring.application.name=piao-dashboard
#指定服务注册中心的地址
eureka.client.serviceUrl.defaultZone=http://localhost:2000/eureka/
在启动类上添加 @EnableHystrixDashboard 注解。
@EnableDiscoveryClient
@SpringBootApplication
@EnableHystrixDashboard
public class PiaoDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(PiaoDashboardApplication.class, args);
}
}
2.4、启动项目
请求地址:http://localhost:2081/hystrix
Hystrix Dashboard 共支持三种不同的监控方式:
- 默认的集群监控: http://turbine-hostname:port/turbine.stream
- 指定的集群监控: http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
- 单体应用的监控: http://hystrix-app:port/actuator/hystrix.stream
2.5、查看应用监控
我们输入要查看的应用地址:http://localhost:2005/actuator/hystrix.stream
页面显示“Loading...”是因为监控的实例本身还没有调用任何服务。
注意:服务地址要填写具体的 ip 地址,如果填写 localhost 或者 127.0.0.1 的话可能会出现“Unable to connect to Command Metric Stream.”错误。
我们还可以通过以下设置来解决:
hystrix.dashboard.proxy-stream-allow-list:localhost,127.0.0.1
我们在重新启动应用,进入该页面。
三、 断路器监介绍
- 实心圆: 共有两种含义。通过颜色的变化代表了实例的健康程度,它的健康度从绿色、黄色、橙色、红色递减。通过圆的大小来代表请求流量的大小,流量越大该实心圆就越大。所以通过该实心圆的展示,就可以在大量的实例中快速的发现故障实例和高压力实例。
- 曲线: 记录2分钟内流量的相对变化,可以通过它来观察到流量的上升和下降趋势。
- 其他数量指标如下: