Spring Cloud 入門教程(八): 斷路器指標數據監控Hystrix Dashboard 和 Turbine


1. Hystrix Dashboard (斷路器:hystrix 儀表盤) 

Hystrix一個很重要的功能是,可以通過HystrixCommand收集相關數據指標. Hystrix Dashboard可以很高效的現實每個斷路器的健康狀況。

1). 在Ribbon服務g和Feign服務的Maven工程的pom.xml中都加入依賴

1 <dependency>
2     <groupId>org.springframework.boot</groupId>
3     <artifactId>spring-boot-starter-actuator</artifactId>
4  </dependency>
5 <dependency>
6     <groupId>org.springframework.cloud</groupId>
7     <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
8 </dependency>

spring-boot-starter-actuator用於手機metric, 支持hystrix.stream。spring-cloud-starter-hystrix-dashboard支持dashboard的UI

2)在Spring Boot啟動類上用@EnableHystrixDashboard注解和@EnableCircuitBreaker注解。需要特別注意的是我們之前的Feign服務由於內置斷路器支持, 所以沒有@EnableCircuitBreaker注解,但要使用Dashboard則必須加,如果不加,Dashboard無法接收到來自Feign內部斷路器的監控數據,會報“Unable to connect to Command Metric Stream”錯誤

 1 @SpringBootApplication
 2 @EnableDiscoveryClient 3 @EnableFeignClients 4 @EnableCircuitBreaker 5 @EnableHystrixDashboard 6 public class ServiceFeignApplication { 7 8 public static void main(String[] args) { 9 SpringApplication.run(ServiceFeignApplication.class, args); 10  } 11 }

3)然后就可以訪問/hystrix,這個URL將dashboard指向定義在Hystrix客戶端應用中的/hystrix.stream

在dashboard中輸入服務的URL:點擊 monitor后進入監控界面,訪問我們之前創建的Ribbon服務localhost:8901/, 或者Feign服務localhost:8902/可以看到監控UI動態變化

2. 利用Turbine在一個Dashboard上監控多個流

以上例子只能監控一個,要同時監控多個流怎么辦? 答案是, 可以單獨做一個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>
pom.xml

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上

 

上一篇:Spring Cloud 入門教程(七):  斷路器

下一篇:Spring Cloud 入門教程(九): 路由網關zuul

參考: http://projects.spring.io/spring-cloud/spring-cloud.html#_circuit_breaker_hystrix_dashboard

           http://blog.csdn.net/ityouknow/article/details/72625646

 


免責聲明!

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



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