上篇博文中學到了Hystrix Board監控單個應用,除此之外還有一個Turbine提供的監控點/trubine.stream是對叢集的監控使用。在複雜的分布式系統中,相同服務的節點經常需要部署上百甚至上千個,很多時候,運維人員希望能夠把相同服務的節點狀态以一個整體叢集的形式展現出來,這樣可以更好的把握整個系統的狀态。 為此,Netflix提供了一個開源項目(Turbine)來提供把多個hystrix.stream的内容聚合為一個資料源供Dashboard展示。
一:建構Turbine項目
1.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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>8.spring-cloud-turbine</groupId>
<artifactId>turbine</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud Maven Webapp</name>
<url>http://maven.apache.org</url>
<!--springboot采用1.5.x 對應springcloud版本為 Dalston -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
</properties>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 這樣變成可執行的jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.啟動類
package com.niugang;
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;
/**
*
* Turbine監控
* @author niugang
*
*/
@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine //啟動turbine
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3.配置
#指定微服務的名稱後續在調用的時候隻需要使用該名稱就可以進行服務的通路
spring.application.name=hystrix-dashboard-turbine
server.port=9004
#配置Eureka中的serviceId清單,表明監控哪些服務
turbine.appConfig=RIBBON-CONSUMER1,RIBBON-CONSUMER2
turbine.aggregator.clusterConfig= default
turbine.clusterNameExpression= new String("default")
#turbine.combine-host-port=true
#注冊中心位址
eureka.client.serviceUrl.defaultZone=http://testhost:8000/eureka/
- turbine.appConfig :配置Eureka中的serviceId清單,表明監控哪些服務
- turbine.aggregator.clusterConfig :指定聚合哪些叢集,多個使用”,”分割,預設為default。可使用http://.../turbine.stream?cluster={clusterConfig之一}通路
- turbine.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
二:額外準備
準備ribbon-consumer1,ribbon-consumer2兩個服務。
1.ribbon-consumer1
#指定微服務的名稱後續在調用的時候隻需要使用該名稱就可以進行服務的通路
spring.application.name=ribbon-consumer1
server.port=9002
spring.cloud.loadbalancer.retry.enabled=true
#注冊中心位址
eureka.client.serviceUrl.defaultZone=http://testhost:8000/eureka/
------------------------------------------------------------------------------------------------------------------
HelloService.java
@CacheResult
@HystrixCommand(fallbackMethod = "queryUserBackMethod",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000"),
@HystrixProperty(name = "requestCache.enabled", value = "true")
})
public String queryUser( String id) {
String body = restTemplate
.postForEntity("http://service-provide/queryUser/{1}", String.class, String.class,id)
.getBody();
return body;
}
2.ribbon-consumer2
#指定微服務的名稱後續在調用的時候隻需要使用該名稱就可以進行服務的通路
spring.application.name=ribbon-consumer2
server.port=9003
spring.cloud.loadbalancer.retry.enabled=true
#注冊中心位址
eureka.client.serviceUrl.defaultZone=http://testhost:8000/eureka/
------------------------------------------------------------------------------------------------------------------
HelloService2.java
@CacheResult
@HystrixCommand(fallbackMethod = "queryUserBackMethod",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000"),
@HystrixProperty(name = "requestCache.enabled", value = "true")
})
public String queryUser2( String id) {
String body = restTemplate
.postForEntity("http://service-provide/queryUser/{1}", String.class, String.class,id)
.getBody();
return body;
}
整體結構:
以上配置完成,啟動注冊中心,啟動服務調用者,啟動服務消費者,啟動Turbine,具體如下:
通路 http://localhost:9004/turbine.stream
重新整理http://localhost:9003/queryUser/5,http://localhost:9002/queryUser/5;讓監控有資料資訊;
通路:http://localhost:9004/hystrix,紅框輸入位址,點選Monitor Stream
微信公衆号