1. 程式人生 > >SpringCloud(六)Hystrix斷路器《2》(服務監控hystrixDashboard)

SpringCloud(六)Hystrix斷路器《2》(服務監控hystrixDashboard)

1、服務監控hystrixDashboard

1.1、概述

除了隔離依賴服務的呼叫以外,Hystrix還提供了準實時的呼叫監控(Hystrix Dashboard),Hystrix會持續地記錄所有通過Hystrix發起的請求的執行資訊,並以統計報表和圖形的形式展示給使用者,包括每秒執行多少請求多少成功,多少失敗等。Netflix通過hystrix-metrics-event-stream專案實現了對以上指標的監控。Spring Cloud也提供了Hystrix Dashboard的整合,對監控內容轉化成視覺化介面。

1.2、新建工程microservice-consumer-hystrix-dashboard

1.3、pom.xml

修改內容:

   <!-- hystrix和 hystrix-dashboard相關-->
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>
spring-cloud-starter-hystrix

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

全部內容:

<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>
  <parent>
    <groupId>com.nari</groupId>
    <artifactId>microservice-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>microservice-consumer-hystrix-dashboard</artifactId>

  <dependencies>
		<!-- 自己定義的api -->
		<dependency>
			<groupId>com.nari</groupId>
			<artifactId>microservice-api</artifactId>
			<version>${project.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 修改後立即生效,熱部署 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
		<!-- Ribbon相關 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-ribbon</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<!-- feign相關 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
		<!-- hystrix和 hystrix-dashboard相關 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
		</dependency>
	</dependencies>
</project>

1.4、application.yml

server:
  port: 9001

1.5、主啟動類改名+新註解@EnableHystrixDashboard


1.6、所有Provider微服務提供類(8001/8002/8003)都需要監控依賴配置

   <!-- actuator監控資訊完善 -->
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>
spring-boot-starter-actuator</artifactId>
   </dependency>

1.7、啟動microservice-consumer-hystrix-dashboard該微服務監控消費端

http://localhost:9001/hystrix

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

Title:該引數對應了頭部標題Hystrix Stream之後的內容,預設會使用具體監控例項的URL,可以通過配置該資訊來展示更合適的標題。

1.8、啟動microservice-provider-dept-hystrix-8001

要訪問/hystrix.stream介面,得先訪問consumer工程中的任意一個其他介面,否則如果直接訪問/hystrix.stream介面的話,會打印出一連串的ping: ping: …。

http://localhost:8001/hystrix.stream

OK,我先訪問consumer中的任意一個其他介面,然後在訪問/hystrix.stream介面,訪問地址如下:http://localhost:8001/hystrix.stream,訪問結果如下:

http://localhost:8001/provider/dept/get/1

http://localhost:8001/hystrix.stream


 

1.9、啟動相關微服務工程

1.10、監控測試

填寫監控地址:

上述我們看到返回結果是一段json資料,單純的檢視json資料,我們很難分析出結果,所以,我們要在Hystrix儀表盤中來檢視這一段json,在hystrix儀表盤中輸入監控地址,如下:

監控結果:

然後點選Monitor Stream按鈕,我們就可以看到監控畫面了,如下:

1.11、引數詳解

OK,儀表盤已經顯示出來了,那麼儀表盤上的各項資料都是什麼意思呢?我們來看下面一張圖:

實心圓:共有兩種含義。它通過顏色的變化代表了例項的健康程度,它的健康度從綠色<黃色<橙色<紅色遞減。該實心圓除了顏色的變化之外,它的大小也會根據例項的請求流量發生變化,流量越大該實心圓就越大。所以通過該實心圓的展示,就可以在大量的例項中快速的發現故障例項和高壓力例項。

曲線:用來記錄2分鐘內流量的相對變化,可以通過它來觀察到流量的上升和下降趨勢。