SpringCloud之Hystrix Dashboard監(jiān)控搭建與配置
Hystrix 儀表盤監(jiān)控
Hystrix 儀表盤(Hystrix Dashboard),就像汽車的儀表盤實(shí)時(shí)顯示汽車的各項(xiàng)數(shù)據(jù)一樣,Hystrix 儀表盤主要用來監(jiān)控 Hystrix 的實(shí)時(shí)運(yùn)行狀態(tài),通過它我們可以看到 Hystrix 的各項(xiàng)指標(biāo)信息,從而快速發(fā)現(xiàn)系統(tǒng)中存在的問題進(jìn)而解決它;
要使用 Hystrix 儀表盤功能,我們首先需要有一個(gè) Hystrix Dashboard項(xiàng)目,這個(gè)功能我們可以在原來的消費(fèi)者應(yīng)用上添加,讓原來的消費(fèi)者應(yīng)用具備 Hystrix 儀表盤功能,但一般地,微服務(wù)架構(gòu)思想是推崇服務(wù)的拆分,Hystrix Dashboard 也是一個(gè)服務(wù),所以通常會(huì)單獨(dú)創(chuàng)建一個(gè)新的工程專門用做 Hystrix Dashboard 服務(wù);
搭建一個(gè) Hystrix Dashboard 服務(wù)的步驟:
第一步:創(chuàng)建一個(gè)普通的 Spring Boot 工程
比如創(chuàng)建一個(gè)名為
springCloud-hystrix-dashboard 的 Spring Boot 工程,建立好基本的結(jié)構(gòu)和配置;
第二步:添加相關(guān)依賴
在創(chuàng)建好的 Spring Boot 項(xiàng)目的 pom.xml 文件中添加相關(guān)依賴,如下:
過時(shí)了:
- <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);
- }
- }
第四步:屬性配置
最后,我們可以根據(jù)個(gè)人習(xí)慣配置一下 application.properties 文件,如下:
- server.port=3721
至此,我們的 Hystrix 監(jiān)控環(huán)境就搭建好了;
Hystrix 儀表盤工程已經(jīng)創(chuàng)建好了,現(xiàn)在我們需要有一個(gè)服務(wù),讓這個(gè)服務(wù)提供一個(gè)路徑為/actuator/hystrix.stream 接口,然后就可以使用 Hystrix 儀表盤來對該服務(wù)進(jìn)行監(jiān)控了;
我們改造消費(fèi)者服務(wù),讓其能提供/actuator/hystrix.stream 接口,步驟如下:
1、消費(fèi)者項(xiàng)目需要有 hystrix 的依賴:
過時(shí)的:
- <!--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、需要有一個(gè) spring boot 的服務(wù)監(jiān)控依賴:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
3、配置文件需要配置 spring boot 監(jiān)控端點(diǎn)的訪問權(quán)限:
- management.endpoints.web.exposure.include=*
這個(gè)是用來暴露 endpoints 的,由于 endpoints 中會(huì)包含很多敏感信息,除
了 health 和 info 兩個(gè)支持直接訪問外,其他的默認(rèn)不能直接訪問,所以我們
讓它都能訪問,或者指定:
- management.endpoints.web.exposure.include=hystrix.stream
4、訪問入口
http://localhost:8081/actuator/hystrix.stream
注意:這里有一個(gè)細(xì)節(jié)需要注意,要訪問/hystrix.stream 接口,首先得訪問consumer 工程中的任意一個(gè)其他接口,否則直接訪問/hystrix.stream 接口時(shí)會(huì)輸出出一連串的 ping: ping: …,先訪問 consumer 中的任意一個(gè)其他接口,然后再訪問/hystrix.stream 接口即可;
Hystrix 儀表盤監(jiān)控?cái)?shù)據(jù)解讀