天天看點

微服務之Hystrix Dashboard監控平台的搭建

Hystrix Dashboard是什麼:

Hystrix提供了對于微服務調用狀态的監控資訊,但是需要結合spring-boot-actuator子產品一起使用。Hystrix Dashboard是Hystrix的一個元件,Hystrix Dashboard提供一個斷路器的監控面闆,可以使我們更好的監控服務和叢集的狀态,僅僅使用Hystrix Dashboard隻能監控到單個斷路器的狀态,實際開發中還需要結合Turbine使用。

Hystrix Dashboard作用:

Hystrix Dashboard主要用來實時監控Hystrix的各項名額資訊。通過Hystrix Dashboard回報的實時資訊,可以幫助我們快速發現系統中存在的問題。

Hystrix Dashboard使用:

使用基于Hystrix的提供者通路資料庫表資料,每通路一次都會記錄是否成功以及最近10s錯誤百分比、逾時數、熔斷數、線程拒絕數、錯誤請求數、失敗/異常數、服務請求頻率等相關資訊

Hystrix Dashboard監控平台:

 建立Hystrix Dashboard子產品

 建立application.yml配置檔案

  建立主啟動類

1. 建立名為microService-hystrix-dashboard-9001的Maven項目

 2. 配置pom.xml

<dependencies>
            <!--依賴關系-->
            <dependency>
                <groupId>cn.zf</groupId>
                <artifactId>microService-api</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</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-netflix-hystrix-dashboard</artifactId>
            </dependency>
        </dependencies>      

  3. 建立application.yml檔案

server:
      port: 9001      

4. 建立主啟動類

package cn.zf.springcloud;
     
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
     
    @EnableHystrixDashboard
    @SpringBootApplication
    public class HystrixDashBoard_9001 {
        public static void main(String[] args) {
            SpringApplication.run(HystrixDashBoard_9001.class,args);
        }
    }      

5. 測試

微服務之Hystrix Dashboard監控平台的搭建

首頁中并沒有具體的監控資訊,從頁面上的内容可以知道,Hystrix Dashboard共支援三種不同的監控方式:

    預設的叢集監控: http://turbine-hostname:port/turbine.stream

    指定的叢集監控: http://turbine-hostname:port/turbine.stream?cluster=[clusterName]

    單體應用的監控: http://hystrix-app:port/actuator/hystrix.stream

頁面上面的幾個參數局域

   最上面的輸入框: 輸入上面所說的三種監控方式的位址,用于通路具體的監控資訊頁面。

    Delay: 該參數用來控制伺服器上輪詢監控資訊的延遲時間,預設2000毫秒。

    Title: 該參數對應頭部标題Hystrix Stream之後的内容,預設會使用具體監控執行個體的Url。

Hystrix Dashboard監控平台的搭建:

    監控平台:使用上面的監控平台

    被監控方:服務提供者。這裡接微服務之Hystrix熔斷器那期代碼

在microService-provider-hystrix-8002項目中的pom.xml添加新的依賴:

<dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <!--添加熔斷器依賴-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            </dependency>      

在application.yml檔案中添加暴露點:

#  在被監控的服務上添加暴露點
    management:
      endpoints:
        web:
          exposure:
            include: hystrix.stream
    #       include: '*'   #'*'代表開放所有端點。      

啟動eureka注冊中心、服務提供者、服務注冊者,浏覽器通路http://localhost:8002/actuator/hystrix.stream出現以下頁面,因為監控的執行個體本身還沒有調用任何服務,是以監控端點也沒記錄任何資訊:

通路一下服務:

微服務之Hystrix Dashboard監控平台的搭建

通路後回到剛才的頁面點選重新整理,有資料傳回,埋點生效。

從浏覽器中的資訊可以看出這些資訊是剛剛請求微服務時所記錄的監控資訊,但是直接去看這些資訊肯定是不友好的,是以Hystrix Dashboard就派上用場了。

微服務之Hystrix Dashboard監控平台的搭建