1. 程式人生 > >Spring Cloud第六篇 | Hystrix儀表盤監控Hystrix Dashboard

Spring Cloud第六篇 | Hystrix儀表盤監控Hystrix Dashboard

本文是Spring Cloud專欄的第六篇文章,瞭解前五篇文章內容有助於更好的理解本文:

  1. Spring Cloud第一篇 | Spring Cloud前言及其常用元件介紹概覽

  2. Spring Cloud第二篇 | 使用並認識Eureka註冊中心

  3. Spring Cloud第三篇 | 搭建高可用Eureka註冊中心

  4. Spring Cloud第四篇 | 客戶端負載均衡Ribbon

  5. Spring Cloud第五篇 | 服務熔斷Hystrix

一、Hystrix儀表盤監控

    Hystrix儀表盤( Hystrix Dashboard),就像汽車的儀表盤實時顯示汽車的各 項資料一樣, Hystrix儀表盤主要用來監控 Hystrix的實時執行狀態,通過它我們可以看到 HystriX的各項指標資訊,從而快速發現系統中存在的問題進而解決 要使用 Hystriⅸ儀表盤功能,我們首先需要有一個 Hystrix Dashboard,這個功能我們可以在原來的消費者應用上新增,讓原來的消費者應用具備Hysr儀表 盤功能,但一般地微服務架構思想是推崇服務的拆分, Hystrix Dashboard也是一個服務,所以通常會單獨建立一個新的工程專門用做 Hystrix Dashboard 服務,Hystrix Dashboard所處的作用如圖

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/spirngcloud-learn