闲话少说:
总共分6步:
(1)添加hystrix依赖以及监控的依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> <version>1.4.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.0.3.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix-dashboard --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> <version>1.4.3.RELEASE</version> </dependency>
2、添加注解:
3、如果是springboot2.0的话需要添加hystrix.stream的访问路径:
加载application中即可:
@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、在配置文件中 添加配置 属性
hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 10000(访问超时配置,默认是1000ms) execution: timeout: enabled: false circuitBreaker: requestVolumeThreshold: 1(断路器状态更改,默认是一个借口有20个请求失败,现在改为1个请求失败即开启断路器)
5、添加@HystrixCommand
直接在有服务调用的地方加上即可,fallback可以不加
6、查看监控界面:需要先访问,然后看下有没有数据,然后进入监控