一、簡介
Hystrix是由Netflix開源的一個延遲和容錯庫,實現熔斷器。斷路器的狀況反應了一個程序的可用性和健壯性,它是一個重要指標。Hystrix Dashboard是一個圖形化界面,支持數據監控。
二、搭建熔斷器服務
<?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>mo-cloud</artifactId>
<groupId>com.mo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<description>Hystrix Dashboard(斷路器:Hystrix 儀表盤)只監控一個實例</description>
<artifactId>hystrix-dashboard</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 服務監控的依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 熔斷依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- 斷路器儀表盤依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
</project>
server:
# springboot設置隨機端口號
port: 0
eureka:
client:
service-url:
default-zone: http://127.0.0.1:8761/eureka/
instance:
# 重寫服務的唯一Id,防止多實例對被註冊成一個服務
instance-id: ${spring.application.name}:${random.int}
# 設置優先使用ip進行註冊
prefer-ip-address: true
spring:
application:
name: hystrix-dashboard
# actuator 安全檢查開放消息推送刷新
management:
endpoints:
web:
exposure:
# 開放安全檢查的端點
include: "*"
package com.mo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* EnableHystrixDashboard註解,開啓HystrixDashboard,儀表盤監控服務
* SpringCloudApplication註解,支持服務註冊,springboot啓動,hystrix熔斷
*
* @author x.pan
* @email [email protected]
* @date 2020/5/2 16:23
*/
@RestController
@SpringCloudApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
@GetMapping("/hello")
@HystrixCommand(fallbackMethod = "helloError")
public String hello() {
return "hello";
}
/**
* HystrixCommand 熔斷的失敗返回方法
*
* @return
*/
public String helloError() {
return "hello error";
}
/**
* 版本差異:添加actuator端點hystrix.stream
* <p>
* 訪問地址 http://localhost:xxx/actuator/hystrix.stream
*
* @return
*/
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registration.addUrlMappings("/actuator/hystrix.stream");
return registration;
}
}
三、Hystrix Dashboard演示
1. 訪問http://127.0.0.1:49859/hystrix
2. 添加 http://127.0.0.1:49859/actuator/hystrix.stream端點
3. 熔斷監控,請求接口http://127.0.0.1:49859/hello