本文是Spring Cloud專欄的第六篇文章,了解前五篇文章內容有助於更好的理解本文:
一、Hystrix儀表盤監控介紹
Hystrix儀表盤( Hystrix Dashboard),就像汽車的儀表盤實時顯示汽車的各 項數據一樣, Hystrix儀表盤主要用來監控 Hystrix的實時運行狀態,通過它我們可以看到 HystriX的各項指標信息,從而快速發現系統中存在的問題進而解決 要使用 Hystriⅸ儀表盤功能,我們首先需要有一個 Hystrix Dashboard,這個功能我們可以在原來的消費者應用上添加,讓原來的消費者應用具備Hysr儀表 盤功能,但一般地微服務架構思想是推崇服務的拆分, Hystrix Dashboard也是一個服務,所以通常會單獨創建一個新的工程專門用做 Hystrix Dashboard 服務,Hystrix Dashboard所處的作用如圖
二、Hystrix儀表盤監控應用
1、新建一個模塊命名為(springcloud-hystrix-dashboard)
2、添加依賴hystrix的Dashboard依賴
<!--hystrix-dashboard功能的起步依賴,儀表盤功能--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency>
此時依賴可能下載不下來,可以添加阿里雲倉庫
<repositories> <repository> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
3、在啟動類上添加注解@EnableHystrixDashboard
4、端口配置為3721,到此我們的hystrix監控服務就搭建完畢了,啟動訪問http://localhost:3721/hystrix
Hystrix儀表盤工程已經創建好了,我們需要有一個服務,讓這個服務提供一個路徑為/actuator/hystrix.stream接口,然后就可以使用Hystrix儀表盤來對該服務進行監控了
5、改造消費者(springcloud-service-consumer)
我們改造消費者服務,讓其能提供/actuator/hystrix.stream接口,步驟如下:
5-1、消費者項目需要有Hystrix的依賴,在之前案例中使用服務熔斷的時候已經加過了
5-2、需要有一個springboot的服務監控依賴,可以直接添加到父模塊依賴中
<!--springboot提供的服務健康檢查監控的依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
5-3、配置文件需要配置springboot監控的端點訪問權限
#用來暴露endpoints的,由於endpoints中會包含很多敏感信息,
#除了health和info兩個支持直接訪問外,其他的默認不能直接訪問,
#所以我們讓他們都能訪問(*),或者指定springboot的監控端點訪問權限,
#*表示所有的端點都允許訪問,如果只寫hystrix.stream,他會把默認的info,health端點關閉
management:
endpoints:
web:
exposure:
include: ["info","health","hystrix.stream"]
從控制台日志上可以看到
2019-08-07 15:58:31.187 INFO 7396 --- [ost-startStop-1] o.s.b.a.e.web.ServletEndpointRegistrar : Registered '/actuator/hystrix.stream' to hystrix.stream-actuator-endpoint
6、第一次訪問,要先訪問其他帶用熔斷器的接口,訪問入口:http://localhost:9090/actuator/hystrix.stream
注意:這里有一個細節需要注意,要訪問/hystrix.stream接口,首先訪問消費者(springcloud-service-consumer)工程中任意的一個帶有熔斷器的接口,否則直接訪問/hystrix.stream接口時,會輸出一連串ping: ping: ....
7、把http://localhost:9090/actuator/hystrix.stream填寫到Hystrix Dashboard儀表盤上,直接在網上找了一個詳細描述圖,如下圖
詳細參考案例源碼:https://gitee.com/coding-farmer/springcloud-learn