十三:Spring Cloud 之Hystrix Dashboard
阿新 • • 發佈:2018-12-11
1. 簡介
Hystrix是Netflix解決自己業務不穩定性的一個限流容錯框架,可以幫助我們解決微服務架構體系中的限流、降級、熔斷等功能。提高系統穩定性,提供了完善的監控實現,並且Hystrix可以根據監控資料動態調整內部處理機制。
Hystrix Dashboard提供了數字與圖形化的呼叫監控情況。
2. 程式碼實現
2.1 涉及的模組及整體步驟
2.1.1 涉及的模組
- eureka-server-singleton:微服務的服務註冊中心,限流熔斷針對的是服務呼叫異常的處理。
- eureka-hystrix-dashboard:圖形化展示資料監控情況
2.1.2 整體步驟
- 實現eureka-server-singleton
- 實現eureka-hystrix-dashboard
- 呼叫eureka-hystrix-dashboard提供的服務介面
- 開啟eureka-hystrix-dashboard的監控介面,輸入相關資訊,檢視呼叫情況
2.2 原始碼
2.2.1 Github地址
2.3 eureka-server-singleton
2.4 eureka-hystrix-dashboard
2.4.1 整體實現
- pom.xml:引入相關依賴,具體檢視下方具體程式碼
- application.yml:指定埠,應用名,註冊中心訪問地址資訊
- bootstrap.yml:指定公開SpringBoot的actuator監控端點為所有
- EurekaHystrixDashboardApplication:Spring boot啟動類,提供一個簡單的REST服務介面
2.4.2 pom.xml
<?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>spring-cloud-finchley-demo</artifactId>
<groupId>org.oscar.scd</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-hystrix-dashboard</artifactId>
<dependencies>
<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>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix-dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
</dependencies>
</project>
2.4.3 application.yml
server:
port: 8785
spring:
application:
name: eureka-hystrix-dashboard
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
2.4.4 bootstrap.yml
management:
endpoints:
web:
exposure:
include: '*'
2.4.5 EurekaHystrixDashboardApplication
package org.oscar.scd.eureka.hystrix.dashboard;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@EnableHystrix
@RestController
@EnableEurekaClient
@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
@EnableHystrixDashboard
public class EurekaHystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaHystrixDashboardApplication.class, args);
}
@Value("${spring.application.name}")
private String appName;
@RequestMapping("/appName")
@HystrixCommand(fallbackMethod = "errorMsg")
public String getAppName() {
return this.appName;
}
@SuppressWarnings("unused")
public String errorMsg() {
return "Error msg by hystrix.";
}
}
3. 驗證
3.1 建立SpringBoot啟動類
最簡單的方式新增一個SpringBoot啟動型別的啟動類就行,如果對SpringBoot有所瞭解或者看過前面系列的文章,這裡很簡單。
3.1.1 EurekaServerSingletonApplication
略
3.1.2 EurekaHystrixDashboardApplication
略
3.2 啟動
EurekaServerSingletonApplication
EurekaHystrixDashboardApplication
3.3 檢視Hystrix Dashboard
eureka-hystrix-dashboard
- 訪問Hystrix介面前兩個輸入框依次輸入:
http://localhost:8785/actuator/hystrix.stream
、2000
4. 思考
- 現在的僅限於單機監控,如何在分散式環境下檢視監控資訊
- 如何與呼叫鏈監控整合,也就是如何搭建一個完整的監控平臺
- 現在這個要輸入查詢條件,如何定製化首介面和展示項,更接近於實用和生產