服務容錯保護斷路器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功能

其它什么都不需要修改,啟動后,看swagger:

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

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

本文涉及2個工程:

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

一個是新建的斷路器監控(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

 

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

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>

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

這個信息表明我們的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 Dashboard圖形展示

上面的是一段json文件,單純的查看json數據,我們很難分析出結果,所以,我們要在Hystrix儀表盤中來查看這一段json,在hystrix儀表盤中輸入監控地址,如下:

將上面http://127.0.0.1:2250/hystrix.stream(ribbon-consumer項目的url)的url填入dashboard主頁里的文本框內,再點擊monitor stream

 

結果:

參數詳解

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

 

五、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開啟,實現對某個具體的服務監控。

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

Delay:采集時間間隔默認為2000毫秒。
Title:監控圖上面的標題,自定義就好。

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

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

實心圓:
實心圓通過顏色表示健康狀態,健康度從綠色、黃色、橙色、紅色遞減。
實心圓大小,表示流量。
曲線:記錄2分鍾內流量的相對變化,表現出流量的升降趨勢。

6.2.2、其他指標

 

Turbine集群監控

見下一節


免責聲明!

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



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