一.导入依赖
1.创建Dashboard模块并导入依赖
<!--Hystrix熔断依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
<!--Dashboard依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
2.为被监控的服务提供端导入依赖
<!--actuator监控信息依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.6.7</version>
</dependency>
<!--Dashboard依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
<!--Hystrix熔断依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
二.代码部分
1.编写Dashboard模块主启动类
注:为主启动类添加@EnableHystrixDashboard注解以开启Dashboard监控页面
@SpringBootApplication
@EnableHystrixDashboard//开启Dashboard监控页面
public class DeptConsumerDashboard_9001 {
public static void main(String[] args) {
SpringApplication.run(DeptConsumerDashboard_9001.class);
}
}
2.为服务提供端主启动类添加bean
//配置收集流数据的servlet
//访问 http://localhost:8001/actuator/hystrix.stream
@Bean
public ServletRegistrationBean SRB(){
ServletRegistrationBean servletRB = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
// servletRB.setName("HystrixMetricsStreamServlet");//名字
// servletRB.setLoadOnStartup(1);//启动优先级
servletRB.addUrlMappings("/actuator/hystrix.stream");
return servletRB;
}
三.配置yml文件
配置Dashboard模块的yml文件
server:
port: 9001
hystrix:
dashboard:
proxy-stream-allow-list: "localhost" #允许Dashboard监控的流的ip地址
运行效果
访问http://localhost:9001/hystrix
在该页面访问http://localhost:8001/actuator/hystrix.stream
注:需要先访问一次启用了熔断机制的服务,否则将一直loading,无法获取数据
进行多次错误请求,可以看到此时熔断机制已经开启