在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。
1. 创建一个eureka_hystrix_dashboard工程,pom.xml
4.0.0
com.kevin
eureka_hystrix_dashboard
0.0.1-SNAPSHOT
jar
eureka_hystrix_dashboard
http://maven.apache.org
UTF-8
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-dependencies
Greenwich.SR1
pom
import
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共支持三种不同的监控方式
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 getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(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
以上图来说明其中各元素的具体含义:
6. 基于feign的hystrix dashboard
虽然feign自带了hystrix,但是却不包含hystrix-metrics-event-stream,所以不能直接被监控。还是需要引入hystrix:
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
同时暴露endpoints
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(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