1. 程式人生 > >第十篇: 斷路器聚合監控(Hystrix Turbine)

第十篇: 斷路器聚合監控(Hystrix Turbine)

上一篇文章講述瞭如何利用Hystrix Dashboard去監控斷路器的Hystrix command。當我們有很多個服務的時候,這就需要聚合所以服務的Hystrix Dashboard的資料了。這就需要用到Spring Cloud的另一個元件了,即Hystrix Turbine。

一、Hystrix Turbine簡介

看單個的Hystrix Dashboard的資料並沒有什麼多大的價值,要想看這個系統的Hystrix Dashboard資料就需要用到Hystrix Turbine。Hystrix Turbine將每個服務Hystrix Dashboard資料進行了整合。Hystrix Turbine的使用非常簡單,只需要引入相應的依賴和加上註解和配置就可以了。

二、準備工作

本文使用的工程為上一篇文章 第九篇: 斷路器監控(Hystrix Dashboard) 的工程,在此基礎上進行改造。因為我們需要多個服務的Dashboard,準備的服務有 leopard-eureka,leopard-feign ,具體專案程式碼檢視之前,在這裡就不詳細說明。

三、建立 leopard-turbine

建立 spring-boot工程,以下為依賴檔案 pom.xml

<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>
	<groupId>com.leopard</groupId>
	<artifactId>leopard-turbine</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	
	<parent>
		<groupId>com.leopard</groupId>
		<artifactId>leopard-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-turbine</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-netflix-turbine</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

</project>

在其入口類TurbineServiceApplication加上註解@EnableTurbine,開啟turbine,@EnableTurbine註解包含了@EnableDiscoveryClient註解,即開啟了註冊服務。

如對 @EnableDiscoveryClient和@EnableEurekaClient 兩個註解有疑問的,

package com.leopard.turbine;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

@EnableTurbine
@SpringBootApplication
public class TurbineServiceApplication {

	public static void main(String[] args) {
		SpringApplication.run(TurbineServiceApplication.class, args);
	}

}

application.yml

spring:
  application.name: leopard-turbine

server:
  port: 8281

#驗證關閉
#security.basic.enabled: false

turbine:
  aggregator:
    clusterConfig: default   
  appConfig: leopard-service-feign,leopard-service-feign1
  clusterNameExpression: new String("default")

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

turbine.aggregator.clusterConfig:指定聚合哪些叢集,多個使用","分割,預設為default。可使用http://.../turbine.stream?cluster={clusterConfig之一}訪問

turbine.aggregator.appConfig:配置Eureka中的serviceId列表,表明監控哪些服務

turbine.aggregator.clusterNameExpression:

#1. clusterNameExpression指定叢集名稱,預設表示式appName;此時:turbine.aggregator.clusterConfig需要配置想要監控的應用名稱
#2. 當clusterNameExpression: default時,turbine.aggregator.clusterConfig可以不寫,因為預設就是default
#3. 當clusterNameExpression: metadata['cluster']時,假設想要監控的應用配置了eureka.instance.metadata-map.cluster: ABC,則需要配置,同時turbine.aggregator.clusterConfig: ABC

四、Turbine演示

依次開啟leopard-eureka(8761)、leopard-service-feign(8181)、leopard-service-feign1(8182) 、leopard-turbine(8281)工程。

leopard-service-feign1 可通過原先專案 leopard-service-feign 改配置後再次啟動,等同於啟動兩個不同的服務,無需多建立。

依次請求:

開啟 http://localhost:8182/hystrix 

輸入監控流:http://localhost:8769/turbine.stream , 2000 , leopard-look

點選monitor stream 進入頁面: