SpringCloud(六)Hystrix儀表盤Dashboard

SpringCloud(六)Hystrix儀表盤Dashboard

SpringCloud提供了Hystrix-Dashboard,可以對每一個HystrixCommand進行監控,直觀的顯示每一個熔斷器的健康狀態。

引入Hystrix Dashboard

在此前項目的基礎上我們新建一個服務dashboard service用作監控服務。
Hystrix Dashboard需要在pom.xml文件中引入

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>

在我們的啓動類上增加@EnableHystrixDashboard註解

/**
 * 監控服務啓動類
 */
@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
public class DashboardApplicationStarter {

    public static void main(String[] args) {
        SpringApplication.run(DashboardApplicationStarter.class, args);
    }
}

這樣一個Hystrix Dashboard服務就已經可以使用了。我們通過瀏覽器直接訪問儀表盤的首頁(http://localhost:8102/hystrix)。
Hystrix Dashboard首頁
我們可以使用儀表盤監控單個服務,指定集羣或者默認集羣的狀態。

監控單個服務

在此處我們只監控一個服務的Hystrix Command,即Single Hystrix App,我們輸入web-app-service服務的hystrix流信息路徑http://localhost:8101/hystrix.stream。點擊監控按鈕跳轉到監控頁面。
監控首頁
此時由於web-app-service的接口還沒有調用過,沒有Hystrix Command的統計信息,所以監控頁面是空白的,我們多次調用web-app-service/user/command/exception接口,隨機觸發異常。
調用接口後儀表盤顯示數據

集成Turbine監控多個服務

上面的方法只能監控到單個服務的接口調用健康狀態,但是在一個分佈式系統中,我們更關心的是全部或者一系列系統的狀況。Turbine可以將我們關心的應用通過Eureka和自身的/hystrix.stream接口組合在一起成爲/turbine.stream,通過儀表盤展示出來。
下面展示如何集成和配置Turbine。
在pom.xml文件中引入Turbine依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>

application.yaml中進行Turbine設置

turbine:
  app-config: web-app-service,sms-service
  cluster-name-expression: "'default'"

trubine.app-config指定的是Eureka中註冊的服務名,Turbine會把這些服務的hystrix.stream聚合起來。
現在我們把監控的流路徑改爲http://localhost:8102/turbine.stream?cluster=default(clusterName爲default時,可以省略該參數即http://localhost:8102/turbine.stream)。

Hystrix儀表盤圖表說明

左側圈的顏色和大小分別表示健康狀態和流量大小,其顏色狀態從健康到熔斷狀態依次變化爲綠色->黃色->橙色->紅色。裏面的折線表示流量變化曲線。
右側的彩色數字在左上角有圖例,分別對應Success | Short-Circuited | Bad Request | Timeout | Rejected | Failure | Error %
圖例
詳細說明貼一張官方Wiki的圖片:
在這裏插入圖片描述


相關代碼

SpringCloudDemo-HystrixDashboard


參考
hystrix-dashboard wiki參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章