开发版本为 Spring Boot 2.2.4.RELEASE 版本和 Spring Cloud Hoxton.SR1 版本,开发工具为 Eclipse IDE for Enterprise Java Developers(Version: 2019-09 R (4.13.0)),Jave 版本为 1.8。
本文为 https://windmt.com/2018/04/16/spring-cloud-5-hystrix-dashboard/ 的学习笔记。
在上一篇 Hystrix 的介绍中,我们提到断路器是根据一段时间窗内的请求情况来判断并操作断路器的打开和关闭状态的。断路器是根据一段时间窗内的请求情况来判断并操作断路器的打开和关闭状态的。而这些请求情况的指标信息都是 HystrixCommand 和 HystrixObservableCommand 实例在执行过程中记录的重要度量信息,它们除了 Hystrix 断路器实现中使用之外,对于系统运维也有非常大的帮助。这些指标信息会以 “滚动时间窗” 与 “桶” 结合的方式进行汇总,并在内存中驻留一段时间,以供内部或外部进行查询使用,Hystrix Dashboard 就是这些指标内容的消费者之一。
下面我们基于之前的示例来结合 Hystrix Dashboard 实现 Hystrix 指标数据的可视化面板,这里我们将用到下之前实现的几个应用,包括:
- eureka-server:服务注册中心
- eureka-producer:服务提供者
- eureka-consumer-feign-hystrix:使用 Feign 和 Hystrix 实现的服务消费者
创建 Hystrix Dashboard
- 创建一个 Spring boot 工程
打开 File -> New -> Project,选择 Spring Starter Project,点击 Next。

name 设置为 hystrix-dashboard,点击 Next。

选择 Hystrix [Maintenance] 和 Hystrix Dashboard [Maintenance],点击 Finish。

- 自动生成的 pom.xml 文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>hystrix-dashboard</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hystrix-dashboard</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
其中比较重要的配置为:
<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>
- 在 resource 文件夹下添加 application.yml 配置文件
spring:
application:
name: hystrix-dashboard
server:
port: 11000
- 启动类 HystrixDashboardApplication 上添加注解
@EnableHystrixDashboard,启用 Hystrix Dashboard 功能。
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@EnableHystrixDashboard
@SpringBootApplication
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
- 启动
启动应用后,访问 http://localhost:11000/hystrix,可以看到如下页面:

为服务实例添加 endpoint
既然 Hystrix Dashboard 监控单实例节点需要通过访问实例的 /actuator/hystrix.stream 接口来实现,自然我们需要为服务实例添加这个 endpoint。
- POM配置
在 pom.xml 中的dependencies中添加spring-boot-starter-actuator监控模块以开启监控相关的端点,并确保已经引入断路器的依赖spring-cloud-starter-netflix-hystrix。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 启动类
为启动类添加@EnableHystrix注解,开启断路器功能:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableHystrix
public class EurekaConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerFeignApplication.class, args);
}
}
- 配置文件
在配置文件 application.yml 中添加management.endpoints.web.exposure.include,如下:
spring:
application:
name: eureka-consumer-feign-hystrix
eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka/ # 指定 Eureka 注册中心的地址
instance:
hostname: localhost
server:
port: 9003
feign:
hystrix:
enabled: true
management:
endpoints:
web:
exposure:
include: hystrix.stream
测试
依次启动项目 eureka-server、eureka-producer、eureka-consumer-feign-hystrix 后,
直接在浏览器访问 http://localhost:9003/actuator/hystrix.stream,此时结果应如下:

然后再首次调用服务 http://localhost:9003/hello/windmt 后,再次访问 http://localhost:9003/actuator/hystrix.stream,则会出现以下结果,说明一切正常。

在 Hystrix-Dashboard 的主界面上输入 eureka-consumer-feign-hystrix 对应的地址 http://localhost:9003/actuator/hystrix.stream 然后点击 Monitor Stream 按钮,进入页面:

界面解读

以上图来说明其中各元素的具体含义:
- 实心圆:它有颜色和大小之分,分别代表实例的监控程度和流量大小。如上图所示,它的健康度从绿色、黄色、橙色、红色递减。通过该实心圆的展示,我们就可以在大量的实例中快速的发现故障实例和高压力实例。
- 曲线:用来记录 2 分钟内流量的相对变化,我们可以通过它来观察到流量的上升和下降趋势。
其他一些数量指标如下图所示:

单个应用的熔断监控已经完成。
本文详细介绍如何在Spring Boot和Spring Cloud环境下搭建Hystrix Dashboard,实现服务实例的Hystrix指标数据可视化。涵盖工程创建、配置、启动及监控流程。
937

被折叠的 条评论
为什么被折叠?



