Hystrix Turbine簡介與使用
一、Hystrix Turbine簡介
看單個的Hystrix Dashboard的資料並沒有什麼多大的價值,要想看這個系統的Hystrix Dashboard資料就需要用到Hystrix Turbine。Hystrix Turbine將每個服務Hystrix Dashboard資料進行了整合。Hystrix Turbine的使用非常簡單,只需要引入相應的依賴和加上註解和配置就可以了。
二、準備工作
本文使用的工程為上一篇文章的工程,在此基礎上進行改造。因為我們需要多個服務的Dashboard,所以需要再建一個服務,取名為service-lucy,它的基本配置同service-hi,具體見原始碼,在這裡就不詳細說明。
三、建立service-turbine
引入相應的依賴:
<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註解,即開啟了註冊服務。
@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列表,表明監控哪些服務
clusterNameExpression: new String("default")
# 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
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
四、Turbine演示
依次開啟eureka-server、service-hi、service-lucy、service-turbine工程。
開啟瀏覽器輸入:http://localhost:8769/turbine.stream,介面如下:
依次請求:
http://localhost:8762/hi?name=forezp
http://localhost:8763/hi?name=forezp
開啟:http://localhost:8763/hystrix,輸入監控流http://localhost:8769/turbine.stream
點選monitor stream 進入頁面:
可以看到這個頁面聚合了2個service的hystrix dashbord資料。