Hystrix儀表板介紹
Hystrix儀表板使您可以實時監視Hystrix指標。
可以使用此儀表板時,通過減少發現和恢復運營事件所需的時間來改善其運營。大多數生產事件的持續時間(由於Hystrix而已經不那么頻繁了)變得更短了,並且影響減小了,這是由於Hystrix儀表板提供了對系統行為的實時洞察力。
Hystrix提供了對於微服務調用狀態的監控信息,但是需要結合spring-boot-actuator模塊一起使用。Hystrix Dashboard是Hystrix的一個組件,Hystrix Dashboard提供一個斷路器的監控面板,可以使我們更好的監控服務和集群的狀態
Hystrix儀表板使用
搭建Hystrix儀表板項目
1、新建項目(test-springcloud-hystrix7979),引入依賴:
1 <!-- hystrix-dashboard --> 2 <dependency> 3 <groupId>org.springframework.cloud</groupId> 4 <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> 5 </dependency>
完整pom文件如下:

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 <parent> 6 <artifactId>test-springcloud</artifactId> 7 <groupId>com.test</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>test-springcloud-hystrix7979</artifactId> 13 14 <dependencies> 15 16 <!-- hystrix-dashboard --> 17 <dependency> 18 <groupId>org.springframework.cloud</groupId> 19 <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> 20 </dependency> 21 22 <!-- spring boot --> 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-web</artifactId> 26 </dependency> 27 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-actuator</artifactId> 31 </dependency> 32 33 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-devtools</artifactId> 37 <scope>runtime</scope> 38 <optional>true</optional> 39 </dependency> 40 41 <dependency> 42 <groupId>org.projectlombok</groupId> 43 <artifactId>lombok</artifactId> 44 <optional>true</optional> 45 </dependency> 46 <dependency> 47 <groupId>org.springframework.boot</groupId> 48 <artifactId>spring-boot-starter-test</artifactId> 49 <scope>test</scope> 50 </dependency> 51 52 </dependencies> 53 54 <build> 55 <finalName>test-springcloud-hystrix7979</finalName> 56 </build> 57 58 </project>
2、編寫application.yml配置文件
1 # 端口 2 server: 3 port: 7979
3、編寫主啟動類,並使用@EnableHystrixDashboard注解開啟HystrixDashboard
1 // 開啟HystrixDashboard 2 @EnableHystrixDashboard 3 @SpringBootApplication 4 public class HystrixMain7979 { 5 public static void main(String[] args) { 6 SpringApplication.run(HystrixMain7979.class, args); 7 } 8 }
4、啟動項目
訪問地址:http://localhost:7979/hystrix
到此Hystrix儀表板項目,搭建完成
監控Hystrix服務項目
1、使用上一章的項目,確保在Hystrix服務項目(test-springcloud-provider-payment8008)中有依賴如下:
1 <!-- hystrix --> 2 <dependency> 3 <groupId>org.springframework.cloud</groupId> 4 <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> 5 </dependency> 6 7 <dependency> 8 <groupId>org.springframework.boot</groupId> 9 <artifactId>spring-boot-starter-actuator</artifactId> 10 </dependency>
2、在配置文件中新增以下內容,啟動“hystrix.stream”端口
1 management: 2 endpoints: 3 web: 4 exposure: 5 include: health,info,hystrix.stream
3、啟動項目測試
1)訪問地址:http://localhost:8008/actuator/hystrix.stream,確保hystrix.stream端口開啟
2)在Hystrix儀表板輸入監控地址:http://localhost:8008/actuator/hystrix.stream
3)點擊Monitor Stream,進入如下界面,使用JMeter請求Hystrix的服務:
說明:
圓圈:健康顏色,從綠色、黃色、橙色、紅色遞減
圓圈:流量越大改實心圓越大
顏色數字:對於請求處理各個結果的統計值
Turbine服務
以上例子只能監控一個,要同時監控多個流,就需要做一個Turbine服務,專門監控所有斷路器狀態,從而掌握整個系統中所有微服務的狀態。
通過Turbine來匯集監控信息,並將聚合后的信息提供給Hystrix Dashboard來集中展示和監控。
項目架構圖如下:
1、在上面實例的基礎上,在新建一個Hystrix服務項目(test-springcloud-provider-payment8009),
保證與Hystrix服務項目(test-springcloud-provider-payment8008)相同,且服務的“hystrix.stream”端口,是啟用的
2、新建項目Turbine服務(springcloud-hystrix-turbine7980),引入Turbine依賴
1 <!-- hystrix-turbine --> 2 <dependency> 3 <groupId>org.springframework.cloud</groupId> 4 <artifactId>spring-cloud-starter-netflix-turbine</artifactId> 5 </dependency>
而Turbine依賴,集成了Eureka
完整POM如下:

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 <parent> 6 <artifactId>test-springcloud</artifactId> 7 <groupId>com.test</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>springcloud-hystrix-turbine7980</artifactId> 13 14 <dependencies> 15 16 <!-- hystrix-turbine --> 17 <dependency> 18 <groupId>org.springframework.cloud</groupId> 19 <artifactId>spring-cloud-starter-netflix-turbine</artifactId> 20 </dependency> 21 22 <!-- spring boot --> 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-web</artifactId> 26 </dependency> 27 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-actuator</artifactId> 31 </dependency> 32 33 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-devtools</artifactId> 37 <scope>runtime</scope> 38 <optional>true</optional> 39 </dependency> 40 41 <dependency> 42 <groupId>org.projectlombok</groupId> 43 <artifactId>lombok</artifactId> 44 <optional>true</optional> 45 </dependency> 46 <dependency> 47 <groupId>org.springframework.boot</groupId> 48 <artifactId>spring-boot-starter-test</artifactId> 49 <scope>test</scope> 50 </dependency> 51 52 </dependencies> 53 54 </project>
3、編輯配置文件application.yml
1 # 端口 2 server: 3 port: 7980 4 5 spring: 6 application: 7 name: hystrix-turbine 8 9 eureka: 10 client: 11 register-with-eureka: true 12 fetch-registry: true 13 service-url: 14 defaultZone: http://localhost:8761/eureka 15 16 turbine: 17 # 配置Eureka中的serviceId列表,表明監控哪些服務,多個用逗號隔開 18 app-config: cloud-payment-service 19 aggregator: 20 cluster-config: default 21 cluster-name-expression: "'default'"
4、編寫啟動類,並使用注解@EnableTurbine,啟用Turbine服務
1 // 啟用Turbine服務 2 @EnableTurbine 3 @SpringBootApplication 4 public class Turbine7980 { 5 public static void main(String[] args) { 6 SpringApplication.run(Turbine7980.class, args); 7 } 8 }
5、測試
1)啟動注冊中心,啟動Hystrix服務,啟動Turbine服務,啟動Hystrix-DashBorad
2)訪問地址:http://localhost:8008/actuator/hystrix.stream
確保Hystrix服務的hystrix.stream 的端口啟動
3)訪問地址:http://localhost:7980/turbine.stream,查看Turbine服務是否正常
4)打開Hystrix儀表板監控地址:http://localhost:7979/hystrix
5)在Hystrix儀表板監控輸入Turbine服務地址:http://localhost:7980/turbine.stream
6)點擊監控流,並使用http請求hystrix服務中的接口,效果如下