Fork me on GitHub

熔断器 - Turbine

目录

Turbine

在复杂的分布式系统中,相同服务的节点经常需要部署上百甚至上千个,很多时候,运维人员希望能够把相同服务的节点状态以一个整体集群的形式展现出来,这样可以更好的把握整个系统的状态。 为此,Netflix提供了一个开源项目(Turbine)来提供把多个hystrix.stream的内容聚合为一个数据源供Dashboard展示。

和Hystrix Dashboard一样,Turbine也可以下载war包部署到Web容器,本文不做赘述。下面讨论Spring Cloud是怎么使用Turbine的。

代码示例

新建Maven项目,并在pom.xml中添加如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-hystrix-turbine</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>cn.cicoding.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

1
2
3
4
5
6
7
8
9
10
11
/**
* 通过@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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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/51289593

这样一个Turbine微服务就编写完成了。

Turbine测试

  1. 启动项目:microservice-discovery-eureka
  2. 启动项目:microservice-provider
  3. 启动项目:microservice-consumer-ribbon-with-hystrix
  4. 启动项目:microservice-consumer-movie-feign-hystrix-stream
  5. 启动项目:microservice-hystrix-dashboard
  6. 启动项目:microservice-hystrix-turbine(即本例)
  7. 访问:http://localhost:8020/fegin/user/1 ,调用feign接口
  8. 访问:http://localhost:8010/ribbon/user/1 /1,调用ribbon接口
  9. 访问:http://localhost:8031/turbine.stream ,可查看到和Hystrix监控类似的内容:
1
data: {"rollingCountFallbackSuccess":0,"rollingCountFallbackFailure":0,"propertyValue_circuitBreakerRequestVolumeThreshold":20,"p

并且会不断刷新以获取实时的监控数据。同样的,我们可以将这些文本内容放入到Dashboard中展示。

访问Hystrix Dashboard:http://localhost:8030/hystrix.stream ,并将http://localhost:8031/turbine.stream 输入到其上的输入框,并随意指定一个Title,如下图:

熔断器-Turbine

将会查看到如下的图表,有图可见,我们把两个项目的API都监控了:

熔断器-Turbine

TIPS

  1. 项目microservice-consumer-movie-ribbon-with-hystrixmicroservice-consumer-movie-feign-with-hystrix-stream 需要配置不同的主机名,并将preferIpAddress设为false或者不设置,否则将会造成在单个主机上测试,Turbine只显示一个图表的情况。项目的代码已经修改好并注释了,这边友情提示一下。
  2. 配置项:turbine.clusterNameExpressionturbine.aggregator.clusterConfig的关系:
turbine.clusterNameExpression取值 turbine.aggregator.clusterConfig 取值
默认(appName) 配置想要聚合的项目,此时使用turbine.stream?cluster=项目名称大写访问监控数据
new String(“default”) 或者”‘default’” 不配置,或者配置default,因为默认就是default
metadata[‘cluster’];同时待监控的项目配置了类似:eureka.instance.metadata-map.cluster: ABC 也设成ABC,需要和待监控的项目配置的eureka.instance.metadata-map.cluster一致。

具体可以关注org.springframework.cloud.netflix.turbine.CommonsInstanceDiscoveryorg.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-cloud-turbine-clusternameexpression-config-mean

相关文章

微信打赏

赞赏是不耍流氓的鼓励