Hystrix Dashboard
Hystrix-dashboard是一款針對Hystrix進行實時監控的工具,通過Hystrix Dashboard我們可以在直觀地看到各Hystrix Command的請求響應時間, 請求成功率等數據。
添加依賴
我們新建一個工程 spring-cloud-consul-monitor,修改 pom 文件,添加相關依賴。
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> </dependencies>
啟動類
在啟動類中添加注解 @EnableHystrixDashboard 開啟熔斷監控支持。
ConsuleMonitorApplication.java
package com.louis.spring.cloud.consul.monitor; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; @EnableHystrixDashboard @SpringBootApplication public class ConsuleMonitorApplication { public static void main(String[] args) { SpringApplication.run(ConsuleMonitorApplication.class, args); } }
修改配置
修改配置文件,配置啟動端口和應用名稱。
application.yml
server: port: 8531 spring: application: name: spring-cloud-consul-monitor
配置監控路徑
注意,如果你使用的是2.x等比較新的版本,需要在 Hystrix 的消費端配置監控路徑,我們這里消費端是 spring-cloud-consul-consumer, 所以修改它的啟動類。
ConsuleConsumerApplication.java
// 此配置是為了服務監控而配置,與服務容錯本身無關, // ServletRegistrationBean因為springboot的默認路徑不是"/hystrix.stream", // 只要在自己的項目里配置上下面的servlet就可以了 @Bean public ServletRegistrationBean getServlet() { HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; }
測試效果
先后啟動 spring-cloud-consul-producer、 spring-cloud-consul-consumer、spring-cloud-consul-monitor 服務。
訪問 http://localhost:8531/hystrix,會看到如下圖所示界面。
此時沒有任何具體的監控信息,需要輸入要監控的消費者地址及監控信息的輪詢時間和標題。
Hystrix Dashboard 共支持三種不同的監控方式:
單體Hystrix 消費者:通過URL http://hystrix-app:port/hystrix.stream 開啟,實現對具體某個服務實例的監控。
默認集群監控:通過URL http://turbine-hostname:port/turbine.stream 開啟,實現對默認集群的監控。
自定集群監控:通過URL http://turbine-hostname:port/turbine.stream?cluster=[clusterName] 開啟,實現對clusterName集群的監控。
我們這里現在是對單體 Hystrix 消費者的監控,后面整合 Turbine 集群的時候再說明后兩種的監控方式。
我們先訪問 http://localhost:8521/feign/call, 查看要監控的服務是否可以正常訪問。
確認服務可以正常訪問之后,在監控地址內輸入 http://localhost:8521/hystrix.stream,然后點擊 Monitor Stream 開始監控。
剛進去,頁面先顯示 loading... 信息, 多次訪問 http://localhost:8521/feign/call 之后,統計圖表信息如下圖所示。
各個指標的含義參見下圖。
源碼下載
碼雲:https://gitee.com/liuge1988/spring-cloud-demo.git
作者:朝雨憶輕塵
出處:https://www.cnblogs.com/xifengxiaoma/
版權所有,歡迎轉載,轉載請注明原文作者及出處。