1、新建监控模块:springcloud-consumer-hystrix-dashboard
pom依赖:
<dependencies> <!--Hystrix依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--dashboard依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--Ribbon--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--Eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--我们需要拿到实体类,所以要配置api moudle--> <dependency> <groupId>com.zhixi</groupId> <artifactId>springcloud-01-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
2、编写端口配置
server: port: 9001
3、启用Hystrix仪表板(主配置类)
// 启用Hystrix仪表板 @EnableHystrixDashboard @SpringBootApplication public class DeptConsumerDashboard_9001 { public static void main(String[] args) { SpringApplication.run(DeptConsumerDashboard_9001.class, args); } }
开启9001服务可以看到监控页面:
4、往服务里面放入监控实例
在springcloud-provider-dept-8001-hystrix模块的主启动类中添加servlet
@EnableCircuitBreaker // 启用发现客户端 @EnableDiscoveryClient // 开启Eureka客户端注解,在服务启动后自动向注册中心注册服务 @EnableEurekaClient // 启动类 @SpringBootApplication public class DeptProvider_hystrix_8001 { public static void main(String[] args) { SpringApplication.run(DeptProvider_hystrix_8001.class, args); }
//增加一个 Servlet @Bean public ServletRegistrationBean hystrixMetricsStreamServlet(){ ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet()); //访问该页面就是监控页面 registrationBean.addUrlMappings("/actuator/hystrix.stream"); return registrationBean; } }
5、测试监控
开启7001服务注册
开启9001服务监控
开启hystrix8001服务创建
访问请求,查看监控信息:
进入监控页面:localhost:9001/hystrix
查看监控页面:
原文:https://www.cnblogs.com/zhangzhixi/p/14386606.html