在学习断路器监控的时候,首先要认识到,监控是一个单独的服务。监控,开启断路器的服务。(服务,进过客户端负载的,如果单个服务,也就没意义)。一定要明白这一点: 服务是服务,监控也是服务,之间没有必然的联系,两者的耦合性几乎为0。
1. 首先导入响应的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<!-- 用于手机metric, 支持hystrix.stream -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 支持dashboard的UI -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
2. 使用maven test命令检测相关jar包加载情况。(只有保证所有包加载成功,才可以进行,不是呆瓜就明白喽*-*)
3. 启动类加入相关注解
@SpringBootApplication
@EnableCircuitBreaker
@EnableHystrixDashboard
4. 确定监控的服务上配置断路器。pom文件中添加依赖
@EnableCircuitBreaker
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
5. 查看spring boot版本,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;
}
6. 测试喽。。。。。。