在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。
1. 创建一个eureka_hystrix_dashboard工程,pom.xml
<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>
<groupId>com.kevin</groupId>
<artifactId>eureka_hystrix_dashboard</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka_hystrix_dashboard</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<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-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<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>
</project>
2. Application.java启动监控
package com.kevin.eureka_hystrix_dashboard;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard
public class Application {
public static void main( String[] args ) {
new SpringApplicationBuilder(Application.class)
.web(WebApplicationType.SERVLET).run(args);
}
}
3. application.properties
spring.application.name=eurekaHystrixDashboard
server.port=9203
4. 启动工程后访问http://127.0.0.1:9203/hystrix
上面的文字介绍,Hystrix Dashboard共支持三种不同的监控方式
- 默认的集群监控:通过http://turbine-hostname:port/turbine.stream开启,实现对默认集群的监控。
- 指定的集群监控:通过http://turbine-hostname:port/turbine.stream?cluster=[clusterName]开启,实现对clusterName集群的监控。
- 单体应用的监控:通过http://hystrix-app:port/actuator/hystrix.stream开启,实现对具体某个服务实例的监控。
Delay:控制服务器上轮询监控信息的延迟时间,默认为2000毫秒,可以通过配置该属性来降低客户端的网络和CPU消耗。
Title:该参数可以展示合适的标题。
5. 需要监控消费端的hystrix,比如输入http://127.0.0.1:9201/actuator/hystrix.stream,点击Monitor Stream
1)可能无反应,通过debug浏览器,发现webjars/jquery/2.1.1/jquery.min.js 404 (Not Found)
原因:可能是maven目录下org/webjars/jquery/2.1.1目录下的jar包下载出错导致的,删除后重新下载。然后把eureka_hystrix_dashboard工程重启。(可能需要刷新工程多试两次)
2)提示Unable to connect to Command Metric Stream.
原因:消费端eureka_ribbon不支持/actuator/hystrix.stream
解决方案:
在Application.java注册/actuator/hystrix.stream,即暴露endpoints
@Bean
public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean<HystrixMetricsStreamServlet> registrationBean = new ServletRegistrationBean<HystrixMetricsStreamServlet>(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
通过访问http://127.0.0.1:9201/actuator/hystrix.stream可以正常返回,调用接口http://127.0.0.1:9201/sayHello后会有数据。
再次通过监控查看http://127.0.0.1:9201/actuator/hystrix.stream
以上图来说明其中各元素的具体含义:
- 实心圆:它有颜色和大小之分,分别代表实例的监控程度和流量大小。如上图所示,它的健康度从绿色、黄色、橙色、红色递减。通过该实心圆的展示,我们就可以在大量的实例中快速的发现故障实例和高压力实例。
- 曲线:用来记录 2 分钟内流量的相对变化,我们可以通过它来观察到流量的上升和下降趋势。
6. 基于feign的hystrix dashboard
虽然feign自带了hystrix,但是却不包含hystrix-metrics-event-stream,所以不能直接被监控。还是需要引入hystrix:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
同时暴露endpoints
@Bean
public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean<HystrixMetricsStreamServlet> registrationBean = new ServletRegistrationBean<HystrixMetricsStreamServlet>(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
监控即可访问:
http://127.0.0.1:9202/actuator/hystrix.stream
参考:
https://cloud.spring.io/spring-cloud-static/Greenwich.SR1/single/spring-cloud.html#netflix-hystrix-dashboard-starter
https://www.cnblogs.com/chenweida/p/9025589.html