本文中,我将向你介绍Spring Cloud Netflix Turbine。它将多个Hystrix Metrics Streams 聚合为一个,以便显示在一个仪表板视图中。
简要介绍Hystrix 。 在微服务架构中,咱们有许多小应用程序相互通讯以完成请求。这些下游服务有可能没法正确响应或彻底失败。为了防止发生级联故障,咱们为微服务设置了Hystrix
回退机制。html
每一个实现Hystrix
的微服务均可以选择公开Hystrix Metrics Streams(经过actuator
端点/hystrix.stream
),以便经过Hystrix Dashboard
查看。java
若是您想了解更多信息,我已在Spring Cloud:Hystrix中详细介绍了这一点。git
Turbine是Netflix
的一个开源工具,用于将多个流聚合到一个流中。 Spring
提供了一个很好的包装器,以方便在Spring
生态系统中使用。github
相似于Spring Cloud:Hystrix
的设置,后端服务以下所示:spring
/recommendations
,并在端口8070上运行。/personalized/{id}
,并在端口8060上运行。Hystrix dashboard
服务,用于显示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
咱们能够在Hystrix dashboard
中单独查看这些,方法是在框中键入URL并单击Monitor Stream
便可:
你将看到以下指标(metric):
注意:若是没有看到任何流(stream),那么可能必须点击该stream的服务端点。 例如:对于user-service
,咱们能够点击http://localhost:8060/personalized/1
来生成流。架构
你可能已经意识到,查看单个流(stream)的效率不高,尤为是有许多微服务时。app
Turbine
能够将全部单独的hystrix.stream
聚合成一个turbine.stream
,以便在Hystrix Dashboard
上查看。spring-boot
它使用DiscoveryClient
接口找出生产/hystrix.stream
的相关服务。
要将Turbine
添加到Hystrix dashboard
,请添加如下依赖项:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency>
注意:这是Turbine
的starter
依赖,默认状况下使用Spring Cloud Eureka
做为服务发现。 若是使用的是Spring Cloud Consul
,请使用如下依赖项:
<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 public class HystrixTurbineApplication { public static void 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= new String("default")
在这里,咱们告诉Turbine
Eureka
服务器的位置,以及它须要获取/ hystrix.stream
的应用程序。并将turbine.clusterNameExpression
设为new String("default")
,即默认集群名称为“default”。
咱们能够打开http://localhost:9090/turbine.stream?cluster=default
来查看user-service
和recommendation-service
的聚合流:
一样,若是没有查看到任何内容,只需点击user-service
和recommendation-service
端点便可生成流。
咱们还能够在Hystrix dashboard
上使用此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
视为Eureka ID
,例如:http://localhost:9090/turbine.stream?cluster=USER-SERVICE
若是特定服务的多个实例正在运行,Turbine
将按照集群进行分拣并将其显示在结果中。
在本文中,咱们已经介绍了如何在Hystrix stream
的基础上设置Turbine
以得到聚合视图。咱们首先看到了Turbine
从全部服务中获取Hystrix stream
的经典方法。
与往常同样,本文中使用的示例代码能够在GitHub上找到。
做者:Dhananjay Singh
译者: Leesen