一、搭建平台
1.1 导入依赖
<!-- 仪表图形化界面 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!-- 解决:NoClassDefFoundError: com/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspect -->
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
1.2 修改启动类
@SpringBootApplication
// @EnableHystrixDashboard开启Hystrix图形化搭建
@EnableHystrixDashboard
public class HystrixDashboardMain9001 {
public static void main(String[] args){
SpringApplication.run(HystrixDashboardMain9001.class, args);
}
}
1.3 运行测试
http://localhost:9001/hystrix
(9001,我是修改了端口)
二、被监控模块
pom依赖
一定要导入actuator依赖
<!-- actuator:健康检查、审计、统计和监控 -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
修改启动类
@SpringBootApplication
@EnableEurekaClient
// 记得添加
@EnableHystrix
public class PaymentHystrixMain8001 {
public static void main(String[] args){
SpringApplication.run(PaymentHystrixMain8001.class, args);
}
/**
* 解决:Unable to connect to Command Metric Stream.
*/
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
测试
http://localhost:8001/actuator/hystrix.stream
实心圆::颜色:健康度绿色<黄色<橙色<红色递减 ,大小:压力以及流量大
曲线:2min流量的上升和下降趋势