SpringCloud极简入门(八)Hystrix Dashbo
2018-07-27 本文已影响7人
叩丁狼教育
作者:陈刚,叩丁狼高级讲师。原创文章,转载请注明出处。
一.什么是Hystrix Dashboard
从上一章节中我们知道,为了防止服务实例因为故障出现整个应用崩塌的情况而出现了熔断器模型。而 Hystrix Dashboard(仪表盘)则是用来监控Hystrix的熔断器状况的一个组件,它提供了数据监控,和友好的图形化展示界面,能让使用者很好的监控和分析熔断器的状态。
二.使用Hystrix Bashboard
1.基于项目Consumer进行改造,添加 Actuator(监控)依赖 和 Hystrix-Dashboard 依赖
<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>
2.找到主程序类,打上 @EnableHystrixDashboard标签,开启Hystrix Dashboard 功能
@EnableHystrix
@EnableHystrixDashboard //开启 Hystrix Dashboard (断路器:Hystrix 仪表盘)
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumerApplication {
3.在主程序类中配置Bean,注册 /hystrix.stream 仪表盘访问地址对应的Servlet:HystrixMetricsStreamServlet
注意:在SpringBoot 2.0是需要对该Servlet进行注册,如果您用的是2.0以下的版本则可以不做此操作
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
4.测试:依次启动项目 EurekaServer ,Producer , Consumer ,访问地址:http://localhost:3333/hystrix 即可看到如下画面:
image.png
依次填写:http://localhost:3333/hystrix.stream ,2000 ,demo1 点击 Monitor 进入各项数据指标页面
image.png
image.png
![](https://img.haomeiwen.com/i807144/f24ed215c47d8711.jpeg)