天天看點

(五)spring-cloud入門學習:斷路器監控Hystrix Dashboard

在微服務架構中為例保證程式的可用性,防止程式出錯導緻網絡阻塞,出現了斷路器模型。斷路器的狀況反應了一個程式的可用性和健壯性,它是一個重要名額。Hystrix Dashboard是作為斷路器狀态的一個元件,提供了資料監控和友好的圖形化界面。

1. 建立一個eureka_hystrix_dashboard工程,pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.kevin</groupId>
  <artifactId>eureka_hystrix_dashboard</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>eureka_hystrix_dashboard</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
  </dependencies>
  
  <dependencyManagement>
    <dependencies>
        <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-dependencies</artifactId>
           <version>Greenwich.SR1</version>
           <type>pom</type>
           <scope>import</scope>
        </dependency>
    </dependencies>
  </dependencyManagement>
</project>
           

2. Application.java啟動監控

package com.kevin.eureka_hystrix_dashboard;

import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@SpringBootApplication
@EnableHystrixDashboard
public class Application {
	
    public static void main( String[] args ) {
    	new SpringApplicationBuilder(Application.class)
    		.web(WebApplicationType.SERVLET).run(args);
    }
}
           

3. application.properties

spring.application.name=eurekaHystrixDashboard
server.port=9203
           

4. 啟動工程後通路http://127.0.0.1:9203/hystrix

(五)spring-cloud入門學習:斷路器監控Hystrix Dashboard

上面的文字介紹,Hystrix Dashboard共支援三種不同的監控方式

  • 預設的叢集監控:通過http://turbine-hostname:port/turbine.stream開啟,實作對預設叢集的監控。
  • 指定的叢集監控:通過http://turbine-hostname:port/turbine.stream?cluster=[clusterName]開啟,實作對clusterName叢集的監控。
  • 單體應用的監控:通過http://hystrix-app:port/actuator/hystrix.stream開啟,實作對具體某個服務執行個體的監控。

Delay:控制伺服器上輪詢監控資訊的延遲時間,預設為2000毫秒,可以通過配置該屬性來降低用戶端的網絡和CPU消耗。

Title:該參數可以展示合适的标題。

5. 需要監控消費端的hystrix,比如輸入http://127.0.0.1:9201/actuator/hystrix.stream,點選Monitor Stream

1)可能無反應,通過debug浏覽器,發現webjars/jquery/2.1.1/jquery.min.js 404 (Not Found)

原因:可能是maven目錄下org/webjars/jquery/2.1.1目錄下的jar包下載下傳出錯導緻的,删除後重新下載下傳。然後把eureka_hystrix_dashboard工程重新開機。(可能需要重新整理工程多試兩次)

2)提示Unable to connect to Command Metric Stream.

(五)spring-cloud入門學習:斷路器監控Hystrix Dashboard

原因:消費端eureka_ribbon不支援/actuator/hystrix.stream

解決方案:

在Application.java注冊/actuator/hystrix.stream,即暴露endpoints

@Bean
public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet(){
    HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
    ServletRegistrationBean<HystrixMetricsStreamServlet> registrationBean = new ServletRegistrationBean<HystrixMetricsStreamServlet>(streamServlet);
    registrationBean.setLoadOnStartup(1);
    registrationBean.addUrlMappings("/actuator/hystrix.stream");
    registrationBean.setName("HystrixMetricsStreamServlet");
    return registrationBean;
}
           

通過通路http://127.0.0.1:9201/actuator/hystrix.stream可以正常傳回,調用接口http://127.0.0.1:9201/sayHello後會有資料。

(五)spring-cloud入門學習:斷路器監控Hystrix Dashboard
(五)spring-cloud入門學習:斷路器監控Hystrix Dashboard

再次通過監控檢視http://127.0.0.1:9201/actuator/hystrix.stream

(五)spring-cloud入門學習:斷路器監控Hystrix Dashboard
(五)spring-cloud入門學習:斷路器監控Hystrix Dashboard

以上圖來說明其中各元素的具體含義:

  • 實心圓:它有顔色和大小之分,分别代表執行個體的監控程度和流量大小。如上圖所示,它的健康度從綠色、黃色、橙色、紅色遞減。通過該實心圓的展示,我們就可以在大量的執行個體中快速的發現故障執行個體和高壓力執行個體。
  • 曲線:用來記錄 2 分鐘内流量的相對變化,我們可以通過它來觀察到流量的上升和下降趨勢。

6. 基于feign的hystrix dashboard

雖然feign自帶了hystrix,但是卻不包含hystrix-metrics-event-stream,是以不能直接被監控。還是需要引入hystrix:

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
           

同時暴露endpoints

@Bean
public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet(){
	HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
	ServletRegistrationBean<HystrixMetricsStreamServlet> registrationBean = new ServletRegistrationBean<HystrixMetricsStreamServlet>(streamServlet);
	registrationBean.setLoadOnStartup(1);
	registrationBean.addUrlMappings("/actuator/hystrix.stream");
	registrationBean.setName("HystrixMetricsStreamServlet");
	return registrationBean;
}
           

監控即可通路:

http://127.0.0.1:9202/actuator/hystrix.stream

(五)spring-cloud入門學習:斷路器監控Hystrix Dashboard
(五)spring-cloud入門學習:斷路器監控Hystrix Dashboard

參考:

https://cloud.spring.io/spring-cloud-static/Greenwich.SR1/single/spring-cloud.html#netflix-hystrix-dashboard-starter

https://www.cnblogs.com/chenweida/p/9025589.html

繼續閱讀