天天看點

SpringCloud進擊 | 五深入:斷路器監控(Hystrix Dashboard)【Finchley版本】1.前言2.準備3.進擊4.測試5.總結6.系列

1.前言

在 上一篇 Hystrix 的介紹中,我們提到斷路器是根據一段時間窗内的請求情況來判斷并操作斷路器的打開和關閉狀态的。而這些請求情況的名額資訊都是 HystrixCommand 和 HystrixObservableCommand 執行個體在執行過程中記錄的重要度量資訊,它們除了 Hystrix 斷路器實作中使用之外,對于系統運維也有非常大的幫助。

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

Netflix 通過 hystrix-metrics-event-stream 項目實作了對以上名額的監控。Spring Cloud 也提供了 Hystrix Dashboard 的整合,對監控内容轉化成可視化界面,Hystrix Dashboard Wiki 上詳細說明了圖上每個名額的含義。

2.準備

  1. 一個服務注冊中心:wei-eureka-server,端口 8090(無需修改,正常啟動)
  2. 一個服務提供者:wei-service-provider,端口 8010(無需修改,正常啟動)
  3. 一個服務消費者:wei-consumer-ribbon,端口 8026(修改對象)

以下實驗都是對 上一節 學習到的成果的延用與疊代(隻需要配置 pom 依賴和 application.yml,再添加@EnableHystrixDashboard注解 就OK)。

3.進擊

3.1.服務消費者改造

改造服務消息者:wei-consumer-ribbon。

3.1.1.POM依賴改造

在 pom.xml 中引入以下必要的依賴:

<!--Hystrix起步依賴,在Ribbon使用斷路器-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <!--Hystrix 監控面闆-->
        <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>
           

3.1.2.配置檔案改造

server:
  port: 8026
spring:
  application:
    name: wei-consumer-ribbon    # 指定進行服務注冊時該服務的名稱,服務與服務之間互相調用一般都是根據這個name
  sleuth:
    web:
      client:
        enabled: true
    sampler:
      probability: 1.0                   # 将采樣比例設定為1.0,也就是全部都需要。預設是0.1
  zipkin:
    base-url: http://localhost:9411/     # 指定了 Zipkin 伺服器的位址
#    base-url: http://localhost:94111/     # 指向錯誤 Zipkin 伺服器的位址,驗證RabbitMQ
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8090/eureka/    # 指定進行服務注冊的位址
management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream     # 暴露 endpoints
           

在配置檔案 application.yml 中添加 management.endpoints.web.exposure.include=hystrix.stream, 這個是用來暴露 endpoints 的。由于 endpoints 中會包含很多敏感資訊,除了 health 和 info 兩個支援 web 通路外,其他的預設不支援 web 通路。

3.1.3.啟動類改造

在啟動類上面引入注解 @EnableHystrixDashboard,啟用 Hystrix Dashboard 斷路器監控面闆功能。

package com.wei;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * 注解@EnableHystrix,開啟Hystrix熔斷功能
 * 注解@EnableHystrixDashboard,開啟建立 Hystrix Dashboard 斷路器監控面闆
 */
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableHystrixDashboard
public class WeiConsumerRibbonApplication {

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

    /**
     * 注解@Bean,向程式注入一個Bean
     * 注解@LoadBalanced,開啟RestTemplate的負載均衡功能
     * 初始化RestTemplate,用來發起 REST 請求
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
           

好了,以上,對于斷路器監控面闆的改造就全部完工。

啟動。

4.測試

确認已經正常啟動準備工作中的 服務注冊中心、服務提供者、服務消費者。

1)斷路器面闆首頁

浏覽器請求URL:http://localhost:8026/hystrix,會看到如下界面:

SpringCloud進擊 | 五深入:斷路器監控(Hystrix Dashboard)【Finchley版本】1.前言2.準備3.進擊4.測試5.總結6.系列

【斷路器監控面闆】

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

  • 預設的叢集監控:通過 URL:http://turbine-hostname:port/turbine.stream 開啟,實作對預設叢集的監控
  • 指定的叢集監控:通過 URL:http://turbine-hostname:port/turbine.stream?cluster=[clusterName] 開啟,實作對 clusterName 叢集的監控
  • 單體應用的監控: 通過 URL:http://hystrix-app:port/actuator/hystrix.stream 開啟 ,實作對具體某個服務執行個體的監控(Actuator 2.x 以後 endpoints 全部在/actuator下)

前兩者都是對叢集的監控,需要整合 Turbine 才能實作。這一節我們先實作對單體應用的監控,這裡的單體應用就使用我們之前在 淺出第四節 使用 Ribbon 和 Hystrix 實作的服務消費者(wei-consumer-ribbon)。

頁面上的另外兩個參數:

  • Delay:控制伺服器上輪詢監控資訊的延遲時間,預設為 2000 毫秒,可以通過配置該屬性來降低用戶端的網絡和 CPU 消耗。
  • Title:該參數可以展示合适的标題。

2)監控面闆

URL監控輸入欄輸入 http://localhost:8026/actuator/hystrix.stream,然後點選 Monitor Stream 按鈕,進入監控頁面。

此為錯誤URL,Hystrix會提示連接配接失敗:Unable to connect to Command Metric Stream.

因為Actuator 2.x 以後 endpoints 全部在/actuator下,這裡需要確定 pom 檔案有依賴 spring-boot-starter-actuator,application.yml 檔案有 endpoints 被暴露設定:management.endpoints.web.exposure.include=hystrix.stream

另外,還要确認一下監控的 URL 字尾是否為 /actuator/hystrix.stream

SpringCloud進擊 | 五深入:斷路器監控(Hystrix Dashboard)【Finchley版本】1.前言2.準備3.進擊4.測試5.總結6.系列

【斷路器監控面闆 - 連結失敗畫面】

URL監控輸入欄輸入 http://localhost:8026/actuator/hystrix.stream,然後點選 Monitor Stream 按鈕,進入監控頁面。

一直顯示 Loading ...,等了一會兒還是一直顯示 Loading ...

SpringCloud進擊 | 五深入:斷路器監控(Hystrix Dashboard)【Finchley版本】1.前言2.準備3.進擊4.測試5.總結6.系列

【斷路器監控面闆 - Loading畫面】

對的,在此之前,如果你還沒有發過請求,監控面闆則會一直顯示 “Loading…”

這時,去浏覽器通路URL:http://localhost:8026/actuator/hystrix.stream,會得到 Hystrix 不斷發出的 ping 請求。

SpringCloud進擊 | 五深入:斷路器監控(Hystrix Dashboard)【Finchley版本】1.前言2.準備3.進擊4.測試5.總結6.系列

【斷路器監控面闆 - hystrix.stream】

3)接口驗證

浏覽器多次請求URL:http://localhost:8026/demo/info?name=tester

傳回結果:

Hi,tester,我是服務,我被調用了,服務名為:wei-service-provider,端口為:8010[Ribbon + REST]

發完請求,再看 Hystrix Dashboard,監控面闆展示如下:

SpringCloud進擊 | 五深入:斷路器監控(Hystrix Dashboard)【Finchley版本】1.前言2.準備3.進擊4.測試5.總結6.系列

【斷路器監控面闆 - 單應用監控】

可以看到監控面闆中 Thread Pools 中隻有一個執行個體,即單應用的斷路器監控。

5)界面解讀

SpringCloud進擊 | 五深入:斷路器監控(Hystrix Dashboard)【Finchley版本】1.前言2.準備3.進擊4.測試5.總結6.系列

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

  • 實心圓:它有顔色和大小之分,分别代表執行個體的監控程度和流量大小。如上圖所示,它的健康度從綠色、黃色、橙色、紅色遞減。通過該實心圓的展示,我們就可以在大量的執行個體中快速的發現故障執行個體和高壓力執行個體。
  • 曲線:用來記錄 2 分鐘内流量的相對變化,我們可以通過它來觀察到流量的上升和下降趨勢。
  • 其他一些數量名額如下圖所示
SpringCloud進擊 | 五深入:斷路器監控(Hystrix Dashboard)【Finchley版本】1.前言2.準備3.進擊4.測試5.總結6.系列

【名額元素說明】

至此,單個應用的熔斷監控開發并驗證完成。

5.總結

pom.xml 檔案完整依賴如下:

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
		<!--用戶端負載均衡-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <!--Hystrix起步依賴,在Ribbon使用斷路器-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <!--Hystrix 監控面闆-->
        <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>

        <!--分布式服務調用鍊路跟蹤實作的核心依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>

        <!--Spring Cloud sleuth + Zipkin + RabbitMQ 整合-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
           

6.系列

SpringCloud進擊 | 一淺出:服務注冊與發現(Eureka)【Finchley版本】

SpringCloud進擊 | 二淺出:服務消費者(Ribbon+REST)【Finchley版本】

SpringCloud進擊 | 三淺出:服務消費者(Feign)【Finchley版本】

SpringCloud進擊 | 四淺出:斷路器與容錯(Hystrix)【Finchley版本】

SpringCloud進擊 | 五淺出:服務網關 - 路由(Zuul Router)【Finchley版本】

SpringCloud進擊 | 六淺出:服務網關 - 過濾器(Zuul Filter)【Finchley版本】

SpringCloud進擊 | 七淺出:配置中心(Git配置與更新)【Finchley版本】

SpringCloud進擊 | 一深入:配置中心(服務化與高可用)【Finchley版本】

SpringCloud進擊 | 二深入:配置中心(消息總線)【Finchley版本】

SpringCloud進擊 | 三深入:服務鍊路跟蹤(Spring Cloud Sleuth)【Finchley版本】

SpringCloud進擊 | 四深入:服務鍊路跟蹤(Sleuth+Zipkin+RabbitMQ整合)【Finchley版本】

SpringCloud進擊 | 五深入:斷路器監控(Hystrix Dashboard)【Finchley版本】

SpringCloud進擊 | 六深入:斷路器聚合監控(Hystrix Turbine)【Finchley版本】

SpringCloud進擊 | 七深入:高可用的服務注冊中心【Finchley版本】