接上篇《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