Spring Cloud 入门教程(08):断路器指标数据监控Hystrix Dashboard 和 Turbine

Hystrix Dashboard (断路器:hystrix 仪表盘)

Hystrix一个很重要的功能是,可以通过HystrixCommand收集相关数据指标. Hystrix Dashboard可以很高效的现实每个断路器的健康状况。

1)加入依赖

在Ribbon服务和Feign服务的Maven工程的pom.xml中添加

1
2
3
4
5
6
7
8
<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-dashboard</artifactId>
</dependency>

spring-boot-starter-actuator用于手机metric, 支持hystrix.stream。spring-cloud-starter-netflix-hystrix-dashboard支持dashboard的UI

2)添加注解

在Spring Boot启动类上用@EnableHystrixDashboard注解和@EnableCircuitBreaker注解。需要特别注意的是我们之前的Feign服务由于内置断路器支持, 所以没有@EnableCircuitBreaker注解,但要使用Dashboard则必须加,如果不加,Dashboard无法接收到来自Feign内部断路器的监控数据,会报“Unable to connect to Command Metric Stream”错误

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
package org.cwalzy.springcloudeureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
//import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;

@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrixDashboard
//@EnableDiscoveryClient
public class SpringcloudHelloworldFeignClientApplication {

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

//https://www.cnblogs.com/mark7/p/8920288.html
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}

3)然后就可以访问/hystrix,这个URL将dashboard指向定义在Hystrix客户端应用中的 /actuator/hystrix.stream
enter description here

在dashboard中输入服务的URL:点击 monitor后进入监控界面,访问我们之前创建的Ribbon服务localhost:8901/, 或者Feign服务localhost:8902/可以看到监控UI动态变化
enter description here

利用Turbine在一个Dashboard上监控多个流

以上例子只能监控一个,要同时监控多个流怎么办? 答案是, 可以单独做一个Turbine服务,专门监控所有断路器状态,从而掌握整个系统中所有微服务的状态。下面我们就来创建一个Turbine服务,来监控我们之前做的Feign服务和Ribbon服务

1). 创建一个maven工程, 在pox.xml添加以下依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-turbine</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-dashboard</artifactId>
</dependency>

整个个pox.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?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.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.cwalzy</groupId>
<artifactId>springcloud.helloworld.turbine.service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud.helloworld.turbine.service</name>
<description>Turbine service demo</description>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-turbine</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-dashboard</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). 创建Turbine Dashboard启动类:

用@EnableHystrixDashboard和@EnableTurbine修饰主类, 分别用于支持Hystrix Dashboard和Turbine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package org.cwalzy.ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class TurbineDashboardApplication {

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

3). 在application.yml中配置turbine参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8903
spring:
application:
name: hystrix-dashboard-turbine
turbine:
#定义了要监控的服务
appConfig: service-feign, service-ribbon
aggregator:
clusterConfig: default ##需要监控的服务集群名,default
clusterNameExpression: new String("default")
instanceUrlSuffix:
##key为clusterConfig的集群名字,默认为default
##value为集群的hystrix监控url后缀,springboot2.0默认为 actuator/hystrix.stream
default: hystrix.stream

turbine.appConfig定义了要监控的服务,这里是我们在前面章节创建的service-feign和sercice-ribbon; aggregator.clusterConfig定义了聚合方式, 此处为default.

1
2
3
4
5
6
7
8
9
10
11
turbine.appConfig :配置Eureka中的serviceId列表,表明监控哪些服务

turbine.aggregator.clusterConfig :指定聚合哪些集群,多个使用”,”分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问

turbine.clusterNameExpression :指定集群名称,可以是三种类型的值

- 默认表达式为appName;此时turbine.aggregator.clusterConfig需要配置想要监控的应用名称;

- 当为default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default;

- 当为metadata[‘cluster’]时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC

4)启动服务

依次启动eureka服务, 2个Helloworld服务, Feign服务,ribbon服务和刚创建turbine服务。从eureka服务中我们可以看到
enter description here

5)通过Turbine服务访问HystrixDashborad, http:localhost:8903/hystrix

enter description here

监控流的URL填 http://localhost:8903/turbine.stream , 点击monitor stream, 进入监控页面, 随便刷新下feign和ribbon服务( http://localhost:8902/hellohttp://localhost:8901 ), 可以看到监控页面的变化。如下图, 两个服务的监控都会显示在dashboard上
enter description here

参考:
http://projects.spring.io/spring-cloud/spring-cloud.html#_circuit_breaker_hystrix_dashboard
http://blog.csdn.net/ityouknow/article/details/72625646

本文转载自 https://www.cnblogs.com/chry/p/7286601.html
原文基于 spring boot 1.5.3.RELEASE 版本
而本文中有一些改动 是基于 spring boot 2.1.5.RELEASE 版本