springcloud配置仪表盘时,出现proxyStreamAllowList报错导致Unable to connect to Command Metric Stream的问题

今早在学习spring cloud的仪表盘配置时,出现了以下的问题:

先说下我配置的过程:

搭建一个 Hystrix Dashboard 服务的步骤:

第一步:创建一个普通的 Spring Boot 工程

比如创建一个名为 springcloud-dashboard 的 Spring Boot 工程,建立好基本的结构和配置;

第二步:添加相关依赖

在创建好的 Spring Boot 项目的 pom.xml 文件中添加相关依赖,如下:

 1 <!-- spring-cloud-starter-netflix-hystrix-dashboard -->
 2 
 3 <dependency>
 4 
 5     <groupId>org.springframework.cloud</groupId>
 6 
 7     <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
 9 
10 </dependency>

第三步:入口类上添加注解

添加好依赖之后,在入口类上添加@EnableHystrixDashboard 注解开启仪表盘功能,如下:

 1 @SpringBootApplication
 2 
 3 @EnableHystrixDashboard
 4 
 5 public class Application {
 6 
 7   public static void main(String[] args) {
 8 
 9     SpringApplication.run(Application.class, args);
10 
11   }
12 
13 }

第四步:属性配置

最后,根据个人习惯配置一下 application.properties 文件,如下(端口号随意起,你喜欢就好):

1 server.port=3761

至此,我们的 Hystrix 监控环境就搭建好了;

运行该项目,在浏览器输入:http://localhost:3761/hystrix 访问,如下图所示:

在消费者模块中:

第一步:添加依赖

1 <!-- spring-cloud-starter-netflix-hystrix -->
2 
3 <dependency>
4 
5     <groupId>org.springframework.cloud</groupId>
6 
7     <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
8 
9 </dependency>
1 <dependency>
2 
3 <groupId>org.springframework.boot</groupId>
4 
5 <artifactId>spring-boot-starter-actuator</artifactId>
6 
7 </dependency> 

第二步:在消费者模块中配置一下暴露服务端点,如果不配置,仪表盘连接不上的:

management.endpoints.web.exposure.include=

这个是用来暴露 endpoints 的,由于 endpoints 中会包含很多敏感信息,除了 health 和 info 两个支持直接访问外,其他的默认不能直接访问,上面的配置是让所有的端口都能访问

以下的配置只允许访问指定的端口即可,如下:

management.endpoints.web.exposure.include=hystrix.stream

这里我采用的是只允许访问指定的端口

第三步:访问入口 http://localhost:8080/actuator/hystrix.stream(我这里消费者的端口号是8080)

注意:这里有一个细节需要注意,要访问/hystrix.stream 接口,首先得访问消费者工程中的任意一个其他接口,否则直接访问/hystrix.stream 接口时会输出出一连串的 ping: ping: …,先访问 consumer 中的任意一个其他接口,然后再访问/hystrix.stream 接口即可;

---------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------

配置到这里就完成了,但在测试的时候还是出现了开头的问题,仪表盘还是连接不上,并且仪表盘模块出现了以下的报错:Origin parameter: http://localhost:8080/actuator/hystrix.stream is not in the allowed list of proxy host names.  If it should be allowed add it to hystrix.dashboard.proxyStreamAllowList.

 其实呢,提示错误提示信息已经提升得很明显了,说 http://localhost:8080/actuator/hystrix.stream这个地址中的localhost不存在于proxyStreamAllowList这个数组当中,应该将localhost添加到这个数组当中去

因此,在上面的步骤之外,还需要在仪表盘模块的配置文件(properties)中添加:hystrix.dashboard.proxyStreamAllowList=localhost 才可以

yml配置文件是:

1 hystrix:
3   dashboard:
5     proxyStreamAllowList: "localhost"

需要注意的是,不同版本的配置的信息可能有所差异,我这里使用的spring cloud版本是H版的最新版是这个配置,但这个配置项不是绝对的,例如之前的版本的配置是:

1 hystrix:
2   dashboard:
3     proxy-stream-allow-list: "localhost"

所以在配置的时候,要根据报错信息的具体提示进行配置。

接着重启仪表盘模块,就这样,仪表盘出来了:

 参考地址:https://www.jianshu.com/p/0a682e4781b0

原文地址:https://www.cnblogs.com/ljfsmile0613/p/14420399.html