Spring Cloud微服务【Finchley.RELEASE版本】(七)Hystrix监控面板
(一)Hystrix监控简介
Hystrix自带了很好的监控面板,用于实时监控服务的调用状况,断路器状态等。我们新建一个spring boot项目,结合之前的几个项目一起,本篇主要讲新增的dashborad项目。
(二)构建项目
依赖引入
主要是下面3个依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
启动类添加注解
@EnableHystrixDashboard
@SpringCloudApplication
public class EurekaFeignApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaFeignApplication.class, args);
}
}
application.properties配置
spring.application.name=hystrix-dashboard
server.port=3004
配置完成,然后访问:http://localhost:3004/hystrix
可以看到如下页面即可:
(二)开启了@HystrixCommand注解的消费者项目
本篇沿用上一篇的eureka-consumer-hystrix demo,因为我市使用的spring boot2.0+spring cloud Finchley.RELEASE版本,所以在原demo里面需要添加部分内容:
添加一个bean,指定/hystrix.stream路径
@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaConsumerApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerApplication.class, args);
}
//版本如果是2.0则需要添加 ServletRegistrationBean 因为springboot的默认路径不是
// "/hystrix.stream",只要在自己的项目里配置上下面的servlet就可以了
@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;
}
}
application.properties配置添加
#配置actuator
management.endpoints.web.exposure.include=[refresh,health,info, hystrix.stream]
(三)测试
做完上述配置,将http://localhost:3003/hystrix.stream填入Hystrix Dashboard页面,然后点击monitor stream,即可成功查看监控面板:
下面是对一些参数的说明:
注意:
可能会出现的问题:
问题一:Unable to connect to Command Metric Stream.
如果出现上面问题,首先检查该有的依赖和注解以及配置是否都正确,如都正确,再看是否添加了spring boot2.0所需要的配置bean和actuator的配置信息,笔者也遇就是使用新版本的spring boot,而没有添加配置就导致一直无法访问。
问题二:一直 load…
访问一下服务再刷新,就可以解决。