前言:
本篇博客主要是分享Hystrix圖形化Dashboard搭建與監控實戰,通過博客的編寫提高自己的掌握能力也想幫助其他人:
概述
除了隔離依賴服務的調用以外,Hystrix還提供了準實時的調用監控(Hystrix Dashboard),Hystrix會持續地記錄所有通過Hystrix發起的請求的執行信息,並以統計報表和圖形的形式展示給用戶,包括每秒執行多少請求多少成功,多少失敗等,Netflix通過hystrix-metrics-event-stream項目實現了對以上指標的監控。Spring Cloud也提供了Hystrix Dashboard的整合,對監控內容轉化成可視化界面。
儀表盤創建
- 修改pom
<?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>com.zcw.springcloud2020508</artifactId>
<groupId>com.zcw</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-hystrix-dashboard9001</artifactId>
<dependencies>
<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>
<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>
- 創建YML
server:
port: 9001
- 修改啓動類
package com.zcw.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
/**
* @ClassName : HystrixDashboardApplication9001
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-21 13:43
*/
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication9001 {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication9001.class,args);
}
}
注意如果服務需要圖形化被監控,需要添加如下依賴:
搭建成功:
斷路器演示
- 修改我們服務提供者配置
重點注意的地方:
package com.zcw.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;
/**
* @ClassName : PaymentHystrixApplication
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-19 15:45
*/
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class PaymentHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixApplication.class,args);
}
/**
* 此配置是爲了服務監控而配置,與服務容錯本身無關,springcloud升級後的坑
* ServletRegistrationBean因爲springboot 的默認路徑不是"/hystrix.stream",
* 只要在自己的項目裏配置上下面的servlet就可以了
* @return
*/
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet hystrixMetricsStreamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(hystrixMetricsStreamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
監控
實心圓:
共有兩種含義:它通過顏色的變化代表了實例的健康程度,它的健康度從綠色<黃色<橙色<紅色遞減。
該實心圓除了顏色變化之外,它的大小也會根據實例的請求流量發生變化,流量越大該實心圓就越大,所以通過該實心圓的展示,就可以在大量的實例中快速的發現故障實例和高壓實例。
一條線:
曲線用來記錄2分鐘內流量的相對變化,可以通過它來觀察到流量的上升和下降的趨勢。
整個圖: