天天看點

Hystrix Dashboard斷路監控儀表盤使用

簡介

在上一篇文章中介紹了Hystrix的使用,如果我們想看到Hystrix相關資料,比如有多少請求、多少成功、多少失敗、多少降級等,那麼引入 SpringBoot健康監控之後,通路/actuator/hystrix.stream接口可以擷取到監控的文字資訊,但是不直覺,是以Hystrix官方還提供了基于圖形化的DashBoard(儀表闆)監控平 台。Hystrix儀表闆可以顯示每個斷路器(被@HystrixCommand注解的方法)的狀态。

Hystrix Dashboard斷路監控儀表盤使用

搭建過程

  1. 首先建立一個SpringBoot工程,引入依賴
<dependencies>
	<!--hystrix-->
	<dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
	</dependency>
	<!--hystrix 儀表盤-->
	<dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
	</dependency>
	<dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
	</dependency>
</dependencies>
           
  1. 啟動類添加@EnableHystrixDashboard激活儀表盤
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrixDashboard  // 開啟hystrix dashboard
public class HystrixDashboard {

    public static void main(String[] args) {
        SpringApplication.run(HystrixDashboard.class,args);
    }
}
           
  1. 建立并修改配置檔案application.yml
server:
  port: 9000
Spring:
  application:
    name: lagou-cloud-hystrix-dashboard
eureka:
  client:
    serviceUrl: 
      # eureka server的路徑
      #把 eureka 叢集中的所有 url 都填寫了進來,也可以隻寫一台,因為各個 eureka server 可以同步系統資料庫
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  instance:
    #使用ip注冊,否則會使用主機名注冊了(此處考慮到對老版本的相容,新版本經過實驗都是ip)
    prefer-ip-address: true
    #自定義執行個體顯示格式,加上版本号,便于多版本管理,注意是ip-address,早期版本是ipAddress
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.[email protected]
           
  1. 在被監測的微服務中注冊監控servlet(自動投遞微服務,監控資料就是來自于這個微服務)
/**
 * 在被監控的微服務中注冊一個serlvet,後期我們就是通過通路這個servlet來擷取該服務的Hystrix監控資料的
 * 前提:被監控的微服務需要引入springboot的actuator功能
 * @return
 */
@Bean
public ServletRegistrationBean getServlet(){
    HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
    registrationBean.setLoadOnStartup(1);
    registrationBean.addUrlMappings("/actuator/hystrix.stream");
    registrationBean.setName("HystrixMetricsStreamServlet");
    return registrationBean;
}
           

被監控微服務釋出之後,可以直接通路監控servlet,但是得到的資料并不直覺,後期可以結合儀表盤更友好的展示

調用被監測的微服務接口,通路http://localhost:8090/actuator/hystrix.stream,如下,可以看到請求及資訊

Hystrix Dashboard斷路監控儀表盤使用

5. 打開剛剛釋出的Hystrix Dashboard位址 http://localhost:9000/hystrix

Hystrix Dashboard斷路監控儀表盤使用

輸入要監控的服務位址點選下面的Monitor Stream按鈕,如下圖所示

Hystrix Dashboard斷路監控儀表盤使用

上圖左邊的實心圓有下面兩個含義

  • 大小:代表請求流量的大小,流量越大球越大
  • 顔色:代表請求處理的健康狀态,從綠色到紅色遞減,綠色代表健康,紅色就代表很不健康

不同的顔色代表不同的名額,在右上角有不同顔色的含義說明

往往真實環境中微服務會以叢集的方式部署,上面需要輸入每個服務的監控位址來檢視對應的資訊,不友善統一檢視所有服務的聚合資訊,

是以就出現了Hystrix Turbine聚合監控,下一篇文章介紹如何使用

繼續閱讀