spring cloud开发Greenwich版:断路器监控可视化(九)

前面的文章,我们利用RestTemplate对象和feign组件实现了微服务之间的通信,并且通过集成Hystrix,从而实现了容错及故障恢复功能

本文主要实现对Hystrix的可视化监控

这里新建微服务’ralab-hystrix-dashboard’

  • New microsevice ralab-hystrix-dashboard and add the Maven spring-cloud-starter-netflix-hystrix-dashboard denpendency
  • Add @EnableHystrixDashboard to the application class
  • Config info to application.yml file

同时被监控的微服务

  • Add spring-boot-starter-actuator denpendency
  • Add management.endpoints.web.exposure.include=hystrix.stream parameter to the application file

微服务’ralab-hystrix-dashboard’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 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.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ralab</groupId>
<artifactId>ralab-hystrix-dashboard</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ralab-hystrix-dashboard</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.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>

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

RalabHystrixDashboardApplication文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.ralab.ralabhystrixdashboard;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@SpringBootApplication
@EnableHystrixDashboard
public class RalabHystrixDashboardApplication {

public static void main(String[] args) {
SpringApplication.run(RalabHystrixDashboardApplication.class, args);
}

}

application.yml文件

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

运行RalabHystrixDashboardApplication,打开http://localhost:8080/hystrix

同时被监控的微服务加入依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

曝露hystrix端点

1
2
3
spring.application.name=nlp-client
server.port=8091
management.endpoints.web.exposure.include=hystrix.stream

这时在输入框输入’http://localhost:8091/actuator/hystrix.stream',点击Monitor stream按钮,就可以看到如下页面了

注:spring cloud还提供了turbine组件对多个微服务的Hystrix信息进行聚合,从而实现对微服务cluster的Hystrix信息的可视化监控

坚持原创技术分享,您的支持是我前进的动力!