服务消费者构建Hystrix Dashboard监控端点
Hystrix 仪表盘工程已经创建好了,现在我们需要有一个服务,让这个服务提供一个路径为/actuator/hystrix.stream 接口,然后就可以使用 Hystrix 仪表盘来对该服务进行监控了;
- 我们改造消费者服务,让其能提供/actuator/hystrix.stream 接口,步骤如下:
1、服务消费者项目需要有 hystrix 的依赖(之前已经加好了):
<!--Spring Cloud 熔断器起步依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
2、服务消费者项目需要有一个 spring boot 的服务监控依赖:
<!--spring boot 提供的一个服务健康检查的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
3、配置文件需要配置 spring boot 监控端点的访问权限:
#springboot的监控端点访问权限,*表示所有的访问端点都允许访问
management.endpoints.web.exposure.include=*
这个是用来暴露 endpoints 的,由于 endpoints 中会包含很多敏感信息,除
了 health 和 info 两个支持直接访问外,其他的默认不能直接访问,所以我们
让它都能访问,或者指定:
management.endpoints.web.exposure.include=hystrix.stream
- 4、访问入口 http://localhost:8081/actuator/hystrix.stream
- 访问失败404 在启动类加入
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");//访问路径
registrationBean.setName("hystrix.stream");
return registrationBean;
}
– 再次访问入口 http://localhost:8081/actuator/hystrix.stream
注意:这里有一个细节需要注意,要访问/hystrix.stream 接口,首先得访问
consumer 工程中的任意一个其他接口,否则直接访问/hystrix.stream 接口时
会输出出一连串的 ping: ping: …,先访问 consumer 中的带有熔断器的任意一个其他接口,
然后再访问/hystrix.stream 接口即可;