轉(zhuǎn)載請標明出處:
http://blog.csdn.net/forezp/article/details/70233227
本文出自方志朋的博客
上一篇文章講述了如何利用Hystrix Dashboard去監(jiān)控斷路器的Hystrix command。當我們有很多個服務(wù)的時候,這就需要聚合所以服務(wù)的Hystrix Dashboard的數(shù)據(jù)了。這就需要用到Spring Cloud的另一個組件了,即Hystrix Turbine。
一、Hystrix Turbine簡介
看單個的Hystrix Dashboard的數(shù)據(jù)并沒有什么多大的價值,要想看這個系統(tǒng)的Hystrix Dashboard數(shù)據(jù)就需要用到Hystrix Turbine。Hystrix Turbine將每個服務(wù)Hystrix Dashboard數(shù)據(jù)進行了整合。Hystrix Turbine的使用非常簡單,只需要引入相應(yīng)的依賴和加上注解和配置就可以了。
二、準備工作
本文使用的工程為上一篇文章的工程,在此基礎(chǔ)上進行改造。因為我們需要多個服務(wù)的Dashboard,所以需要再建一個服務(wù),取名為service-lucy,它的基本配置同service-hi,具體見源碼,在這里就不詳細說明。
三、創(chuàng)建service-turbine
引入相應(yīng)的依賴:
<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>
在其入口類ServiceTurbineApplication加上注解@EnableTurbine,開啟turbine,@EnableTurbine注解包含了@EnableDiscoveryClient注解,即開啟了注冊服務(wù)。
@SpringBootApplication
@EnableTurbine
public class ServiceTurbineApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ServiceTurbineApplication.class).web(true).run(args);
}
}
配置文件application.yml:
spring:
application.name: service-turbine
server:
port: 8769
security.basic.enabled: false
turbine:
aggregator:
clusterConfig: default # 指定聚合哪些集群,多個使用","分割,默認為default。可使用http://.../turbine.stream?cluster={clusterConfig之一}訪問
appConfig: service-hi,service-lucy ### 配置Eureka中的serviceId列表,表明監(jiān)控哪些服務(wù)
clusterNameExpression: new String("default")
# 1. clusterNameExpression指定集群名稱,默認表達式appName;此時:turbine.aggregator.clusterConfig需要配置想要監(jiān)控的應(yīng)用名稱
# 2. 當clusterNameExpression: default時,turbine.aggregator.clusterConfig可以不寫,因為默認就是default
# 3. 當clusterNameExpression: metadata['cluster']時,假設(shè)想要監(jiān)控的應(yīng)用配置了eureka.instance.metadata-map.cluster: ABC,則需要配置,同時turbine.aggregator.clusterConfig: ABC
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
配置文件注解寫的很清楚。
四、Turbine演示
依次開啟eureka-server、service-hi、service-lucy、service-turbine工程。
打開瀏覽器輸入:http://localhost:8769/turbine.stream,界面如下:
依次請求:
http://localhost:8763/hi?name=forezp
打開:http://localhost:8763/hystrix,輸入監(jiān)控流http://localhost:8769/turbine.stream
點擊monitor stream 進入頁面:
可以看到這個頁面聚合了2個service的hystrix dashbord數(shù)據(jù)。
源碼下載:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter13