搜索

8.11. Hystrix

download PDF

概述

由 Camel 2.18 提供。

Hystrix 模式可让应用程序与 Netflix Hystrix 集成,这可以在 Camel 路由中提供断路器。Hystrix 是一个延迟和容错库,旨在

  • 隔离对远程系统、服务和第三方库的访问点
  • 停止级联失败
  • 在复杂的分布式系统中启用弹性,其中故障不可避免

如果您使用 maven,请在 pom.xml 文件中添加以下依赖项以使用 Hystrix:

<dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-hystrix</artifactId>
      <version>x.x.x</version>
      <!-- Specify the same version as your Camel core version. -->
</dependency>

Java DSL 示例

以下是一个示例路由,它演示了 Hystrix 端点,通过回退到行的回退路由来防止运行缓慢。默认情况下,超时请求只是 1000ms,因此 HTTP 端点必须相当快速成功。

from("direct:start")
    .hystrix()
        .to("http://fooservice.com/slow")
    .onFallback()
        .transform().constant("Fallback message")
    .end()
    .to("mock:result");

XML 配置示例

以下是相同的示例,但在 XML 中:

<camelContext xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="direct:start"/>
    <hystrix>
      <to uri="http://fooservice.com/slow"/>
      <onFallback>
        <transform>
          <constant>Fallback message</constant>
        </transform>
      </onFallback>
    </hystrix>
    <to uri="mock:result"/>
  </route>
</camelContext>

使用 Hystrix 回退功能

onFallback() 方法用于本地处理,您可以转换消息或调用 bean 或其他内容作为回退。如果您需要通过网络调用外部服务,那么您应该使用 onFallbackViaNetwork() 方法,它在使用其自己的线程池的独立 HystrixCommand 对象中运行,使它不会耗尽第一个命令对象。

Hystrix 配置示例

Hystrix 有许多选项,如下一节中所述。以下示例显示,用于将执行超时设置为 5 秒的 Java DSL,而不是默认的 1 秒,并让断路器等待 10 秒,而不是 5 秒(默认),在状态停止打开前再次尝试请求。

from("direct:start")
    .hystrix()
        .hystrixConfiguration()
             .executionTimeoutInMilliseconds(5000).circuitBreakerSleepWindowInMilliseconds(10000)
        .end()
        .to("http://fooservice.com/slow")
    .onFallback()
        .transform().constant("Fallback message")
    .end()
    .to("mock:result");

以下是相同的示例,但在 XML 中:

<camelContext xmlns="http://camel.apache.org/schema/spring">
<camelContext xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="direct:start"/>
    <hystrix>
      <hystrixConfiguration executionTimeoutInMilliseconds="5000" circuitBreakerSleepWindowInMilliseconds="10000"/>
      <to uri="http://fooservice.com/slow"/>
      <onFallback>
        <transform>
          <constant>Fallback message</constant>
        </transform>
      </onFallback>
    </hystrix>
    <to uri="mock:result"/>
  </route>
</camelContext>
 You can also configure Hystrix globally and then refer to that
configuration. For example:
<camelContext xmlns="http://camel.apache.org/schema/spring">
   <!-- This is a shared config that you can refer to from all Hystrix patterns. -->
   <hystrixConfiguration id="sharedConfig" executionTimeoutInMilliseconds="5000" circuitBreakerSleepWindowInMilliseconds="10000"/>

   <route>
         <from uri="direct:start"/>
         <hystrix hystrixConfigurationRef="sharedConfig">
         <to uri="http://fooservice.com/slow"/>
         <onFallback>
            <transform>
               <constant>Fallback message</constant>
            </transform>
         </onFallback>
      </hystrix>
      <to uri="mock:result"/>
   </route>
</camelContext>

选项

ths Hystrix 组件支持以下选项:Hystrix 提供默认值。

名称默认值类型描述

circuitBreakerEnabled

true

布尔值

决定断路器是否用于跟踪健康和短路请求(如果它出差)。

circuitBreakerErrorThresholdPercentage

50

整数

设置在 或以上处的错误百分比,电路应开路并启动短路到回退逻辑。

circuitBreakerForceClosed

false

布尔值

值 true 会强制断路器进入闭路状态,而无论错误百分比如何,它都允许请求请求。

circuitBreakerForceOpen

false

布尔值

值 true 会强制断路器进入 open(出)状态,在其中拒绝所有请求。

circuitBreakerRequestVolumeThreshold

20

整数

设置出电路在滚动窗口中的最小请求数。

circuitBreakerSleepWindownInMilliseconds

5000

整数

设置断路器以拒绝请求的时间。在此时间过后,允许请求尝试来确定电路是否应该再次关闭。

commandKey

节点 ID

字符串

标识 Hystrix 命令。您无法配置这个选项。它始终是节点 ID,以使命令唯一。

corePoolSize

10

整数

设置核心 thread-pool 大小。这是可以同时执行的最大 HystrixCommand 对象数。

executionIsolationSemaphoreMaxConcurrentRequests

10

整数

在使用 ExecutionIsolationStrategy.SEMAPHORE 时,设置 HystrixCommand.run() 方法可以进行的最大请求数。

executionIsolationStrategy

线程

字符串

指示通过以下哪个隔离策略 HystrixCommand.run() 执行。THREAD 在单独的线程上执行,并发请求受 thread-pool 中的线程数量的限制。SEMAPHORE 在调用线程上执行,且并发请求受 semaphore 数量的限制:

executionIsolationThreadInterruptOnTimeout

true

布尔值

指明在超时发生时是否应该中断 HystrixCommand.run() 执行。

executionTimeoutInMilliseconds

1000

整数

为执行完成设置超时(以毫秒为单位)。

executionTimeoutEnabled

true

布尔值

指明是否应该 timed HystrixCommand.run() 执行。

fallbackEnabled

true

布尔值

决定在发生故障或拒绝时是否尝试调用 HystrixCommand.getFallback()

fallbackIsolationSemaphoreMaxConcurrentRequests

10

整数

设置 HystrixCommand.getFallback() 方法从调用线程发出的最大请求数。

groupKey

CamelHystrix

字符串

标识正在用来关联统计数据和断路器属性的 Hystrix 组。

keepAliveTime

1

整数

以分钟为单位设置 keep-alive 时间。

maxQueueSize

-1

整数

设置 BlockingQueue 实施的最大队列大小。

metricsHealthSnapshotIntervalInMilliseconds

500

整数

在允许执行运行状况快照之间设置要等待的时间(以毫秒为单位)。健康快照计算成功和错误百分比,并影响断路器状态。

metricsRollingPercentileBucketSize

100

整数

设置每个存储桶保留的最大执行时间。如果在这段时间内进行更多执行,它们将换行,并在存储桶开始时进行覆盖。

metricsRollingPercentileEnabled

true

布尔值

指明是否应该跟踪执行延迟。延迟计算为百分比。值 false 会导致概述统计(mean,百分比数)返回为 -1。

metricsRollingPercentileWindowBuckets

6

整数

设置 rollingPercentile 窗口将被分成的 bucket 数量。

metricsRollingPercentileWindowInMilliseconds

60000

整数

设置执行时间的滚动窗口的持续时间,以便允许达到 50% 的计算时间(以毫秒为单位)。

metricsRollingStatisticalWindowBuckets

10

整数

设置滚动统计窗口的存储桶数量被分成.

metricsRollingStatisticalWindowInMilliseconds

10000

整数

这个选项和以下选项适用于从 HystrixCommandHystrixObservableCommand 执行捕获指标。

queueSizeRejectionThreshold

5

整数

设定队列大小拒绝阈值 - 智能的最大队列大小,即使尚未达到maxQueueSize 也是如此。

requestLogEnabled

true

布尔值

指明 HystrixCommand 执行和事件应记录到 HystrixRequestLog

threadPoolKey

null

字符串

定义此命令应在其中运行的 thread-pool。默认情况下,这使用与 group 键相同的密钥。

threadPoolMetricsRollingStatisticalWindowBucket

10

整数

设置滚动统计窗口的存储桶数量被分成.

threadPoolMetricsRollingStatisticalWindowInMilliseconds

10000

整数

设置统计滚动窗口的持续时间,以毫秒为单位。这是为线程池保留指标的时长。

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.