15. Hystrix 超时和 Ribbon Clients

使用包装 Ribbon clients 的 Hystrix 命令时,要确保将 Hystrix 超时配置为长于配置的 Ribbon 超时,包括可能进行的任何可能的重试。例如,如果您的 Ribbon 连接超时为一秒且 Ribbon client 可能会重试请求三次,那么 Hystrix 超时应该略大于三秒。

15.1 如何包含 Hystrix 仪表板

要在项目中包含 Hystrix 仪表板,请使用 group ID 为org.springframework.cloud且 artifact ID 为spring-cloud-starter-netflix-hystrix-dashboard的 starter。有关使用当前 Spring Cloud Release Train 设置 build 系统的详细信息,请参阅Spring Cloud 项目页面

要 run Hystrix 仪表板,请使用@EnableHystrixDashboard注释 Spring Boot main class。然后访问/hystrix并将仪表板指向 Hystrix client application 中的单个实例的/hystrix.stream端点。

当连接到使用 HTTPS 的/hystrix.stream端点时,服务器使用的证书必须由 JVM 信任。如果证书不受信任,则必须将证书导入 order 中的 JVM,以便 Hystrix 仪表板成功连接到流端点。

15.2 Turbine

查看单个实例的 Hystrix 数据在系统的整体运行状况方面不是很有用。 涡轮是一个 application,它将所有相关的/hystrix.stream endpoints 聚合为/turbine.stream组合,以便在 Hystrix 仪表板中使用。个别实例位于 Eureka。 Running Turbine 需要使用@EnableTurbine annotation 注释主 class(对于 example,使用 spring-cloud-starter-netflix-turbine 来设置 classpath)。来自Turbine 1 维基的所有记录的 configuration properties 都适用。唯一的区别是turbine.instanceUrlSuffix不需要 port 前置,因为这是自动处理的,除非turbine.instanceInsertPort=false

默认情况下,Turbine 通过查找 Eureka 中的hostNameport条目然后将/hystrix.stream附加到其上来查找已注册实例上的/hystrix.stream端点。如果实例的元数据包含management.port,则使用它来代替/hystrix.stream端点的port value。默认情况下,名为management.port的元数据条目等于management.port configuration property。可以使用以下 configuration 覆盖它:

eureka:
  instance:
    metadata-map:
      management.port: ${management.port:8081}

turbine.appConfig configuration key 是_tureine 用于查找实例的 Eureka serviceIds 列表。然后在 Hystrix 仪表板中使用 turbine 流,其 URL 类似于以下内容:

http://my.turbine.server:8080/turbine.stream?cluster=CLUSTERNAME

如果 name 是default,则可以省略 cluster 参数。 cluster参数必须_匹配turbine.aggregator.clusterConfig中的条目。 Eureka 返回的值为 upper-case。因此,如果在 Eureka 中注册了一个名为customers的_app_,则以下示例有效:

turbine:
  aggregator:
    clusterConfig: CUSTOMERS
  appConfig: customers

如果需要自定义 Turbine 应使用哪些 cluster 名称(因为您不想在turbine.aggregator.clusterConfig configuration 中 store cluster 名称),请提供类型的 bean。

clusterName可以通过turbine.clusterNameExpression中的 SPEL 表达式自定义,其中 root 用作InstanceInfo的实例。默认的 value 是appName,这意味着 Eureka serviceId成为 cluster key(即,InstanceInfo for customers 的appNameCUSTOMERS)。另一个 example 是turbine.clusterNameExpression=aSGName,它从 AWS ASG name 获取 cluster name。以下清单显示了另一个 example:

turbine:
  aggregator:
    clusterConfig: SYSTEM,USER
  appConfig: customers,stores,ui,admin
  clusterNameExpression: metadata['cluster']

在上面的示例中,来自四个服务的 cluster name 是从其元数据 map 中提取的,并且应该具有包含SYSTEMUSER的值。

要为所有应用程序使用“默认”cluster,您需要一个 string 文字表达式(如果它在 YAML 中,则使用单引号并使用 double 引号进行转义):

turbine:
  appConfig: customers,stores
  clusterNameExpression: "'default'"

Spring Cloud 提供spring-cloud-starter-netflix-turbine,它具有获取 Turbine 服务器 running 所需的所有依赖项。要广告 Turnbine,请创建 Spring Boot application 并使用@EnableTurbine注释它。

默认情况下,Spring Cloud 允许 Turbine 使用 host 和 port 为每个 host 提供多个进程,每个 cluster。如果您希望 Turbine 中内置的本机 Netflix 行为不允许每个 host 的多个进程,每 cluster(实例 ID 的 key 是主机名),请设置turbine.combineHostPort=false

15.2.1 集群端点

在某些情况下,其他应用程序可能有助于了解 Turbine 中已配置的集群。为了支持这一点,您可以使用/clusters端点,该端点将返回所有已配置集群的 JSON array。

GET /clusters.

[
  {
    "name": "RACES",
    "link": "http://localhost:8383/turbine.stream?cluster=RACES"
  },
  {
    "name": "WEB",
    "link": "http://localhost:8383/turbine.stream?cluster=WEB"
  }
]

可以通过将turbine.endpoints.clusters.enabled设置为false来禁用此端点。

15.3 Turbine Stream

在某些环境中(例如在 PaaS 设置中),从所有分布式 Hystrix 命令中拉出 metrics 的经典 Turbine model 不起作用。在这种情况下,您可能希望让 Hystrix 命令将 metrics 推送到 Turbine。 Spring Cloud 通过消息传递实现。要在 client 上执行此操作,请为spring-cloud-netflix-hystrix-stream和您选择的spring-cloud-starter-stream-*添加依赖项。有关代理的详细信息以及如何配置 client 凭据,请参阅Spring Cloud Stream 文档。它应该为本地 broker 开箱即用。

在服务器端,创建一个 Spring Boot application 并使用@EnableTurbineStream注释它。 Turbine Stream 服务器需要使用 Spring Webflux,因此spring-boot-starter-webflux需要包含在您的项目中。默认情况下,将spring-cloud-starter-netflix-turbine-stream添加到 application 时会包含spring-boot-starter-webflux

然后,您可以将 Hystrix 仪表板指向 Turbine Stream Server 而不是单个 Hystrix 流。如果 Turbine Stream 在 myhost 上 port 8989 上运行,则将http://myhost:8989放在 Hystrix 仪表板的流输入字段中。电路的前缀为各自的serviceId,后跟一个点(.),然后是电路 name。

Spring Cloud 提供spring-cloud-starter-netflix-turbine-stream,它具有获取 Turbine Stream 服务器 running 所需的所有依赖项。然后,您可以添加您选择的 Stream binder - 例如spring-cloud-starter-stream-rabbit

Turbine Stream 服务器还支持cluster参数。与 Turbine 服务器不同,Turbine Stream 使用 eureka serviceIds 作为 cluster 名称,这些不可配置。

如果 Turbine Stream 服务器在my.turbine.server上 port 8989 上运行并且您的环境中有两个 eureka serviceIds customersproducts,则 Turbine Stream 服务器上将提供以下 URL。 default和空 cluster name 将提供 Turbine Stream 服务器接收的所有 metrics。

http://my.turbine.sever:8989/turbine.stream?cluster=customers
http://my.turbine.sever:8989/turbine.stream?cluster=products
http://my.turbine.sever:8989/turbine.stream?cluster=default
http://my.turbine.sever:8989/turbine.stream

因此,您可以将 eureka serviceIds 用作 Turbine 仪表板(或任何兼容的仪表板)的 cluster 名称。您不需要为 Turbine Stream 服务器配置turbine.appConfigturbine.clusterNameExpressionturbine.aggregator.clusterConfig等任何 properties。

Turbine Stream 服务器使用 Spring Cloud Stream 从配置的输入 channel 收集所有 metrics。这意味着它不会从每个实例主动收集 Hystrix metrics。它只能提供已经由每个实例收集到输入 channel 中的 metrics。