前言:在上一節中我們使用了Hystrix進行服務熔斷處理,至此關于Hystrix的使用到此為止,本節內容關注的是如何使用HystrixDashboard對調用進行監控。
1、HystrixDashboard概述
Hystrix提供的準實時的調用監控(HystrixDashboard),Hystrix會持續地記錄所有通過Hystrix發起的請求的執行信息,并以統計報表和圖形的形式展示給用戶,包括每秒執行多少請求多少成功,多少失敗等。Netflix通過hystrix-metrics-event-stream項目實現了對以上指標的監控。SpringCloud也提供了HystrixDashboard的整合,對監控內容轉化成可視化界面。
?
2、創建一個監控程序模塊,命名為cloud-consumer-hystrix-dashboard9001
(1)在父工程下新建模塊
?(2)選擇模塊的項目類型為Maven并選擇模塊要使用的JDK版本
(3)填寫子模塊的名稱,然后點完成即可完成創建
效果圖:
3、修改cloud-consumer-hystrix-dashboard9001子模塊的pom.xml文件,然后reolad一下,下載依賴
例:
<?xml version="1.0" encoding="UTF-8"?>
<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"><parent><artifactId>springcloud01</artifactId><groupId>com.ken.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-consumer-hystrix-dashboard9001</artifactId><dependencies><!--hystrix dashboard--><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><!--熱部署--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><!--lombok插件--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>
</project>
4、為cloud-consumer-hystrix-dashboard9001子模塊添加名為application的yml配置文件(注:yml是官方推薦的配置文件格式,最好使用yml文件而不是properties文件)
效果圖:
5、修改application.yml文件配置
server:port: 9001
6、為cloud-consumer-hystrix-dashboard9001子模塊新建一個主啟動類,類名輸入com.ken.springcloud.HystrixDashboardMain9001,然后創建即可
效果圖:
7、編寫HystrixDashboardMain9001啟動類
package com.ken.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardMain9001 {public static void main(String[] args) {SpringApplication.run(HystrixDashboardMain9001.class, args);}}
8、啟動cloud-consumer-hystrix-dashboard9001項目
效果圖:
9、在瀏覽器地址欄里輸入http://localhost:9001/hystrix然后回車進入Hystrix Dashboard監控頁面
效果圖:
10、監控指定的服務,我這里選擇監控cloud-provider-hystrix-payment8001服務
(1)修改cloud-provider-hystrix-payment8001服務的主啟動類
package com.ken.springcloud;import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;@SpringBootApplication
//使用Feign,激活并開啟
@EnableEurekaClient
//開啟斷路器功能
@EnableCircuitBreaker
public class PaymentHystrixMain8001 {public static void main(String[] args) {SpringApplication.run(PaymentHystrixMain8001.class, args);}//此配置是為了服務監控而配置,與服務容錯本身無關,springcLoud升級后的坑,ServletRegistrationBean因為springboot的默認路徑不是"/hystrix.stream" ,只要在自己的項目里配置上下面的servlet就可以了@Beanpublic ServletRegistrationBean getServlet() {HystrixMetricsStreamServlet hystrixMetricsStreamServlet = new HystrixMetricsStreamServlet();ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(hystrixMetricsStreamServlet);servletRegistrationBean.setLoadOnStartup(1);servletRegistrationBean.addUrlMappings("/hystrix.stream");servletRegistrationBean.setName("HystrixMetricsStreamServlet");return servletRegistrationBean;}}
(2)先啟動eureka-server7001然后再啟動cloud-provider-hystrix-payment8001
效果圖:
(3)再次來到Hystrix Dashboard監控頁面,在對應的地方填寫上相應的信息
[1]填寫Turbine訪問地址http://localhost:8001/hystrix.stream
[2]Delay填寫2000
[3]Title填寫Test
例:
(4)點擊按鈕開始監控cloud-provider-hystrix-payment8001服務
效果圖:
注:這里一開始是空白的,開始沒有請求的話會一直顯示Loading,不用擔心,不是程序問題
11、 查看請求正常的情況下斷路器的狀態
在瀏覽器的地址欄里連續多次的輸入http://localhost:8001/payment/circuit/1,調用接口后返回到HystrixDashboard的頁面監控cloud-provider-hystrix-payment8001服務的狀態
例:
?
可以看出在請求正常的情況下斷路器的狀態是CLOSED
?
12、根據圖形化界面判斷服務、請求的狀態
(1)根據右邊對應狀態的7種顏色可以在左邊看出對應狀態下的請求次數
每個狀態都對應著一個顏色,可以通過顏色在界面左邊的那對數字那里看出對應請求狀態的出現次數
(2)根據左邊的實心圓判斷實例的健康程度、請求數量
實心圓共有兩種含義,其一是通過顏色的變化代表了實例的健康程度,它的健康度從綠色<黃色<橙色<紅色遞減;其二是實心圓的大小也會根據實例的請求流量發生變化,流量越大該實心圓就越大,所以通過觀察實心圓的變化,就可以在大量的實例中快速的發現故障實例和高壓力實例。
綠色:
紅色:
(3)根據左邊的曲線判斷流量的情況
曲線記錄了2分鐘內流量的相對變化,可以通過它來觀察到流量的上升和下降趨勢。