上篇博文中学到了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>
-
-
<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>
-
-
<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.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