本文中,我将向你介绍 Spring Cloud Netflix Turbine 。它将多个Hystrix Metrics Streams 聚合为一个,以便显示在一个仪表板视图中。 简要介绍 Hystrix 。 在微服务架构中,我们有许多小应用程序相互通信以完成请求。这些下游服务有可能无法正确响应或完全失败。为了防止发生级联故障,我们为微服务设置了 Hystrix 回退机制。
每个实现 Hystrix 的微服务都可以选择公开 Hystrix Metrics Streams (通过 actuator 端点 /hystrix.stream ),以便通过 Hystrix Dashboard 查看。
如果您想了解更多信息,我已在 Spring Cloud:Hystrix 中详细介绍了这一点。
Turbine 是 Netflix 的一个开源工具,用于将多个流聚合到一个流中。 Spring 提供了一个很好的包装器,以方便在 Spring 生态系统中使用。
类似于 Spring Cloud:Hystrix 的设置,后端服务如下所示:
/recommendations
/personalized/{id}
Hystrix dashboard
以下是我们在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):
user-service ,我们可以点击
http://localhost:8060/personalized/1
来生成流。
你可能已经意识到,查看单个流(stream)的效率不高,尤其是有许多微服务时。
Turbine 可以将所有单独的 hystrix.stream 聚合成一个 turbine.stream ,以便在 Hystrix Dashboard 上查看。
它使用 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 上找到。
原文链接: stackabuse.com/spring-clou…
作者:Dhananjay Singh
译者: Leesen