搜索

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 提供默认值。

Name默认值类型描述

circuitBreakerEnabled

true

布尔值

确定断路器是否用于跟踪健康以及短路请求(如果被行程)。

circuitBreakerErrorThresholdPercentage

50

整数

设置在或上面的错误百分比,使电路应打开并启动对回退逻辑的短路请求。

circuitBreakerForceClosed

false

布尔值

值 true 会强制断路器进入闭状态,其允许请求而不考虑错误百分比。

circuitBreakerForceOpen

false

布尔值

true 值强制断路器进入开放(往返)状态,其中拒绝所有请求。

circuitBreakerRequestVolumeThreshold

20

整数

在滚动窗口中设置将遍历电路的最少请求数。

circuitBreakerSleepWindownInMilliseconds

5000

整数

设置在传输电路以拒绝请求后的时间长度。在这个时间过后,允许请求尝试确定电路是否应再次关闭。

commandKey

节点 ID

字符串

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

corePoolSize

10

整数

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

executionIsolationSemaphoreMaxConcurrentRequests

10

整数

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

executionIsolationStrategy

THREAD

字符串

指明执行哪些隔离策略 HystrixCommand.run ()THREAD 对单独的线程和并发请求执行受 thread-pool 中的线程数量的限制。SEMAPHORE 对调用线程和并发请求执行受 semaphore 计数的限制:

executionIsolationThreadInterruptOnTimeout

true

布尔值

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

executionTimeoutInMilliseconds

1000

整数

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

executionTimeoutEnabled

true

布尔值

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

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 会导致摘要统计(百分比)返回为 -1。

metricsRollingPercentileWindowBuckets

6

整数

设置 rollingPercentile 窗口将划分为的 bucket 数量。

metricsRollingPercentileWindowInMilliseconds

60000

整数

设置滚动窗口的持续时间,在其中保留执行时间,允许有百分比的计算(以毫秒为单位)。

metricsRollingStatisticalWindowBuckets

10

整数

设置滚动统计窗口划分为的 bucket 数量。

metricsRollingStatisticalWindowInMilliseconds

10000

整数

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

queueSizeRejectionThreshold

5

整数

设置队列大小 rejection 阈值 - 人工最大队列大小,即使 swigmaxQueueSize 尚未达到拒绝,也会发生拒绝。

requestLogEnabled

true

布尔值

指明 HystrixCommand 执行,事件是否应记录到 HystrixRequestLog

threadPoolKey

null

字符串

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

threadPoolMetricsRollingStatisticalWindowBucket

10

整数

设置滚动统计窗口划分为的 bucket 数量。

threadPoolMetricsRollingStatisticalWindowInMilliseconds

10000

整数

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

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

© 2024 Red Hat, Inc.