Hystrix-dashboard是一款針對Hystrix進行實時監控的工具,通過Hystrix Dashboard我們可以在直覺地看到各Hystrix Command的請求響應時間, 請求成功率等資料。但是隻使用Hystrix Dashboard的話, 你隻能看到單個應用内的服務資訊, 這明顯不夠. 我們需要一個工具能讓我們彙總系統内多個服務的資料并顯示到Hystrix Dashboard上, 這個工具就是Turbine.
Hystrix Dashboard
我們在熔斷示例項目spring-cloud-consumer-hystrix的基礎上更改,重新命名為:spring-cloud-consumer-hystrix-dashboard。
1、添加依賴
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-hystrix</artifactId>
- </dependency>
- <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
這三個包必須添加
2、啟動類
啟動類添加啟用Hystrix Dashboard和熔斷器
- @SpringBootApplication
- @EnableDiscoveryClient
- @EnableFeignClients
- @EnableHystrixDashboard
- @EnableCircuitBreaker
- public class ConsumerApplication {
- public static void main(String[] args) {
- SpringApplication.run(ConsumerApplication.class, args);
- }
- }
3、測試
啟動工程後通路 http://localhost:9001/hystrix,将會看到如下界面:
圖中會有一些提示:
Cluster via Turbine (default cluster): http://turbine-hostname:port/turbine.stream
Cluster via Turbine (custom cluster): http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
Single Hystrix App: http://hystrix-app:port/hystrix.stream
大概意思就是如果檢視預設叢集使用第一個url,檢視指定叢集使用第二個url,單個應用的監控使用最後一個,我們暫時隻示範單個應用的是以在輸入框中輸入: http://localhost:9001/hystrix.stream ,輸入之後點選 monitor,進入頁面。
如果沒有請求會先顯示 Loading...,通路http://localhost:9001/hystrix.stream 也會不斷的顯示ping。
請求服務http://localhost:9001/hello/neo,就可以看到監控的效果了,首先通路http://localhost:9001/hystrix.stream,顯示如下:
- ping:
- data: {"type":...}
說明已經傳回了監控的各項結果
到監控頁面就會顯示如下圖:
其實就是http://localhost:9001/hystrix.stream傳回結果的圖形化顯示,Hystrix Dashboard Wiki上詳細說明了圖上每個名額的含義,如下圖:
到此單個應用的熔斷監控已經完成。
Turbine
在複雜的分布式系統中,相同服務的節點經常需要部署上百甚至上千個,很多時候,運維人員希望能夠把相同服務的節點狀态以一個整體叢集的形式展現出來,這樣可以更好的把握整個系統的狀态。 為此,Netflix提供了一個開源項目(Turbine)來提供把多個hystrix.stream的内容聚合為一個資料源供Dashboard展示。
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-turbine</artifactId>
- </dependency>
- <artifactId>spring-cloud-netflix-turbine</artifactId>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
- </dependencies>
2、配置檔案
- spring.application.name=hystrix-dashboard-turbine
- server.port=8001
- turbine.appConfig=node01,node02
- turbine.aggregator.clusterConfig= default
- turbine.clusterNameExpression= new String("default")
- eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
- turbine.appConfig :配置Eureka中的serviceId清單,表明監控哪些服務
- turbine.aggregator.clusterConfig :指定聚合哪些叢集,多個使用","分割,預設為default。可使用 http://.../turbine.stream?cluster={clusterConfig之一}通路
- turbine.clusterNameExpression : 1. clusterNameExpression指定叢集名稱,預設表達式appName;此時: turbine.aggregator.clusterConfig需要配置想要監控的應用名稱;2. 當clusterNameExpression: default時, turbine.aggregator.clusterConfig可以不寫,因為預設就是default;3. 當clusterNameExpression: metadata['cluster']時,假設想要監控的應用配置了 eureka.instance.metadata-map.cluster:ABC,則需要配置,同時 turbine.aggregator.clusterConfig:ABC
3、啟動類
啟動類添加 @EnableTurbine,激活對Turbine的支援
- @EnableTurbine
- public class DashboardApplication {
- SpringApplication.run(DashboardApplication.class, args);
到此Turbine(hystrix-dashboard-turbine)配置完成
4、測試
在示例項目spring-cloud-consumer-hystrix基礎上修改為兩個服務的調用者spring-cloud-consumer-node1和spring-cloud-consumer-node2
spring-cloud-consumer-node1項目改動如下: application.properties檔案内容
- spring.application.name=node01
- server.port=9001
- feign.hystrix.enabled=true
spring-cloud-consumer-node2項目改動如下: application.properties檔案内容
- spring.application.name=node02
- server.port=9002
HelloRemote類修改:
- @FeignClient(name= "spring-cloud-producer2", fallback = HelloRemoteHystrix.class)
- public interface HelloRemote {
- @RequestMapping(value = "/hello")
- public String hello2(@RequestParam(value = "name") String name);
對應的 HelloRemoteHystrix和 ConsumerController類跟随修改,具體檢視代碼
修改完畢後,依次啟動spring-cloud-eureka、spring-cloud-consumer-node1、spring-cloud-consumer-node1、hystrix-dashboard-turbine(Turbine)
打開eureka背景可以看到注冊了三個服務:
通路 http://localhost:8001/turbine.stream
傳回:
- : ping
- data: {"reportingHostsLast10Seconds":1,"name":"meta","type":"meta","timestamp":1494921985839}
并且會不斷重新整理以擷取實時的監控資料,說明和單個的監控類似,傳回監控項目的資訊。進行圖形化監控檢視,輸入:http://localhost:8001/hystrix,傳回酷酷的小熊界面,輸入: http://localhost:8001/turbine.stream,然後點選 Monitor Stream ,可以看到出現了倆個監控清單