我使用的SpringBoot版本是2.3.5,SpringCloud版本是SR9。
dashboard的module中导入的包的依赖如下:
<!-- hystrix-dashboard-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
被监控服务中导入的监控依赖如下:
<!-- 完善监控信息-->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.3.6.RELEASE</version>
</dependency>
服务启动类中需要添加一个servlet,代码如下:
//增加一个servlet,增加监控固定代码 dashboard
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registrationBean.addUrlMappings("/actuator/hystrix.stream");
return registrationBean;
}
然后启动注册中心、服务和dashboard,注册和服务都是能成功运行的,网页也能成功显示,包括http://localhost:8001/actuator/hystrix.stream中的ping,也能显示data。如下图所示:
我设置的hystrix dashboard的端口是9001,所以打开http://localhost:9001/hystrix进入dashboard首页,这也能进,如下图:
然后输入http://localhost:8001/actuator/hystrix.stream,延迟设2000,title设为demo,后两个设置其实也没什么影响的,但是就是一直报错,通过网页检查显示找不到连接。
随后就通过百度找相应解决方案,网上大部分方案都是修改actuator的配置,在被监控服务的application配置文件中加入以下设置:
#熔断监控
management:
endpoints:
web:
exposure:
include: "*"
修改之后还是没用,dashboard始终连接不到任何链接。之后还尝试了修改dashboard、autuator的版本,并没有卵用。
几乎把网上的解决方案都找完了,自己仔细回顾这些问题,首先是注册中心、服务和dashboard启动是没问题的,单个访问也是没问题的,反正就是监控这里哪儿的权限出了问题,网上大部分的方案都是actuator暴露点的设置,将include设置为'*',这我也试过了。最后终于看到一个博客是在dashboard的配置中改权限了,在dashboard的application配置文件中加入如下配置:
hystrix:
dashboard:
proxy-stream-allow-list: "*"
通过如上配置,添加所有进入允许列表,这是再重新启动就能访问了。
网上还有其他解决方案说是可以设置proxy-stream-allow-list: "localhost" ,这样也能访问,最重要的就是设置这个proxyStreamAllowList的值了,有时候读源码看原理是特别重要的,不懂原理遇到坑的时候真的是要多浪费很多时间。