SpringCloud框架Turbine服务内容聚合监控

本文介绍如何使用Netflix的开源项目Turbine进行微服务监控,实现将多个Hystrix.stream的内容聚合为单一数据源,便于运维人员整体掌握系统状态。通过Spring Cloud集成Turbine,实现对多个服务实例的状态监控。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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测试

  1. 启动项目:microservice-discovery-eureka
  2. 启动项目:microservice-provider-user
  3. 启动项目:microservice-consumer-movie-ribbon-with-hystrix
  4. 启动项目:microservice-consumer-movie-feign-with-hystrix-stream
  5. 启动项目:microservice-hystrix-dashboard
  6. 启动项目:microservice-hystrix-turbine(即本例)
  7. 访问:http://localhost:8011/ribbon/1,调用ribbon接口
  8. 访问:http://localhost:8022/feign/1,调用feign接
  9. 访问: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

  1. 项目 microservice-consumer-movie-ribbon-with-hystrix 和
    microservice-consumer-movie-feign-with-hystrix-stream 需要配置
    不同的主机名,并将preferIpAddress设为false或者不设置,否则将会造成在单
    个主机上测试,Turbine只显示一个图表的情况。项目的代码已经修改好并注释
    了,这边友情提示一下。
  2. 配置项: 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

coder阿龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值