Spring Cloud :Turbine
点击左上角,关注:“锅外的大佬”
专注分享国外最新技术内容帮助每位开发者更优秀地成长
1.概述
本文中,我将向你介绍Spring Cloud Netflix Turbine。它将多个Hystrix Metrics Streams 聚合为一个,以便显示在一个仪表板视图中。 简要介绍Hystrix 。 在微服务架构中,我们有许多小应用程序相互通信以完成请求。这些下游服务有可能无法正确响应或完全失败。为了防止发生级联故障,我们为微服务设置了
Hystrix
回退机制。每个实现
Hystrix
的微服务都可以选择公开Hystrix Metrics Streams(通过 actuator
端点 /hystrix.stream
),以便通过 HystrixDashboard
查看。如果您想了解更多信息,我已在Spring Cloud:Hystrix中详细介绍了这一点。
Turbine是
Netflix
的一个开源工具,用于将多个流聚合到一个流中。 Spring
提供了一个很好的包装器,以方便在 Spring
生态系统中使用。2.搭建
类似于
SpringCloud:Hystrix
的设置,后端服务如下所示:- Eureka Server :作为服务注册运行并在端口8761上运行。
- 推荐服务:一个简单的REST服务,只有一个端点:
/recommendations
,并在端口8070上运行。 - 用户服务:一个简单的REST服务,单个端点为:
/personalized/{id}
,并在端口8060上运行。 - Hystrix Turbine :
Hystrixdashboard
服务,用于显示Hystrix流,并在端口'9090'上运行。
以下是我们在Eureka服务器上看到的服务列表:
user-service
和
recommendation-service
都实现了
Hystrix
回退机制,并通过Actuator暴露了
/hystrix.stream
端点:
- 用户服务的
Hystrix
端点:http://localhost:8060/actuator/hystrix.stream
- 用于推荐服务的
Hystrix
端点:http://localhost:8070/actuator/hystrix.stream
我们可以在
Hystrixdashboard
中单独查看这些,方法是在框中键入URL并单击 MonitorStream
即可:你将看到如下指标(metric):
注意:如果没有看到任何流(stream),那么可能必须点击该stream的服务端点。 例如:对于
user-service
,我们可以点击
http://localhost:8060/personalized/1
来生成流。
3.安装Turbine
你可能已经意识到,查看单个流(stream)的效率不高,尤其是有许多微服务时。
Turbine
可以将所有单独的 hystrix.stream
聚合成一个 turbine.stream
,以便在 HystrixDashboard
上查看。它使用
DiscoveryClient
接口找出生产 /hystrix.stream
的相关服务。要将
Turbine
添加到 Hystrixdashboard
,请添加以下依赖项:<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
注意:这是
Turbine
的 starter
依赖,默认情况下使用 SpringCloudEureka
作为服务发现。 如果使用的是 SpringCloudConsul
,请使用以下依赖项:<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
在本文中,我们将使用starter依赖,即
spring-cloud-starter-netflix-turbine
。要启用
Turbine
,只需使用 @EnableTurbine
注解主类:@SpringBootApplication
@EnableTurbine
@EnableDiscoveryClient
@EnableHystrixDashboard
publicclassHystrixTurbineApplication{
publicstaticvoid main(String[] args){
SpringApplication.run(HystrixTurbineApplication.class, args);
}
}
为了使
Turbine
按预期工作,我们必须在 application.properties
中添加一些细节:server.port=9090
spring.application.name= hystirx-turbine
eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/
turbine.appConfig= user-service,recommendation-service
turbine.clusterNameExpression=newString("default")
在这里,我们告诉
Turbine
Eureka
服务器的位置,以及它需要获取 /hystrix.stream
的应用程序。并将 turbine.clusterNameExpression
设为 newString("default")
,即默认集群名称为“default”。我们可以打开
http://localhost:9090/turbine.stream?cluster=default
来查看 user-service
和 recommendation-service
的聚合流:同样,如果没有查看到任何内容,只需点击
user-service
和 recommendation-service
端点即可生成流。我们还可以在
Hystrixdashboard
上使用此URL来生成一个很好的聚合视图:有时,您可能希望将
Eureka
的
serviceId
用作
dashboard
的集群名称。这可以通过设置
turbine.aggregator.clusterConfig
来完成:
server.port =9090
spring.application.name = hystirx-turbine
eureka.client.serviceUrl.defaultZone = http:// localhost:8761 / eureka /
turbine.aggregator.clusterConfig = USER-SERVICE,RECOMMENDATION-SERVICE
turbine.appConfig =用户服务,推荐服务
您还可以通过点击
/clusters
端点来检查 Turbine
应用程序中当前已配置的集群。可以通过将
turbine.endpoints.clusters.enabled
设置为 false
来禁用此端点。所以,现在我们可以将
turbine.stream
视为
EurekaID
,例如:
http://localhost:9090/turbine.stream?cluster=USER-SERVICE
如果特定服务的多个实例正在运行,
Turbine
将按照集群进行分拣并将其显示在结果中。4.总结
在本文中,我们已经介绍了如何在
Hystrixstream
的基础上设置 Turbine
以获得聚合视图。我们首先看到了 Turbine
从所有服务中获取 Hystrixstream
的经典方法。与往常一样,本文中使用的示例代码可以在GitHub上找到。
原文链接:https://stackabuse.com/spring-cloud-turbine/作者: Dhananjay Singh译者: Leesen
上篇好文:测试时设置Spring Boot日志级别
点击在看,和我一起帮助更多开发者!
阅读原文 最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。