天天看點

服務容錯保護斷路器Hystrix之三:斷路器監控(Hystrix Dashboard)-單體監控

turbine:英 [ˈtɜ:baɪn] 美 [ˈtɜ:rbaɪn] n.汽輪機;渦輪機;透平機

一、Hystrix Dashboard簡介

在微服務架構中為了保證程式的可用性,防止程式出錯導緻網絡阻塞,出現了斷路器模型。斷路器的狀況反應了一個程式的可用性和健壯性,它是一個重要名額。Hystrix Dashboard是作為斷路器狀态的一個元件,提供了資料監控和友好的圖形化界面。

本文我們将從兩個方面來看Hystrix儀表盤的使用,一方面是監控單體應用,另一方面則整合Turbine,對叢集進行監控。

背景

  Hystrix除了隔離依賴服務的調用外,Hystrix還提供了近乎實時的監控,Hystrix會實時的,累加的記錄所有關于HystrixCommand的執行資訊,包括執行了每秒執行了多少請求,多少成功,多少失敗等等,更多名額請檢視:https://github.com/Netflix/Hystrix/wiki/Metrics-and-Monitoring

導出監控資料

有了這些名額,Netflix還提供了一個類庫(hystrix-metrics-event-stream:https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-metrics-event-stream)把這些名額資訊以‘text/event-stream’的格式開放給外部使用,用法非常簡單,首先,把hystrix-metrics-event-stream庫添加到項目中:

dependencies {
    compile(
             ...
            'com.netflix.hystrix:hystrix-metrics-event-stream:1.3.9',
            ...
    )
}      

然後,在web.xml中配置一個Servlet來擷取Hystrix提供的資料:

<servlet>  
        <description></description>  
        <display-name>HystrixMetricsStreamServlet</display-name>  
        <servlet-name>HystrixMetricsStreamServlet</servlet-name>  
        <servlet-class>com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet</servlet-class>  
    </servlet>  
  
    <servlet-mapping>  
        <servlet-name>HystrixMetricsStreamServlet</servlet-name>  
        <url-pattern>/hystrix.stream</url-pattern>  
    </servlet-mapping>        

配置好,重新啟動應用。通路http://hostname:port/appname/hystrix.stream, 可以看到如下的輸出:

data: {"type":"HystrixCommand","name":"Address","group":"Address","currentTime":1393154954462,"isCircuitBreakerOpen":false,"errorPercentage":0,"errorCount":0,"requestCount":0,"rollingCountCollapsedRequests"......

系統會不斷重新整理以擷取實時的資料。

從上面的輸出可以看到,這樣的純字元輸出可讀性實在太差,運維人員很難從中就看出系統的目前狀态,于是Netflix又開發了一個開源項目(Dashboard:https://github.com/Netflix/Hystrix/wiki/Dashboard)來可視化這些資料,幫助運維人員更直覺的了解系統的目前狀态。

二、監控單體應用

監控環境搭建

不管是監控單體應用還是Turbine叢集監控,我們都需要一個Hystrix Dashboard,當然我們可以在要監控的單體應用上繼續添加功能,讓它也具備儀表盤的功能,但是這樣并不符合我們微服務的思想,是以,Hystrix儀表盤我還是單獨建立一個新的工程專門用來做Hystrix Dashboard。OK,在Spring Cloud中建立一個Hystrix Dashboard非常簡單,如下:

在現有的單體應用上添加Hystrix Dashboard功能

例如:在之前的工程上jar和開啟Hystrix Dashboard功能

服務容錯保護斷路器Hystrix之三:斷路器監控(Hystrix Dashboard)-單體監控
服務容錯保護斷路器Hystrix之三:斷路器監控(Hystrix Dashboard)-單體監控

其它什麼都不需要修改,啟動後,看swagger:

服務容錯保護斷路器Hystrix之三:斷路器監控(Hystrix Dashboard)-單體監控

第一步:建立一個普通的Spring Boot工程

建立一個Spring Boot工程這個比較簡單,直接建立一個名為hystrix-dashboard的Spring Boot工程。

本文涉及2個工程:

一個有熔斷功能的示例,前面文章《服務容錯保護斷路器Hystrix之一:入門介紹》中的ribbon-consumer

一個是建立的斷路器監控(Hystrix Dashboard)工程

服務容錯保護斷路器Hystrix之三:斷路器監控(Hystrix Dashboard)-單體監控

三、開始建立斷路器監控(Hystrix Dashboard)

建立一個hystrix-dashboard的springboot工程,pom的工程檔案引入相應的依賴:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dxz.dashboard</groupId>
    <artifactId>dashboard</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>hystrix-dashboard</name>
    <description>dashboard project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>   <!--配合spring cloud版本 -->
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <!--設定字元編碼及java版本 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--增加hystrix的依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <!--增加dashboard的依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
        </dependency>
        <!--用于測試的,本例可省略 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--依賴管理,用于管理spring-cloud的依賴 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-parent</artifactId>
                <version>Brixton.SR3</version>   <!--官網為Angel.SR4版本,但是我使用的時候總是報錯 -->
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <!--使用該插件打包 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>      

在程式的入口HystrixDashboardApplication類,加上@EnableHystrixDashboard注解開啟斷路器,開啟HystrixDashboard

package com.dxz.dashboard;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@EnableHystrixDashboard
@SpringBootApplication
public class HystrixDashboardApplication {

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

配置端口資訊application.properties

spring.application.name=hystrix-dashboard
server.port=2259      

運作程式,通路http://127.0.0.1:2259/hystrix

服務容錯保護斷路器Hystrix之三:斷路器監控(Hystrix Dashboard)-單體監控

三個參數的含義我已在圖中标注出來了。

OK,現在我們的儀表盤工程已經建立成功了,但是還不能用來監控某一個服務,要監控某一個服務,需要該服務提供一個/hystrix.stream接口,so,我們需要對我們的服務消費者工程稍加改造。

改造要監控的服務

我們來改造一下我們的服務消費者工程,改造方式很簡單,兩個步驟就搞定,首先在pom.xml檔案中添加如下依賴:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>      
服務容錯保護斷路器Hystrix之三:斷路器監控(Hystrix Dashboard)-單體監控
服務容錯保護斷路器Hystrix之三:斷路器監控(Hystrix Dashboard)-單體監控

然後在服務消費者工程的入口類上添加@EnableCircuitBreaker注解,表示開啟斷路器功能。此時,我們再來啟動我們的eureka-server、provider、和consumer工程,在consumer工程的啟動日志中,我們可以看到如下資訊:

服務容錯保護斷路器Hystrix之三:斷路器監控(Hystrix Dashboard)-單體監控

這個資訊表明我們的consumer工程目前已經具備了/hystrix.stream接口,我們可以直接通路這個接口了。但是這裡有一個細節需要小夥伴們注意:要通路/hystrix.stream接口,得先通路consumer工程中的任意一個其他接口,否則如果直接通路/hystrix.stream接口的話,會列印出一連串的ping: ping: ...。 OK,我先通路consumer中的任意一個其他接口,然後在通路/hystrix.stream接口,通路位址如下:http://localhost:9000/hystrix...,通路結果如下:

再啟動ribbon-consumer(eureka-server,computer-service),

通路ribbon-consumer的http://127.0.0.1:2250/hystrix.stream,會列印大量的監控端點信心,如下所示:

服務容錯保護斷路器Hystrix之三:斷路器監控(Hystrix Dashboard)-單體監控

四、Hystrix Dashboard圖形展示

上面的是一段json檔案,單純的檢視json資料,我們很難分析出結果,是以,我們要在Hystrix儀表盤中來檢視這一段json,在hystrix儀表盤中輸入監控位址,如下:

将上面http://127.0.0.1:2250/hystrix.stream(ribbon-consumer項目的url)的url填入dashboard首頁裡的文本框内,再點選monitor stream

服務容錯保護斷路器Hystrix之三:斷路器監控(Hystrix Dashboard)-單體監控

結果:

服務容錯保護斷路器Hystrix之三:斷路器監控(Hystrix Dashboard)-單體監控

參數詳解

OK,儀表盤已經顯示出來了,那麼儀表盤上的各項資料都是什麼意思呢?我們來看下面一張圖,下面有文字介紹

服務容錯保護斷路器Hystrix之三:斷路器監控(Hystrix Dashboard)-單體監控

五、Hystrix Dashboard注意點

被監控的單機服務需要有:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>      
spring-boot-starter-actuator監控子產品以開啟監控相關的斷點      
spring-cloud-starter-hystrix并確定引入斷路器的依賴      

六、Hystrix Dashboard功能介紹

 6.1、在首頁中我們可以知道有3種監控方式:

預設的叢集監控:通過URL http://turbine-hostname:port/turbine.stream開啟,實作對預設叢集的監控。

指定的叢集監控:通過URL http://turbine-hostname:port/turbine.stream?cluster=[clusterName]開啟,實作對clusterName的監控。

單體應用監控:通過URL http://hystrix-app:port/hystrix.stream開啟,實作對某個具體的服務監控。

服務容錯保護斷路器Hystrix之三:斷路器監控(Hystrix Dashboard)-單體監控

 6.2、首頁中還有2個參數:

Delay:采集時間間隔預設為2000毫秒。

Title:監控圖上面的标題,自定義就好。

 6.2、監控頁面各元素的具體意義:

6.2.1、一個實心圓和一條曲線

實心圓:

實心圓通過顔色表示健康狀态,健康度從綠色、黃色、橙色、紅色遞減。

實心圓大小,表示流量。

曲線:記錄2分鐘内流量的相對變化,表現出流量的升降趨勢。

6.2.2、其他名額

Turbine叢集監控