本文源碼:GitHub·點這裡 || GitEE·點這裡
寫在前面,閱讀本文前,你需要了解熔斷器相關内容
SpringCloud微服務:Hystrix元件,實作服務熔斷
一、聚合監控簡介
1、Dashboard元件
微服務架構中為了保證程式的可用性,防止程式出錯導緻網絡阻塞,出現了斷路器模型。斷路器的狀況反應程式的可用性和健壯性,它是一個重要名額。HystrixDashboard是作為斷路器狀态的一個元件,提供了資料監控和直覺的圖形化界面。
2、Turbine元件
Hystrix Dashboard元件監控服務的熔斷情況時,每個服務都有圖形界面,當微服務數量很多時,監控非常繁雜.為了同時監控多個服務的熔斷狀況,Netflix開源了Hystrix的另一個元件Turbine.Turbine用于聚合多個Hystrix Dashboard監控,将多個Hystrix Dashboard元件的資料聚集在一個面闆展示,集中監控。
3、案例結構

聚合監控服務
node04-monitor-7002
注冊中心
node04-eureka-7001
兩個服務提供者,都配置了熔斷器,和Dashboard元件
node04-provider-6001
node04-provider-6002
二、Dashboard元件
這個元件是針對單個微服務的監控的。具體使用流程如下。
1、注解和依賴
<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>
啟動類注解
- @EnableHystrix
- @EnableHystrixDashboard
2、啟動下面兩個服務
node04-eureka-7001
node04-provider-6001
3、通路指定接口
1)、通路配置的熔斷接口
http://localhost:6001/getInfo
2)、打開資料面闆
http://localhost:6001/hystrix.stream
可以看到一些具體的資料,類似列印日志的方式,展現上面接口的執行資訊。
3)、打開圖形面闆
http://localhost:6001/hystrix
檢視配置監控資訊。
重新整理幾次上面配置的熔斷接口,檢視效果。
三、Turbine元件
node04-monitor-7002 聚合監控服務,聚集6001,和6002兩個服務的監控。
1、依賴和注解
1)、服務提供者新增依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2)、聚合服務依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- @EnableTurbine
2、啟動服務
依次啟動注冊中心,兩個服務提供者,最後啟動聚合監控中心。
3、操作流程
1)、打開監控面闆
進行如下配置
2)、重新整理兩個服務的熔斷接口
http://localhost:6001/getInfo
http://localhost:6002/getInfo
檢視上面面闆的監控資訊如下。
聚合監控服務流程就是這樣了。
四、源代碼說明
GitHub·位址
https://github.com/cicadasmile/spring-cloud-base
GitEE·位址
https://gitee.com/cicadasmile/spring-cloud-base