SpringCloud之Hystrix Dashboard監控搭建與配置
Hystrix 儀表盤監控
Hystrix 儀表盤(Hystrix Dashboard),就像汽車的儀表盤實時顯示汽車的各項數據一樣,Hystrix 儀表盤主要用來監控 Hystrix 的實時運行狀態,通過它我們可以看到 Hystrix 的各項指標信息,從而快速發現系統中存在的問題進而解決它;
要使用 Hystrix 儀表盤功能,我們首先需要有一個 Hystrix Dashboard項目,這個功能我們可以在原來的消費者應用上添加,讓原來的消費者應用具備 Hystrix 儀表盤功能,但一般地,微服務架構思想是推崇服務的拆分,Hystrix Dashboard 也是一個服務,所以通常會單獨創建一個新的工程專門用做 Hystrix Dashboard 服務;
搭建一個 Hystrix Dashboard 服務的步驟:
第一步:創建一個普通的 Spring Boot 工程
比如創建一個名為
springCloud-hystrix-dashboard 的 Spring Boot 工程,建立好基本的結構和配置;
第二步:添加相關依賴
在創建好的 Spring Boot 項目的 pom.xml 文件中添加相關依賴,如下:
過時了:
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
- <version>1.4.5.RELEASE</version>
- </dependency>
新的依賴:
- <!-- spring-cloud-starter-netflix-hystrix-dashboard -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
- </dependency>
第三步:入口類上添加注解
添加好依賴之后,在入口類上添加@EnableHystrixDashboard 注解開啟儀表盤功能,如下:
- @SpringBootApplication
- @EnableHystrixDashboard
- public class Application {
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- }
第四步:屬性配置
最后,我們可以根據個人習慣配置一下 application.properties 文件,如下:
- server.port=3721
至此,我們的 Hystrix 監控環境就搭建好了;
Hystrix 儀表盤工程已經創建好了,現在我們需要有一個服務,讓這個服務提供一個路徑為/actuator/hystrix.stream 接口,然后就可以使用 Hystrix 儀表盤來對該服務進行監控了;
我們改造消費者服務,讓其能提供/actuator/hystrix.stream 接口,步驟如下:
1、消費者項目需要有 hystrix 的依賴:
過時的:
- <!--Spring Cloud 熔斷器起步依賴-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-hystrix</artifactId>
- <version>1.4.5.RELEASE</version>
- </dependency>
新的:
- <!-- spring-cloud-starter-netflix-hystrix -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
- </dependency>
2、需要有一個 spring boot 的服務監控依賴:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
3、配置文件需要配置 spring boot 監控端點的訪問權限:
- management.endpoints.web.exposure.include=*
這個是用來暴露 endpoints 的,由于 endpoints 中會包含很多敏感信息,除
了 health 和 info 兩個支持直接訪問外,其他的默認不能直接訪問,所以我們
讓它都能訪問,或者指定:
- management.endpoints.web.exposure.include=hystrix.stream
4、訪問入口
http://localhost:8081/actuator/hystrix.stream
注意:這里有一個細節需要注意,要訪問/hystrix.stream 接口,首先得訪問consumer 工程中的任意一個其他接口,否則直接訪問/hystrix.stream 接口時會輸出出一連串的 ping: ping: …,先訪問 consumer 中的任意一個其他接口,然后再訪問/hystrix.stream 接口即可;
Hystrix 儀表盤監控數據解讀