Fork me on GitHub

熔断器-Hystrix Dashboard

目录

Hystrix监控Ribbon

除了隔离依赖服务的调用以外,Hystrix还提供了近实时的监控,Hystrix会实时、累加地记录所有关于HystrixCommand的执行信息,包括每秒执行多少请求多少成功,多少失败等。Netflix通过hystrix-metrics-event-stream项目实现了对以上指标的监控。

上文提到的microservice-consumer-ribbon-with-hystrix项目已经具备对Hystrix监控的能力,下面我们进入测试。

Hystrix提供了监控Hystrix Command的能力,本节来详细探讨。

监控端点与数据

应用整合Hystrix,同时应用包含spring-boot-starter-actuator 依赖,就会存在一个/actuator/hystrix.stream 端点,用来监控Hystrix Command。当被@HystrixCommand 注解了的方法被调用时,就会产生监控信息,并暴露到该端点中。当然,该端点默认是不会暴露的,需使用如下配置将其暴露。

1
2
3
4
5
6
management:
endpoints:
web:
exposure:
include: 'hystrix.stream'
include: '*'

此时,访问/actuator/hystrix.stream 可返回如下结果:

1
{"type":"HystrixCommand","name":"findById","group":"MovieController","currentTime":1547905939151,"isCircuitBreakerOpen":false,"errorPercentage":0,"errorCount":0,"requestCount":0,"rollingCountBadRequests":0,"rollingCountCollapsedRequests":0,"rollingCountEmit":0,"rollingCountExceptionsThrown":0,"rollingCountFailure":0,"rollingCountFallbackEmit":0,"rollingCountFallbackFailure":0,"rollingCountFallbackMissing":0,"rollingCountFallbackRejection":0,"rollingCountFallbackSuccess":0,"rollingCountResponsesFromCache":0,"rollingCountSemaphoreRejected":0,"rollingCountShortCircuited":0,"rollingCountSuccess":0,"rollingCountThreadPoolRejected":0,"rollingCountTimeout":0,"currentConcurrentExecutionCount":0,"rollingMaxConcurrentExecutionCount":0,"latencyExecute_mean":0,"latencyExecute":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"latencyTotal_mean":0,"latencyTotal":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"propertyValue_circuitBreakerRequestVolumeThreshold":20,"propertyValue_circuitBreakerSleepWindowInMilliseconds":5000,"propertyValue_circuitBreakerErrorThresholdPercentage":50,"propertyValue_circuitBreakerForceOpen":false,"propertyValue_circuitBreakerForceClosed":false,"propertyValue_circuitBreakerEnabled":true,"propertyValue_executionIsolationStrategy":"THREAD","propertyValue_executionIsolationThreadTimeoutInMilliseconds":1000,"propertyValue_executionTimeoutInMilliseconds":1000,"propertyValue_executionIsolationThreadInterruptOnTimeout":true,"propertyValue_executionIsolationThreadPoolKeyOverride":null,"propertyValue_executionIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_metricsRollingStatisticalWindowInMilliseconds":10000,"propertyValue_requestCacheEnabled":true,"propertyValue_requestLogEnabled":true,"reportingHosts":1,"threadPool":"MovieController"}

测试步骤

  1. 启动:microservice-discovery-eureka
  2. 启动:microservice-provider
  3. 启动:microservice-consumer-ribbon-with-hystrix
  4. 访问:http://localhost:8010/ribbon/user/1 ,注意:该步骤不能省略,因为如果应用的所有接口都未被调用,将只会看到一个ping

熔断器-Hystrix Dashboard

  1. 访问:http://localhost:8010/actuator/hystrix.stream,可以看到类似如下输出:

熔断器-Hystrix Dashboard

1
data: {"type":"HystrixCommand","name":"findById","group":"RibbonHystrixService","currentTime":1472658867784,"isCircuitBreakerOpen":false,"errorPercentage":0,"errorCount":0,"requestCount":0,"rollingCountBadRequests":0....}

并且会不断刷新以获取实时的监控数据。但是纯文字的输出可读性实在是太差,运维人员很难一眼看出系统当前的运行状态。那么是不是有可视化的工具呢?

监控对于Feign

前面讲过Feign默认已经整合了Hystrix,但这个整合其实是“不完整”,因为它默认不带有监控端点,如果你在使用Feign的同时,也想使用监控端点,需按照如下步骤操作:

  • 加依赖
1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
  • 在启动类上添加注解@EnableCircuitBreaker
  • application.yml 中添加如下配置:
1
2
3
4
5
management:
endpoints:
web:
exposure:
include: 'hystrix.stream'

测试:http://127.0.0.1:8020/actuator/hystrix.stream

代码示例:

microservice-consumer-movie-feign-hystrix-stream

Hystrix Dashboard

Hystrix Dashboard可以可视化查看实时监控数据。我们可以下载hystrix-dashboard的war包部署到诸如Tomcat之类的容器中,本文不做赘述。另外Spring Cloud也提供了Hystrix Dashboard的整合,下面我们看看Spring Cloud是怎么玩转Hystrix Dashboard的。

新建一个maven项目,在pom.xml中添加如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?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 http://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.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>microservice-hystrix-dashboard</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>microservice-hystrix-dashboard</name>
<description>Demo project for Spring Boot</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<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>
</dependencies>

<!-- 引入spring cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</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>

编写启动类:HystrixDashboardApplication.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* 测试步骤:
* 1. 访问http://localhost:8030/hystrix.stream 可以查看Dashboard
* 2. 在上面的输入框填入: http://想监控的服务:端口/hystrix.stream进行测试
* 注意:首先要先调用一下想监控的服务的API,否则将会显示一个空的图表.
* @author example
*/
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(HystrixDashboardApplication.class).web(true).run(args);
}
}

配置文件:application.yml

1
2
3
4
5
spring:
application:
name: hystrix-dashboard
server:
port: 8030

启动后,访问http://localhost:8030/hystrix.stream 将会看到如下界面:

熔断器-Hystrix Dashboard

启动microservice-consumer-movie-feign-hystrix-stream

启动microservice-hystrix-dashboard

此时,我们在输入框中输入http://localhost:8020/actuator/hystrix.stream ,并随意设置一个Title后,点击Monitor Stream按钮,会出现如下界面:

熔断器-Hystrix Dashboard

此时我们会看到findById这个API的各种指标。Hystrix Dashboard Wiki上详细说明了图上每个指标的含义,如下图:

熔断器-Hystrix Dashboard

熔断器-Hystrix Dashboard

此时,我们可以尝试将microservice-provider停止,然后重复访问多次http://localhost:8020/fegin/user/1 (20次以上),会发现断路器状态会变为开启。

熔断器-Hystrix Dashboard

大功告成!鼓掌!

相关文章

微信打赏

赞赏是不耍流氓的鼓励