Spring Cloud學習筆記【五】Hystrix Dashboard監控面板


ystrix除了隔離依賴服務的調用以外,Hystrix 還提供了准實時的調用監控(Hystrix Dashboard),Hystrix 會持續地記錄所有通過 Hystrix 發起的請求的執行信息,並以統計報表和圖形的形式展示給用戶,包括每秒執行多少請求多少成功,多少失敗等。

下面我們基於之前的示例來結合 Hystrix Dashboard 實現 Hystrix 指標數據的可視化面板,這里我們將用到下之前實現的幾個應用,包括:

  • eureka-server:服務注冊中心
  • service-producer:服務提供者
  • service-hystrix-feign:使用 Feign 和 Hystrix 實現的服務消費者

創建 Hystrix Dashboard

創建一個標准的 Spring Boot 工程,命名為:service-hystrix-dashboard

POM依賴

1 <dependency>
2     <groupId>org.springframework.cloud</groupId>
3     <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
4 </dependency>
5 <dependency>
6     <groupId>org.springframework.cloud</groupId>
7     <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
8 </dependency>

屬性配置(application.yml)

spring:
  application:
    name: service-hystrix-dashboard
server:
  port: 11000

啟動類

在 Spring Boot 的啟動類上面引入注解@EnableHystrixDashboard,啟用 Hystrix Dashboard 功能

 1 package com.carry.springcloud;  2 
 3 import org.springframework.boot.SpringApplication;  4 import org.springframework.boot.autoconfigure.SpringBootApplication;  5 import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;  6 
 7 @EnableHystrixDashboard  8 @SpringBootApplication  9 public class ServiceHystrixDashboardApplication { 10 
11     public static void main(String[] args) { 12         SpringApplication.run(ServiceHystrixDashboardApplication.class, args); 13  } 14 }

啟動應用,然后再瀏覽器中輸入 http://localhost:11000/hystrix 可以看到如下界面

通過 Hystrix Dashboard 主頁面的文字介紹,我們可以知道,Hystrix Dashboard 共支持三種不同的監控方式:

前兩者都對集群的監控,需要整合 Turbine 才能實現。這一部分我們先實現對單體應用的監控,這里的單體應用就用我們之前使用 Feign 和 Hystrix 實現的服務消費者——service-hystrix-feign。

頁面上的另外兩個參數:

  • Delay:控制服務器上輪詢監控信息的延遲時間,默認為 2000 毫秒,可以通過配置該屬性來降低客戶端的網絡和 CPU 消耗。
  • Title:該參數可以展示合適的標題。

 

為服務實例service-hystrix-feign添加 endpoint

Hystrix Dashboard 監控單實例節點需要通過訪問實例的/actuator/hystrix.stream接口來實現,所以我們需要為服務實例添加這個 endpoint

POM依賴

在服務實例pom.xml中的dependencies節點中新增spring-boot-starter-actuator監控模塊以開啟監控相關的端點,並確保已經引入斷路器的依賴spring-cloud-starter-netflix-hystrix

1 <dependency>
2     <groupId>org.springframework.boot</groupId>
3     <artifactId>spring-boot-starter-actuator</artifactId>
4 </dependency>

啟動類

為啟動類添加@EnableCircuitBreaker@EnableHystrix注解,開啟斷路器功能

 1 package com.carry.springcloud;  2 
 3 import org.springframework.boot.SpringApplication;  4 import org.springframework.boot.autoconfigure.SpringBootApplication;  5 import org.springframework.cloud.netflix.hystrix.EnableHystrix;  6 import org.springframework.cloud.openfeign.EnableFeignClients;  7 
 8 @EnableHystrix  9 @EnableFeignClients 10 @SpringBootApplication 11 public class ServiceHystrixFeignApplication { 12 
13     public static void main(String[] args) { 14         SpringApplication.run(ServiceHystrixFeignApplication.class, args); 15  } 16 }

配置文件

在配置文件 application.yml 中添加

management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream

management.endpoints.web.exposure.include這個是用來暴露 endpoints 的,由於 endpoints 中會包含很多敏感信息,除了 health 和 info 兩個支持 web 訪問外,其他的默認不支持 web 訪問。詳情請看官方文檔 50. Endpoints

測試

在Hystrix-Dashboard 的主界面上輸入service-hystrix-feign對應的地址 http://localhost:9002/actuator/hystrix.stream 然后點擊 Monitor Stream 按鈕,進入頁面如果沒有請求會一直顯示 “Loading…”,這時訪問 http://localhost:9002/actuator/hystrix.stream 也是不斷的顯示“ping”,然后訪問一下 http://localhost:9002/getPoducerInfoByFeign,可以看到 Hystrix Dashboard 中出現了類似下面的效果

 

提示:如果在這個頁面看到報錯:Unable to connect to Command Metric Stream.,可以參考這個 Issue 解決

 停掉服務生產者service-producer,繼續多次訪問http://localhost:9002/getPoducerInfoByFeign,最終發現Circuit的值變成Open,此時說明斷路器已經打開

界面解讀

以上圖來說明其中各元素的具體含義:

  • 實心圓:它有顏色和大小之分,分別代表實例的監控程度和流量大小。如上圖所示,它的健康度從綠色、黃色、橙色、紅色遞減。通過該實心圓的展示,我們就可以在大量的實例中快速的發現故障實例和高壓力實例。
  • 曲線:用來記錄 2 分鍾內流量的相對變化,我們可以通過它來觀察到流量的上升和下降趨勢。
  • 其他一些數量指標如下圖所示

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM