turbine:英 [ˈtɜ:baɪn] 美 [ˈtɜ:rbaɪn] n.汽輪機;渦輪機;透平機html
在微服務架構中爲了保證程序的可用性,防止程序出錯致使網絡阻塞,出現了斷路器模型。斷路器的情況反應了一個程序的可用性和健壯性,它是一個重要指標。Hystrix Dashboard是做爲斷路器狀態的一個組件,提供了數據監控和友好的圖形化界面。java
本文咱們將從兩個方面來看Hystrix儀表盤的使用,一方面是監控單體應用,另外一方面則整合Turbine,對集羣進行監控。git
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庫添加到項目中:github
dependencies { compile( ... 'com.netflix.hystrix:hystrix-metrics-event-stream:1.3.9', ... ) }
而後,在web.xml中配置一個Servlet來獲取Hystrix提供的數據:web
<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"......
系統會不斷刷新以獲取實時的數據。spring
從上面的輸出能夠看到,這樣的純字符輸出可讀性實在太差,運維人員很難從中就看出系統的當前狀態,因而Netflix又開發了一個開源項目(Dashboard:https://github.com/Netflix/Hystrix/wiki/Dashboard)來可視化這些數據,幫助運維人員更直觀的瞭解系統的當前狀態。apache
不論是監控單體應用仍是Turbine集羣監控,咱們都須要一個Hystrix Dashboard,固然咱們能夠在要監控的單體應用上繼續添加功能,讓它也具有儀表盤的功能,可是這樣並不符合咱們微服務的思想,因此,Hystrix儀表盤我仍是單首創建一個新的工程專門用來作Hystrix Dashboard。OK,在Spring Cloud中建立一個Hystrix Dashboard很是簡單,以下:json
例如:在以前的工程上jar和開啓Hystrix Dashboard功能springboot
其它什麼都不須要修改,啓動後,看swagger:網絡
建立一個Spring Boot工程這個比較簡單,直接建立一個名爲hystrix-dashboard的Spring Boot工程。
本文涉及2個工程:
一個有熔斷功能的示例,前面文章《服務容錯保護斷路器Hystrix之一:入門介紹》中的ribbon-consumer
一個是新建的斷路器監控(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,會打印大量的監控端點信心,以下所示:
上面的是一段json文件,單純的查看json數據,咱們很難分析出結果,因此,咱們要在Hystrix儀表盤中來查看這一段json,在hystrix儀表盤中輸入監控地址,以下:
將上面http://127.0.0.1:2250/hystrix.stream(ribbon-consumer項目的url)的url填入dashboard主頁裏的文本框內,再點擊monitor stream
結果:
OK,儀表盤已經顯示出來了,那麼儀表盤上的各項數據都是什麼意思呢?咱們來看下面一張圖,下面有文字介紹
被監控的單機服務須要有:
<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並確保引入斷路器的依賴
默認的集羣監控:經過URL http://turbine-hostname:port/turbine.stream開啓,實現對默認集羣的監控。
指定的集羣監控:經過URL http://turbine-hostname:port/turbine.stream?cluster=[clusterName]開啓,實現對clusterName的監控。
單體應用監控:經過URL http://hystrix-app:port/hystrix.stream開啓,實現對某個具體的服務監控。
Delay:採集時間間隔默認爲2000毫秒。
Title:監控圖上面的標題,自定義就好。
實心圓:
實心圓經過顏色表示健康狀態,健康度從綠色、黃色、橙色、紅色遞減。
實心圓大小,表示流量。
曲線:記錄2分鐘內流量的相對變化,表現出流量的升降趨勢。
見下一節