Turbine
在复杂的分布式系统中,相同服务的结点经常需要部署上百甚至上千个,很多时
候,运维人员希望能够把相同服务的节点状态以一个整体集群的形式展现出来,这
样可以更好的把握整个系统的状态。 为此,Netflix提供了一个开源项目
(Turbine)来提供把多个hystrix.stream的内容聚合为一个数据源供Dashboard展
示。
和Hystrix Dashboard一样,Turbine也可以下载war包部署到Web容器,本文不做赘
述。下面讨论Spring Cloud是怎么使用Turbine的。
上手
新建Maven项目,并在pom.xml中添加如下内容:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht
tp://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://m
aven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-hystrix-turbine</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>spring-cloud-microservice-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<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>
</dependencies>
</project>
- 启动类:TurbineApplication.java
/**
* 通过@EnableTurbine接口,激活对Turbine的支持。
* @author eacdy
*/
@SpringBootApplication
@EnableTurbine
public class TurbineApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(TurbineApplication.class).web(true).run(args);
}
}
- 配置文件:application.yml
spring:
application.name: microservice-hystrix-turbine
server:
port: 8031
security.basic.enabled: false
turbine:
aggregator:
clusterConfig: default
# 指定聚合哪些集群,多个使用","分割,默认
# 为default。可使用http://.../turbine.stream?cluster={clusterConfig
# 之一}访问
appConfig: microservice-consumer-movie-feign-with-hystrix-stream,microservice-consumer-movie-ribbon-with-hystrix
### 配置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://discovery:8761/eureka/
### 参考:http://blog.csdn.net/liaokailin/article/details/51344281
### 参考:http://blog.csdn.net/zhuchuangang/article/details/51289
这样一个Turbine微服务就编写完成了。
Turbine测试
- 启动项目:microservice-discovery-eureka
- 启动项目:microservice-provider-user
- 启动项目:microservice-consumer-movie-ribbon-with-hystrix
- 启动项目:microservice-consumer-movie-feign-with-hystrix-stream
- 启动项目:microservice-hystrix-dashboard
- 启动项目:microservice-hystrix-turbine(即本例)
- 访问:http://localhost:8011/ribbon/1,调用ribbon接口
- 访问:http://localhost:8022/feign/1,调用feign接
- 访问:http://localhost:8031/turbine.stream,可查看到和Hystrix监控类似的内容:
data: {"rollingCountFallbackSuccess":0,"rollingCountFallbackFail
ure":0,"propertyValue_circuitBreakerRequestVolumeThreshold":20,"
p
并且会不断刷新以获取实时的监控数据。同样的,我们可以将这些文本内容放入到
Dashboard中展示。
访问Hystrix Dashboard:http://localhost:8030/hystrix.stream,并
将http://localhost:8031/turbine.stream输入到其上的输入框,并随意指定一个
Title,如下图:
将会查看到如下的图表,有图可见,我们把两个项目的API都监控了:
TIPS
- 项目 microservice-consumer-movie-ribbon-with-hystrix 和
microservice-consumer-movie-feign-with-hystrix-stream 需要配置
不同的主机名,并将preferIpAddress
设为false或者不设置,否则将会造成在单
个主机上测试,Turbine只显示一个图表的情况。项目的代码已经修改好并注释
了,这边友情提示一下。 - 配置项: turbine.clusterNameExpression 与turbine.aggregator.clusterConfig 的关系:
具体可以关
注 org.springframework.cloud.netflix.turbine.CommonsInstanceDiscovery
和 org.springframework.cloud.netflix.turbine.EurekaInstanceDiscovery
两个类。特别关注一
下 org.springframework.cloud.netflix.turbine.EurekaInstanceDiscovery.marshall(InstanceInfo) 方法。
参考文档
http://blog.csdn.net/liaokailin/article/details/51344281
http://stackoverflow.com/questions/31468227/whats-for-the-spring-cloudturbine-
clusternameexpression-config-mean