接上篇《21.單個FeginClient禁用Hystrix》 Spring Cloud版本為Finchley.SR2版
上一篇我們簡單介紹了存在多個FeignClient的情況下,如何禁用單個的Hystrix。
本篇我們延續之前的Hystrix系列,着重講解一下
本部分官方文檔:https://cloud.spring.io/spring-cloud-static/Finchley.SR4/single/spring-cloud.html#_circuit_breaker_hystrix_dashboard
注:好像Finchley.SR2的文檔已經挂了,最新的是Finchley.SR4的文檔。
還記得之前Hystrix的Metrics Stream支援嗎?Metrics流是用來監控目前API的情況,引入spring-boot-start-actuator的依賴,并設定management.endpoints.web.exposure.include: hystrix.stream後,通路/actuator/hystrix.stream即可看到目前系統的API情況:
之前我們提到過,這種JSON的列印日志閱讀起來十分困難,Spring Cloud給我們提供了“Hystrix Dashboard”圖形化監控看闆,可以讓我們更輕松的擷取應用的API狀态資訊,下面我們就來介紹它。
一、Hystrix Dashboard介紹
根據官方文檔的描述,因為Hystrix會收集每一組HystrixCommand指令名額,而Hystrix Dashboard(儀表盤)就是以一種有效的方式顯示每個斷路器的健康狀況,通過儀表盤我們可以看到Hystrix的各項名額資訊,以便于快速發現系統中存在的問題進而解決它。
下面就是官方文檔提供的一個Hystrix Dashboard的圖形界面樣例:
Hystrix Dashboard有兩種使用政策,一種是對單體應用的監控,另一種是整合Turbine,對叢集進行監控。我們分别來進行介紹。
一、單體應用的監控
按照我們原有的思路,應該是在需要監控的微服務上嵌入Hystrix儀表盤,但是作為微服務體系,我們沒有必要為每一個微服務都嵌入Hystrix Dashboard。合适的做法是,我們需要專門建立一個工程來實作Hystrix Dashboard。
下面我們建立一個工程來實作Hystrix Dashboard。
1、建立一個名為“microserver-hystrix-dashboard”的Spring Boot工程:
2、建立好工程之後,在工程的pom.xml中,新增Spring Cloud的父工程,以及hystrix、hystrix-dashboard以及actuator的依賴:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.microserver.cloud</groupId>
<artifactId>microserver-hystrix-dashboard</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>microserver-hystrix-dashboard</name>
<parent>
<groupId>com.microserver.cloud</groupId>
<artifactId>microserver-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
注意,這裡為了便于版本統一管理,該工程的parent父工程和我們User、Movie工程一樣,均依賴于microserver-spring-cloud工程(此父工程統一引入了spring-cloud-dependencies的Finchley.SR2版,這個在前面的章節已經講過)。
父工程pom.xml的modules中别忘記加入這個新工程(microserver-hystrix-dashboard):
<modules>
<module>microserver-provider-user</module>
<module>microserver-consumer-movie</module>
<module>microserver-discovery-eureka</module>
<module>microserver-discovery-eureka-high-availability</module>
<module>microserver-hystrix-dashboard</module>
</modules>
建立完畢之後工程結構如下:
3、建立啟動類,在啟動類中,添加“@EnableHystrixDashboard”注解,以開啟儀表盤功能:
package com.microserver.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard
public class MicroserverHystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserverHystrixDashboardApplication.class, args);
}
}
4、然後在resource檔案夾下建立application.yml,配置一下應用名和應用端口:
spring:
application:
name: microserver-hystrix-dashboard
server:
port: 2001
工程完整結構:
下面我們啟動該工程,通路“http://localhost:2001/hystrix”,我們可以看到下面的界面:
該界面有幾個需要輸入的參數,在圖中已标注。
現在我們要監控之前的Movie服務,我們将Movie、Eureka Server啟動起來,保證“/hystrix.stream”接口可以正常通路,然後在儀表盤首頁中輸入Movie工程的hystrix.stream監控位址(http://localhost:7901/actuator/hystrix.stream),點選“Monitor Stream”按鈕,就可以看到它的監控頁面了:
下圖是關于監控頁面的詳細解釋:
好了,有關Hystrix Dashboard的單體應用監控就介紹到這,下一篇我們來介紹使用Turbine來實作Hystrix Dashboard的叢集監控(因為篇幅過長,我們拆開成單獨的一篇來講解)。
參考:《51CTO學院Spring Cloud進階視訊》
https://my.oschina.net/xiaominmin/blog/1788456
轉載請注明出處:https://blog.csdn.net/acmman/article/details/102314768