10.Spring-Cloud-Hystrix之熔斷監控Hystrix Dashboard單個應用
SpringCloud完美的整合Hystrix-dashboard,Hystrix-dashboard是一款針對Hystrix進行實時監控的工具,通過Hystrix Dashboard我們可以在直觀地看到各Hystrix Command的請求響應時間, 請求成功率等資料。可以實時反饋資訊幫助我們快速發現系統中,但是隻使用Hystrix Dashboard的話, 你只能看到單個應用內的服務資訊。
在上一個專案上重構地址
1.pom.xml(必須包)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--引入hystrix熔斷器 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <!--引入hystrix dashboard(儀表盤)--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency>
2.啟動類
package com.niugang; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.web.client.RestTemplate; /** * 負責服務的發現與消費 * * @author niugang * */ @SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker @EnableHystrixDashboard //啟動儀表盤 /* * @SpringCloudApplication * 包含了 * @SpringBootApplication * @EnableDiscoveryClient * @EnableCircuitBreaker * 著三個註解 */ @ComponentScan public class Application { //負載均衡 @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
3.訪問
上面截圖是HystrixBoard的監控首頁,該頁面並沒有什麼監控資訊。從1,2,3標號中可以知道HystrixBoard提供了三種不同的監控方式。
標號1:預設的叢集方式:通過URL http://turbine-hostname:port/turbine.stream開啟,實現對預設叢集的監控。
標號2:指定的叢集監控,通過URL http://turbine-hostname:port/turbine.stream?cluster=[clusterName]開啟對clusterName的叢集監控。
標號3:單體應用的監控,通過URL http://hystrix-app:port/hystrix.stream開啟,實現對具體某個服務例項的監控。
標號4:Delay:改引數用來控制伺服器上輪訓監控資訊的延遲時間,預設為2000毫秒,可以通過配置該屬性來降低客戶端的網路和CPU消耗。
標號5:Title:該引數對應了上圖頭補標題Hystrix Stream之後的內容,預設會使用具體監控例項的URL,可以通過該資訊來展示更合適的標題。
點選Monitor Stream ,如果沒有請求會先顯示Loading ...,重新整理幾次http://localhost:9002/queryUser/5,儀表板監控如下:
訪問http://localhost:9002/hystrix.stream 也會不斷的顯示ping(瀏覽器一直在重新整理):
Hystrix Dashboard Wiki上詳細說明了圖上每個指標的含義,如下圖:
左上部實心圓和一條曲線含義:
實心圓:通過顏色的變化代表了例項的健康程度,它的大小也會根據例項的請求流量發生變化,流量越來實心圓越大。
曲線:用來記錄2分鐘內流量的相對變化,可通過它來觀察流量的上升和下降趨勢。
微信公眾號:
JAVA程式猿成長之路
分享學習資源,學習方法,記錄程式設計師生活。