上一篇文章講述了如何利用Hystrix Dashboard去監控斷路器的Hystrix command。當我們有很多個服務的時候,這就需要聚合所有服務的Hystrix Dashboard的數據了。這就需要用到Spring Cloud的另一個組件了,即Hystrix Turbine。
一、Hystrix Turbine簡介
看單個的Hystrix Dashboard的數據並沒有什么多大的價值,要想看多個系統或集群系統的Hystrix Dashboard數據就需要用到Hystrix Turbine。Hystrix Turbine將每個服務Hystrix Dashboard數據進行了整合。Hystrix Turbine的使用非常簡單,只需要引入相應的依賴和加上注解和配置就可以了。
二、准備工作
因為我們需要監控多個服務的Dashboard,所以需要搭建一個Turbine服務來聚合監控 Hystrix 斷路器,取名為spring-cloud-hystrix-turbine。
三、創建spring-cloud-hystrix-turbine
1、引入pom依賴
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<artifactId>spring-cloud-hystrix-turbine</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId> spring-cloud-starter-netflix-eureka-client </artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
2、配置文件application.yml
spring:
application:
name: spring-cloud-hystrix-turbine
cloud:
consul:
discovery:
prefer-ip-address: true
instanceId: ${spring.application.name}:${server.port}
host: localhost
port: 8500
server:
port: 8810
turbine:
aggregator:
#監控所有微服務集群
#hytrix儀表盤:http://localhost:8810/hystrix/
#監控地址:http://localhost:8810/turbine.stream
#在hystrix儀表盤中監控上面的地址即可
clusterConfig: default
#要監控的微服務serviceId
appConfig: mcc-feign-hystrix,mcc-ribbon-hystrix,mcc-ribbon-hystrix-propagating
clusterNameExpression: "'default'"
3、TurbineApplication——Turbine入口程序
package com.lynch.consumer.turbine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
@SpringBootApplication
//開啟Turbine支持,用來進行集群監控
@EnableTurbine
//開啟Hystrix儀表盤
@EnableHystrixDashboard
public class TurbineApplication {
public static void main(String[] args) {
SpringApplication.run(TurbineApplication.class, args);
}
}
四、Turbine演示
依次開啟mcc-feign-hystrix、mcc-ribbon-hystrix、mcc-ribbon-hystrix-propagating、spring-cloud-hystrix-turbine工程。
打開瀏覽器輸入:http://localhost:8810/turbine.stream,界面如下:
依次多次請求:
http://localhost:8807/ribbon/get/aa
http://localhost:8808/feign1/get/aa
hystrix斷路器生效。
打開:http://localhost:8810/hystrix/,輸入監控流http://localhost:8810/turbine.stream
點擊monitor stream 進入頁面:
可以看到這個頁面聚合了2個service的hystrix dashbord數據。
參數詳解
OK,儀表盤已經顯示出來了,那么儀表盤上的各項數據都是什么意思呢?我們來看下面一張圖: