04)springcloud学习 熔断器 Hystrix 实战_qq445829096的博客-CSDN博客
在04)章节已经在pesservice01项目实现了hystrix熔断器的效果,本章节实现hystrix监控
1.pesservice01项目新增依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
(注意:如果不引入这个依赖,http://127.0.0.1:8012/hystrix.stream将无法监控请求数量,以及报错情况,"errorCount":0,"requestCount":0 始终都是0
actuator是springboot的4个核心之一
actuator是springboot程序的监控系统,可以实现健康检查,info信息等。在使用之前需要引入spring-boot-starter-actuator,并做简单的配置即可。)
2.pesservice01项目配置hystrix.stream路径servlet
通过注解形式配置servlet
package com.wying.pesservice01.servletConfig;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* description:hystrix监控 配置servlet
* date: 2022/1/25
* author: gaom
* version: 1.0
*/
@Configuration
public class HystrixConfig {
@Bean
public HystrixMetricsStreamServlet hystrixMetricsStreamServlet(){
return new HystrixMetricsStreamServlet();
}
@Bean
public ServletRegistrationBean registration(HystrixMetricsStreamServlet servlet){
ServletRegistrationBean registrationBean = new ServletRegistrationBean();
registrationBean.setServlet(servlet);
registrationBean.setEnabled(true);//是否启用该registrationBean
registrationBean.addUrlMappings("/hystrix.stream");
return registrationBean;
}
}
如果不想新建一个配置类,直接在Pesservice01Application.java配置也行,我选择第一种新建配置类的方式
3.项目都启动后,访问http://127.0.0.1:8012/hystrix.stream
3.1该servlet会一直监听hystrix执行情况,目前因为hystrix熔断器的代码还没运行,没返回数据
3.2 postman测试-没触发熔断的请求
postman访问服务后 输出了数据,因为正常执行,所以错误数为0,请求数为1
3.3 postman测试 触发熔断的请求
制造一个空指针异常
postman访问服务
4.配置hystrix-dashboard 仪表盘监控
因为http://127.0.0.1:8012/hystrix.stream监控的可视化效果太差了,图形化的界面会比较友好
我这边自己搭框架学习用的,所以在pesservice01项目中集成了hystrix-dashboard, 正式环境还是要单独建一个hystrix-dashboard项目,把hystrix-dashboard独立出来,通过hystrix-dashboard监控所有的微服务
4.1 pesservice01项目新增依赖hystrix-dashboard依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
4.2 pesservice01项目启动类增加注解@EnableHystrixDashboard
5. 浏览器访问http://127.0.0.1:8012/hystrix hystrix-dashboard可视化界面
5.1 输入监控地址,点击Monitor Stream
提示Unable to connect to Command Metric Stream.
后面打印出警告
WARN 16636 --- [nio-8012-exec-2] ashboardConfiguration$ProxyStreamServlet : Origin parameter: http://127.0.0.1:8012/hystrix.stream is not in the allowed list of proxy host names. If it should be allowed add it to hystrix.dashboard.proxyStreamAllowList.
解决方法:
在对应dashboard工程加yml文件增加配置,我这里是在pesservice01项目增加
proxy-stream-allow-list配置的127.0.0.1.正式环境可根据服务器ip修改
hystrix:
dashboard:
proxy-stream-allow-list: "127.0.0.1"
再次输入监控地址,点击Monitor Stream
5.2 postman执行几个请求测试 ,正常的,触发熔断的都模拟一些
6.测试总结
通过可视化监控发现错误和请求不是累加的,是某一段时间内的,过1段时间没报错和请求熟了,都会重置为0,波形图也是一条直线了,类似心电图的概念。并没有历史记录的功能,我们只能实时查看,根据波形图的走势,及显示的报错数量判断系统这一时刻出现过问题。想要把所有报错记录下来估计要重新他的实现方法了,自己保存了。