在Ribbon和Feign项目添加Hystrix仪表盘功能,两个项目添加方式相同
本文在feign添加仪表盘示例
1.在pox.xml中添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
2.在Application里增加@EnableHystrixDashboard
@EnableHystrixDashboard
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerFeignApplication.class, args);
}
}
3. 创建hystrix.stream的Servlet配置
SpringBoot2.x版本开启Hystrix DashBoard与Springboot1.x的方法略有不同,需要增加一个HystrixMetricsStreamServlet的配置,新建config目录,创建HystrixDashboardConfiguration类,代码如下:
@Configuration
public class HystrixDashboardConfiguration {
@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;
}
}
3.运行server,并在地址栏里输入http://localhost:8792/hystrix,出现hystrix监控页面,输入locahost:8792/hystrix.stream,延迟2000ms,title:whale,单击monitor stream
单击monitor stream出现如下显示,说明连接成功
然后在另一个地址栏输入http://localhost:8792/hello,断开提供者,发生刷新熔断,
则监控器如下显示,则说明发生了熔断