咱們提到斷路器是根據一段時間窗內的請求狀況來判斷並操做斷路器的打開和關閉狀態的。而這些請求狀況的指標信息都是HystrixCommand和HystrixObservableCommand實例在執行過程當中記錄的重要度量信息,它們除了Hystrix斷路器實現中使用以外,對於系統運維也有很是大的幫助。這些指標信息會以「滾動時間窗」與「桶」結合的方式進行彙總,並在內存中駐留一段時間,以供內部或外部進行查詢使用,Hystrix Dashboard就是這些指標內容的消費者之一。java
下面咱們基於以前的示例來結合Hystrix Dashboard實現Hystrix指標數據的可視化面板,這裏咱們將用到下以前實現的幾個應用,包括:web
eureka-server:服務註冊中心spring
eureka-client:服務提供者apache
eureka-consumer-ribbon-hystrix:使用ribbon和hystrix實現的服務消費者服務器
因爲eureka-consumer-ribbon-hystrix項目中的/consumer
接口實現使用了@HystrixCommand
修飾,因此這個接口的調用狀況會被Hystrix記錄下來,以用來給斷路器和Hystrix Dashboard使用。斷路器咱們在上一篇中已經介紹過了,下面咱們來具體說說Hystrix Dashboard的構建。網絡
在Spring Cloud中構建一個Hystrix Dashboard很是簡單,只須要下面四步:app
建立一個標準的Spring Boot工程,命名爲:hystrix-dashboard。運維
編輯pom.xml,具體依賴內容以下:maven
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Dalston.SR1</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
爲應用主類加上@EnableHystrixDashboard
,啓用Hystrix Dashboard功能。ide
@EnableHystrixDashboard @SpringCloudApplication public class HystrixDashboardApplication { public static void main(String[] args) { SpringApplication.run(HystrixDashboardApplication.class, args); } }
根據實際狀況修改application.properties
配置文件,好比:選擇一個未被佔用的端口等,此步非必須。
spring.application.name=hystrix-dashboard server.port=1301
到這裏咱們已經完成了基本配置,接下來咱們能夠啓動該應用,並訪問:http://localhost:1301/hystrix
,咱們能夠看到以下頁面:
這是Hystrix Dashboard的監控首頁,該頁面中並無具體的監控信息。從頁面的文字內容中咱們能夠知道,Hystrix Dashboard共支持三種不一樣的監控方式,依次爲:
默認的集羣監控:經過URLhttp://turbine-hostname:port/turbine.stream
開啓,實現對默認集羣的監控。
指定的集羣監控:經過URLhttp://turbine-hostname:port/turbine.stream?cluster=[clusterName]
開啓,實現對clusterName集羣的監控。
單體應用的監控:經過URLhttp://hystrix-app:port/hystrix.stream
開啓,實現對具體某個服務實例的監控。
前二者都對集羣的監控,須要整合Turbine才能實現,這部份內容咱們將在下一篇中作詳細介紹。在本節中,咱們主要實現對單個服務實例的監控,因此這裏咱們先來實現單個服務實例的監控。
既然Hystrix Dashboard監控單實例節點須要經過訪問實例的/hystrix.stream
接口來實現,天然咱們須要爲服務實例添加這個端點,而添加該功能的步驟也一樣簡單,只須要下面兩步:
在服務實例pom.xml
中的dependencies
節點中新增spring-boot-starter-actuator
監控模塊以開啓監控相關的端點,並確保已經引入斷路器的依賴spring-cloud-starter-hystrix
:
<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
或@EnableHystrix
註解,開啓了斷路器功能。
到這裏已經完成了全部的配置,咱們能夠在Hystrix Dashboard的首頁輸入http://localhost:2101/hystrix.stream
,已啓動對「eureka-consumer-ribbon-hystrix」的監控,點擊「Monitor Stream」按鈕,此時咱們能夠看到以下頁面:
在對該頁面介紹前,咱們先看看在首頁中咱們尚未介紹的兩外兩個參數:
Delay
:該參數用來控制服務器上輪詢監控信息的延遲時間,默認爲2000毫秒,咱們能夠經過配置該屬性來下降客戶端的網絡和CPU消耗。
Title
:該參數對應了上圖頭部標題Hystrix Stream以後的內容,默認會使用具體監控實例的URL,咱們能夠經過配置該信息來展現更合適的標題。
回到監控頁面,咱們來詳細說說其中各元素的具體含義:
咱們能夠在監控信息的左上部分找到兩個重要的圖形信息:一個實心圓和一條曲線。
實心圓:共有兩種含義。它經過顏色的變化表明了實例的健康程度,以下圖所示,它的健康度從綠色、黃色、橙色、紅色遞減。該實心圓除了顏色的變化以外,它的大小也會根據實例的請求流量發生變化,流量越大該實心圓就越大。因此經過該實心圓的展現,咱們就能夠在大量的實例中快速的發現故障實例和高壓力實例。
曲線:用來記錄2分鐘內流量的相對變化,咱們能夠經過它來觀察到流量的上升和降低趨勢。
其餘一些數量指標以下圖所示:
以上例子只能監控一個,要同時監控多個流怎麼辦? 答案是, 能夠單獨作一個Turbine服務,專門監控全部斷路器狀態,從而掌握整個系統中全部微服務的狀態。下面咱們就來建立一個Turbine服務,來監控咱們以前作的Feign服務和Ribbon服務
1). 建立一個maven工程, 在pox.xml添加如下依賴
1 <dependency>
2 <groupId>org.springframework.cloud</groupId>
3 <artifactId>spring-cloud-starter-turbine</artifactId>
4 </dependency>
5 <dependency>
6 <groupId>org.springframework.cloud</groupId>
7 <artifactId>spring-cloud-netflix-turbine</artifactId>
8 </dependency>
9 <dependency>
10 <groupId>org.springframework.boot</groupId>
11 <artifactId>spring-boot-starter-actuator</artifactId>
12 </dependency>
13 <dependency>
14 <groupId>org.springframework.cloud</groupId>
15 <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
16 </dependency>
整個個pox.xml文件以下:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6 <groupId>cm.chry</groupId>
7 <artifactId>spring.helloworld.turbine.service</artifactId>
8 <version>0.0.1-SNAPSHOT</version>
9 <name>spring.helloworld.turbine.service</name>
10 <description>Turbine service demo</description>
11 <parent>
12 <groupId>org.springframework.boot</groupId>
13 <artifactId>spring-boot-starter-parent</artifactId>
14 <version>1.5.3.RELEASE</version>
15 <relativePath/> <!-- lookup parent from repository -->
16 </parent>
17
18 <properties>
19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21 <java.version>1.8</java.version>
22 </properties>
23
24 <dependencies>
25 <dependency>
26 <groupId>org.springframework.cloud</groupId>
27 <artifactId>spring-cloud-starter-eureka</artifactId>
28 </dependency>
29 <dependency>
30 <groupId>org.springframework.boot</groupId>
31 <artifactId>spring-boot-starter-web</artifactId>
32 </dependency>
33 <dependency>
34 <groupId>org.springframework.cloud</groupId>
35 <artifactId>spring-cloud-starter-turbine</artifactId>
36 </dependency>
37 <dependency>
38 <groupId>org.springframework.cloud</groupId>
39 <artifactId>spring-cloud-netflix-turbine</artifactId>
40 </dependency>
41 <dependency>
42 <groupId>org.springframework.boot</groupId>
43 <artifactId>spring-boot-starter-actuator</artifactId>
44 </dependency>
45 <dependency>
46 <groupId>org.springframework.cloud</groupId>
47 <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
48 </dependency>
49 <dependency>
50 <groupId>org.springframework.boot</groupId>
51 <artifactId>spring-boot-starter-test</artifactId>
52 <scope>test</scope>
53 </dependency>
54 </dependencies>
55
56 <dependencyManagement>
57 <dependencies>
58 <dependency>
59 <groupId>org.springframework.cloud</groupId>
60 <artifactId>spring-cloud-dependencies</artifactId>
61 <version>Dalston.RC1</version>
62 <type>pom</type>
63 <scope>import</scope>
64 </dependency>
65 </dependencies>
66 </dependencyManagement>
67
68 <build>
69 <plugins>
70 <plugin>
71 <groupId>org.springframework.boot</groupId>
72 <artifactId>spring-boot-maven-plugin</artifactId>
73 </plugin>
74 </plugins>
75 </build>
76
77 <repositories>
78 <repository>
79 <id>spring-milestones</id>
80 <name>Spring Milestones</name>
81 <url>https://repo.spring.io/milestone</url>
82 <snapshots>
83 <enabled>false</enabled>
84 </snapshots>
85 </repository>
86 </repositories>
87 </project>
2). 建立Turbine Dashboard啓動類:
用@EnableHystrixDashboard和@EnableTurbine修飾主類, 分別用於支持Hystrix Dashboard和Turbine
1 package spring.helloworld.turbine.service;
2
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
6 import org.springframework.cloud.netflix.turbine.EnableTurbine;
7
8 @SpringBootApplication
9 @EnableHystrixDashboard
10 @EnableTurbine
11 public class DashboardApplication {
12
13 public static void main(String[] args) {
14 SpringApplication.run(DashboardApplication.class, args);
15 }
16 }
3). 在application.yml中配置turbine參數
1 eureka:
2 client:
3 serviceUrl:
4 defaultZone: http://localhost:8761/eureka/
5 server:
6 port: 8903
7 spring:
8 application:
9 name: hystrix-dashboard-turbine
10 turbine:
11 appConfig: service-feign, service-ribbon
12 aggregator:
13 clusterConfig: default
14 clusterNameExpression: new String("default")
turbine.appConfig定義了要監控的服務,這裏是咱們在前面章節建立的service-feign和sercice-ribbon; aggregator.clusterConfig定義了聚合方式, 此處爲default.
turbine.appConfig
:配置Eureka中的serviceId列表,代表監控哪些服務
turbine.aggregator.clusterConfig
:指定聚合哪些集羣,多個使用」,」分割,默認爲default。可以使用http://.../turbine.stream?cluster={clusterConfig之一}
訪問
turbine.clusterNameExpression
:指定集羣名稱,能夠是三種類型的值
- 默認表達式爲appName;此時turbine.aggregator.clusterConfig
須要配置想要監控的應用名稱;
- 當爲default時,turbine.aggregator.clusterConfig
能夠不寫,由於默認就是default;
- 當爲metadata[‘cluster’]時,假設想要監控的應用配置了eureka.instance.metadata-map.cluster: ABC
,則須要配置,同時turbine.aggregator.clusterConfig: ABC
4). 依次啓動eureka服務, 2個Helloworld服務, Feign服務,ribbon服務和剛建立turbine服務。從eureka服務中咱們能夠看到
5)經過Turbine服務訪問HystrixDashborad, http:localhost:8903/hystrix
監控流的URL填http://localhost:8903/turbine.stream, 點擊monitor stream, 進入監控頁面, 隨便刷新下feign和ribbon服務(http://localhost:8902/hello和http://localhost:8901), 能夠看到監控頁面的變化。以下圖, 兩個服務的監控都會顯示在dashboard上