今早在學習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