首先有两种设置为HystrixDashboard设置servlet的方式:
至于为什么要设置servlet,已经有很多大神讲解,在此不再赘述。
1. 创建类继承servlet
@WebServlet("/actuator/hystrix.stream") public class HystrixServlet extends HystrixMetricsStreamServlet { }
2.主类添加扫包注解:
@ServletComponentScan("com.springcloud.XXX")
3.其他步骤相同
导入maven依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
主类添加注解
@EnableCircuitBreaker
@EnableHystrixDashboard
修改配置文件:
hystrix: dashboard: proxy-stream-allow-list: "*"
另一种配置servlet方法,也是网上用的最多的。
直接在主类上添加一个方法:
@Bean public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet() { HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean<HystrixMetricsStreamServlet> registrationBean = new ServletRegistrationBean<>(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/actuator/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; }
其他步骤并无任何不同。
值得注意的是访问目录设置成
/actuator/hystrix.stream
或
/hystrix.stream
并无影响(暂时未发现)所以配置哪个都应该没问题。但豪猪哥的首页已经提示你配第一个路径,所以按规矩来,可以规避菜鸟期遇到的各种奇葩问题,为你累积奋进的力量也更有助益