《深入理解Spring Cloud与微服务构建》学习笔记(十五)~使用Hystrix DashBoard监控熔断器状态
继续在前面项目上实践,首先在study_client的pom.xml 必须有以下三个依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
在程序启动类添加 @EnableHystrixDashboard 注解。
依次启动程序:eureka_service的peer1实例,eureka_client的两个实例、study_client.
在浏览器访问:http://localhost:8763/hi 可以看到正常访问eureka_client的两个实例。
然后可以输入:http://localhost:8763/hystrix.stream 查看熔断器的数据指标,会发现这里访问为404 无法访问,此时在启动类添加以下代码(暂时不确定为什么),重启试试:
@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;
}
会发现页面不断刷新熔断器数据显示。
在浏览器输入:http://localhost:8763/hystrix 可以看到熔断器仪表盘界面:
在界面输入以下信息,点击monitor stream :
点击可以看到具体指标数据展示:
还可以在Feign中使用Hystrix Dashboard,操作步骤同上。
在使用Hystrix Dashboard组件监控熔断器状况时,每个服务都有自己的一个Hystrix Dashboard主页,当服务数量很多时,监控很不方便,为了同时监控多个服务的状况,Netflix开源了Hystrix的另一个组件Turbrie。Turbrie聚合了多个Hystrix Dashboard,将多个Hystrix Dashboard组件的数据放在一个页面展示,进行监控。
继续新建一个Module,eureka-monitor-client,作为Turbrie聚合监控的工程,首先在pom下添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
配置application.yaml:
spring:
application.name: service-turbine
server:
port: 9000
turbine:
aggregator:
clusterConfig: default # 指定聚合哪些集群,多个使用","分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
appConfig: study-client,eureka-client,eureka-feign ### 配置Eureka中的serviceId列表,表明监控哪些服务
clusterNameExpression: new String("default")
# 1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称
# 2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default
# 3. 当clusterNameExpression: metadata['cluster']时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC
eureka:
client:
serviceUrl:
defaultZone: http://peer1:8760/eureka/
启动类添加 @EnableTurbine 注解,开启turbine功能。
上述配置了服务名:server-turbine ,app-config配置了需要监控的服务名。
依次启动工程,在浏览器访问:http://localhost:8763/hi http://localhost:8765/hi 可以看到正常访问。
继续访问:http://localhost:8763/hystrix.stream 可以看到刷新熔断监控数据.
但是在启动的时候,会报一个错误
跟我们访问的路径不一样,所以无法监控到,此时需要在配置文件里添加一条配置:instanceUrlSuffix: hystrix.stream ,指定无前缀路径:
此时重新启动服务,再次访问以上页面。
同时在访问:http://localhost:9000/turbine.stream 可以看到turbine刷新监控数据。
访问页面:http://localhost:8763/hystrix 在页面输入http://localhost:9000/turbine.stream,2000毫秒,title,点击Monitor Stream 可以看到页面聚合了study-client、eureka-client的监控数据。