1. 程式人生 > >六、SpringCloud斷路監控面板Hystrix Dashboard

六、SpringCloud斷路監控面板Hystrix Dashboard

一、簡介
Hystrix Dashboard是Hystrix的一個元件,Hystrix Dashboard提供一個斷路器的監控面板,可以使我們更好的監控服務和叢集的狀態,僅僅使用Hystrix Dashboard只能監控到單個斷路器的狀態,實際開發中還需要結合Turbine使用
二、Hystrix Dashboard的使用
2.1 、在Ribbon+RestTemplate負載均衡中使用Hystrix Dashboard
第一步:在consulclient3中新增依賴

<!--斷路器面板-->
        <dependency>
            <groupId
>
org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency>

第二步:配置Hystrix Stream 的urlMapping
由於在SpringBoot 2.0版本中已經不能自動配置urlMapping,所以需要手動配置了

@Configuration
public class HystrixConfiguration
{
@Bean public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet(){ HystrixMetricsStreamServlet hystrixMetricsStreamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean<HystrixMetricsStreamServlet> servletRegistrationBean = new ServletRegistrationBean(); servletRegistrationBean.setServlet(hystrixMetricsStreamServlet); servletRegistrationBean.addUrlMappings("/hystrix.stream"
); servletRegistrationBean.setName("HystrixMetricsStreamServlet"); return servletRegistrationBean; } }

第三步、Springboot Application中添加註解,並啟動程式

@EnableHystrixDashboard //開啟Dashboard

第四步:檢視面板
瀏覽器中開啟localhost:9999/hystrix
這裡寫圖片描述
圖中提示資料來源有三種:
http://turbine-hostname:port/turbine.stream (所有叢集)
http://turbine-hostname:port/turbine.stream?cluster=[clusterName](指定名稱的叢集)
http://hystrix-app:port/hystrix.stream(單個應用)
我們選擇第三個,點選Moitor Stream會自動生成監控面板,我們就可以實時監控資料
這裡寫圖片描述
不同的顏色對對應斷路器監控的百分比,通過統計10秒內得出的資料

2.1 、在Feign負載均衡中使用Hystrix Dashboard
注意:一定需要配置開啟斷路器

feign.hystrix.enabled=true

其他步驟和2.1中相同

如果處於loading data狀態,是為沒有資料,呼叫服務就可以載入資料了

原始碼:
https://github.com/NapWells/spring_cloud_learn/tree/master/discover_server_with_consul/springcloudlearn