什麼是turbine
Turbine是聚合服務器發送事件流數據的一個工具,用來監控集羣下hystrix的metrics情況。Turbine通過註冊中心獲取對應集羣下的所有微服務實例,然後依次訪問每個實例的/hystrix.stream,並收集和聚合所有節點健康信息
turbine的作用
彙總系統內多個服務的數據並顯示到Hystrix Dashboard 上
turbine的搭建
1、引入maven依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</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> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2、創建啓動類
@SpringBootApplication @EnableDiscoveryClient @EnableTurbine public class TurbineServer { private static Logger logger = LoggerFactory.getLogger(TurbineServer .class); public static void main(String[] args) { SpringApplication.run(TurbineServer .class, args); } }
注:如果是使用新版的eureka客戶端依賴,則無需加上@EnableDiscoveryClient註解
3、配置
server: port: 7400 spring: application: name: ${deploy.servicename} turbine: aggregator: clusterConfig: muse,oxford appConfig: aaabbb clusterNameExpression: metadata['cluster'] instanceUrlSuffix: oxford: /Oxford/hystrix.stream eureka: instance: ipAddress: ${eurekaInstanceIpAddress} preferIpAddress: true name: ${deploy.servicename} metadataMap: zone: ${eurekaZone} instanceId: ${deploy.servicename}_${spring.cloud.client.ipAddress} client: serviceUrl: defaultZone: ${eurekaClientServiceUrlDefaultZone} healthcheck: enabled: true
4、配置文件turbine參數介紹
1、turbine.aggregator.clusterConfig # 指定聚合哪些集羣,多個使用","分割,默認爲default。可使用http://.../turbine.stream?cluster={clusterConfig之一}訪問 2、turbine.appConfig # 配置Eureka中的serviceId列表,表明監控哪些服務 3、turbine.clusterNameExpression a. clusterNameExpression指定集羣名稱,默認表達式appName;此時:turbine.aggregator.clusterConfig需要配置想要監控的應用名稱 b. 當clusterNameExpression: default時,turbine.aggregator.clusterConfig可以不寫,因爲默認就是default c. 當clusterNameExpression: metadata['cluster']時,假設想要監控的應用配置了eureka.instance.metadata-map.cluster: ABC,則需要配置,同時turbine.aggregator.clusterConfig: ABC。即:從客戶端的metadata-map的元數據中取key=cluster的值,如果跟上面配置的clusterConfig中的一致纔可以訪問 4、turbine.instanceUrlSuffix.oxford #由於某些項目配置了context-path,而默認的turbine訪問的hystrix.stream是/hystrix.stream,這會導致turbine不能採集到配置context-path的項目信息。因此需要配置turbine.instanceUrlSuffix.[clusterName]=/context-path/hystrix.stream
5、 當clusterNameExpression: metadata['cluster'],則客戶端需要添加行如
eureka: instance: metadataMap: cluster: muse
6、 啓動hystrix dashborad查看turbine彙集的系統信息
hystrix dashborad界面輸入形如:
http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
小結
以上是turbine搭建的簡單入門,在實際開發中,turbine通常聚合網關服務就可以,因爲基本上微服務都是通過網關訪問,當然要具體根據實際情況分析