Spring Cloud Netflix 熔断组件 —Hystrix @HystrixCommand配置详解

在微服务架构中, 我们将系统拆分成了很多服务单元,各个服务之间通过远程调用的方式执行,这样就有可能因为网络原因或其他原因出现调用故障或延迟,而这些问题会直接导致调用方的对外服务也出现延迟,若此时调用方的请求不断增加, 最后就会因等待出现故障的依赖方响应形成任务积压,最终导致自身服务的瘫痪。针对上述问题, Spring Cloud Hystrix实现了断路器、 线程隔离等一系列服务保护功能。它也是基于Netflix的开源框架Hystrix实现的。

在上一篇我们使用Hysttrix编写了一个简单的案例,在调用失败时调用fallMethod方法中主要使用了@HystrixCommand注解并讲述了其中的属性,下面我们将详细介绍@HystrixCommand注解中的属性commandProperties和threadPoolProperties的具体使用方式。从源码中我们知道这两个配置配置的是HystrixProperty实例,我们查看HystrixProperty源码如下:

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface HystrixProperty {
    String name();
    String value();
}

从源码可以知道它包含一个name和value属性,我们需要配置的也是name和value属性,下面我们介绍一些常用的HystrixProperties属性值。

commandProperties配置

commandProperties属性用于控制HystrixCommand的行为,其中包括Execution用于控制HystrixCommand.run()的执行,

execution.isolation.strategy配置用于配置执行的时候使用哪个隔离策略,它有两个策略THREAD,它在单独的线程上执行,并发请求受线程池中线程数的限制和SEMAPHORE,它在调用线程上执行,并发请求受信号量限制。详细使用如下表格:

配置名称

execution.isolation.strategy

默认值

THREAD

可选值

SEMAPHORE

默认属性

hystrix.command.default.execution.isolation.strategy

实例属性

hystrix.command.HystrixCommandKey.execution.isolation.strategy

execution.isolation.thread.timeoutInMilliseconds,该属性设置超时时间(以毫秒为单位),在此时间之后调用方将观察到超时并退出命令执行。Hystrix将标记HystrixCommand为TIMEOUT,并执行后备逻辑。

配置名称

execution.isolation.thread.timeoutInMilliseconds

默认值

1000

可选值

整形

默认属性

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds

实例属性

hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds

 

execution.timeout.enabled,此属性指示HystrixCommand.run()执行是否允许超时。

配置名称

execution.timeout.enabled

默认值

true

可选值

false

默认属性

hystrix.command.default.execution.timeout.enabled

实例属性

hystrix.command.HystrixCommandKey.execution.timeout.enabled

execution.isolation.thread.interruptOnTimeout,此属性表示HystrixCommand.run()在发生超时时是否应中断执行。

配置名称

execution.isolation.thread.interruptOnTimeout

默认值

true

可选值

false

默认属性

hystrix.command.default.execution.isolation.thread.interruptOnTimeout

实例属性

hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnTimeout

execution.isolation.thread.interruptOnCancel,此属性表示HystrixCommand.run()在发生取消时是否应中断执行。

配置名称

execution.isolation.thread.interruptOnCancel

默认值

true

可选值

false

默认属性

hystrix.command.default.execution.isolation.thread.interruptOnCancel

实例属性

hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnCancel

execution.isolation.semaphore.maxConcurrentRequests该配置用于当隔离策略是SEMAPHORE时HystrixCommand.run()方法所执行的最大请求数,如果达到此最大并发限制,则后续请求将被拒绝。

配置名称

execution.isolation.semaphore.maxConcurrentRequests,

默认值

10

可选值

整型

默认属性

hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests

实例属性

hystrix.command.HystrixCommandKey.execution.isolation.semaphore.maxConcurrentRequests

 

fallBack用于控制 HystrixCommand.getFallback()的执行行为,它有一下两个属性:

fallback.isolation.semaphore.maxConcurrentRequests,该属性设置getFallBack的最大请求数,如果达到最大并发限制,则后续请求将被拒绝并引发异常,因为无法查询到getFallBack。

配置名称

fallback.isolation.semaphore.maxConcurrentRequests

默认值

10

可选值

整型

默认属性

hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests

实例属性

hystrix.command.HystrixCommandKey.fallback.isolation.semaphore.maxConcurrentRequests

fallback.enabled,此属性确定HystrixCommand.getFallback()在发生故障或拒绝时是否被调用

配置名称

fallback.enabled

默认值

true

可选值

false

默认属性

hystrix.command.default.fallback.enabled

实例属性

hystrix.command.HystrixCommandKey.fallback.enabled

Circuit Breaker用于控制HystrixCircuitBreaker的执行行为,它有以下6个属性

circuitBreaker.enabled,该属性用于开启和关闭HystrixCircuitBreaker,如下表格为详细用法

配置名称

circuitBreaker.enabled

默认值

true

可选值

false

默认属性

hystrix.command.default.circuitBreaker.enabled

实例属性

hystrix.command.HystrixCommandKey.circuitBreaker.enabled

 

circuitBreaker.requestVolumeThreshold此属性在滚动窗口中设置将使电路跳闸的最小请求数。

配置名称

circuitBreaker.requestVolumeThreshold

默认值

20

可选值

整型

默认属性

hystrix.command.default.circuitBreaker.requestVolumeThreshold

实例属性

hystrix.command.HystrixCommandKey.circuitBreaker.requestVolumeThreshold

circuitBreaker.sleepWindowInMilliseconds,此属性设置电路跳闸后拒绝请求的时间,然后允许再次尝试确定是否应再次闭合电路。

配置名称

circuitBreaker.requestVolumeThreshold

默认值

20

可选值

整型

默认属性

hystrix.command.default.circuitBreaker.requestVolumeThreshold

实例属性

hystrix.command.HystrixCommandKey.circuitBreaker.requestVolumeThreshold

circuitBreaker.errorThresholdPercentage,此属性设置错误百分比,电路应在该百分比或以上跳闸,并启动对后备逻辑的短路请求。

配置名称

circuitBreaker.errorThresholdPercentage

默认值

50

可选值

整型

默认属性

hystrix.command.default.circuitBreaker.errorThresholdPercentage

实例属性

hystrix.command.HystrixCommandKey.circuitBreaker.errorThresholdPercentage

circuitBreaker.forceOpen,如果该属性为ture,circuitBreaker将拒绝所有的请求

配置名称

circuitBreaker.forceOpen

默认值

false

可选值

true

默认属性

hystrix.command.default.circuitBreaker.forceOpen

实例属性

hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen

circuitBreaker.forceClosed,如果该属性为true,circuitBreaker将接收所有的请求而不在管百分比参数

配置名称

circuitBreaker.forceClosed

默认值

false

可选值

true

默认属性

hystrix.command.default.circuitBreaker.forceClosed

实例属性

hystrix.command.HystrixCommandKey.circuitBreaker.forceClosed

 

以下属性与从HystrixCommand和HystrixObservableCommand执行的捕获指标有关。

metrics.rollingStats.timeInMilliseconds,此属性设置统计滚动窗口的持续时间(毫秒)。

这就是Hystrix为circuit breaker保留指标值的时间,供断路器使用和发布。

配置名称

metrics.rollingStats.timeInMilliseconds

默认值

10000

可选值

整型

默认属性

hystrix.command.default.metrics.rollingStats.timeInMilliseconds

实例属性

hystrix.command.HystrixCommandKey.metrics.rollingStats.timeInMilliseconds

 

metrics.rollingStats.numBuckets,此属性设置滚动统计窗口划分为的存储桶数。

配置名称

metrics.rollingStats.numBuckets

默认值

10

可选值

整型timeInMilliseconds %numBuckets !=0

默认属性

hystrix.command.default.metrics.rollingStats.numBuckets

实例属性

hystrix.command.HystrixCommandKey.metrics.rollingStats.numBuckets

metrics.rollingPercentile.enabled,此属性指示是否应跟踪执行延迟并将其计算为百分数。如果禁用它们,则所有摘要统计信息(平均值,百分位数)都将返回-1。

配置名称

metrics.rollingPercentile.enabled

默认值

true

可选值

false

默认属性

hystrix.command.default.metrics.rollingPercentile.enabled

实例属性

hystrix.command.HystrixCommandKey.metrics.rollingPercentile.enabled

metrics.rollingPercentile.timeInMilliseconds,此属性设置滚动窗口的持续时间,其中保留执行时间以允许百分位计算,以毫秒为单位。

配置名称

metrics.rollingPercentile.timeInMilliseconds

默认值

60000

可选值

整型

默认属性

hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds

实例属性

hystrix.command.HystrixCommandKey.metrics.rollingPercentile.timeInMilliseconds

metrics.rollingPercentile.numBuckets,此属性设置rollingPercentile窗口将被划分为的存储桶数。

配置名称

metrics.rollingPercentile.numBuckets

默认值

6

可选值

整型timeInMilliseconds %numBuckets !=0

默认属性

hystrix.command.default.metrics.rollingPercentile.numBuckets

实例属性

hystrix.command.HystrixCommandKey.metrics.rollingPercentile.numBuckets

metrics.rollingPercentile.bucketSize,此属性设置每个存储区保留的最大执行时间。如果在这段时间内发生了更多的执行,它们将环绕并在存储桶的开头开始覆盖。

配置名称

metrics.rollingPercentile.bucketSize

默认值

100

可选值

整型

默认属性

hystrix.command.default.metrics.rollingPercentile.bucketSize

实例属性

hystrix.command.HystrixCommandKey.metrics.rollingPercentile.bucketSize

metrics.healthSnapshot.intervalInMilliseconds,此属性设置允许进行运行状况快照之间的等待时间(以毫秒为单位),以运行状况快照来计算成功率和错误率百分比并影响断路器状态。

配置名称

metrics.healthSnapshot.intervalInMilliseconds

默认值

500

可选值

整型

默认属性

hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds

实例属性

hystrix.command.HystrixCommandKey.metrics.healthSnapshot.intervalInMilliseconds

下面的属性跟HystrixCommand使用的HystrixRequestContext有关。

requestCache.enabled,此属性表示是否HystrixCommand.getCacheKey()应与HystrixRequestCache一起使用,以通过请求范围缓存提供重复数据消除功能。

配置名称

requestCache.enabled

默认值

true

可选值

false

默认属性

hystrix.command.default.requestCache.enabled

实例属性

hystrix.command.HystrixCommandKey.requestCache.enabled

requestLog.enabled,此属性表示是否应将HystrixCommand执行和事件记录到HystrixRequestLog。

配置名称

requestLog.enabled

默认值

true

可选值

false

默认属性

hystrix.command.default.requestLog.enabled

实例属性

hystrix.command.HystrixCommandKey.requestLog.enabled

 以上是跟commandProperties相关的配置,下面将介绍与threadPoolProperties相关的配置,以下配置用于控制HystrixCommand所执行的线程池。

coreSize,该属性表示线程池核心数量

配置名称

coreSize

默认值

10

可选值

整型

默认属性

hystrix.threadpool.default.coreSize

实例属性

hystrix.threadpool.HystrixCommandKey.coreSize

maximumSize,该属性表示线程池的最大数量

配置名称

maximumSize

默认值

10

可选值

整型

默认属性

hystrix.threadpool.default.maximumSize

实例属性

hystrix.threadpool.HystrixCommandKey.maximumSize

maxQueueSize,该属性设置BlockingQueue的实现的队列的最大大小,如果设置为-1将使用SynchronousQueue否则使用LinkedBlockingQueue

配置名称

maxQueueSize

默认值

-1

可选值

整型

默认属性

hystrix.threadpool.default.maxQueueSize

实例属性

hystrix.threadpool.HystrixCommandKey.maxQueueSize

queueSizeRejectionThreshold,此属性设置队列大小拒绝阈值,HystrixCommand在排队执行线程时使用。此属性不适用maxQueueSize == -1。

配置名称

queueSizeRejectionThreshold

默认值

5

可选值

整型

默认属性

hystrix.threadpool.default.queueSizeRejectionThreshold

实例属性

hystrix.threadpool.HystrixCommandKey.queueSizeRejectionThreshold

 

keepAliveTimeMinutes,此属性以分钟为单位设置保持活动时间。

配置名称

keepAliveTimeMinutes

默认值

1

可选值

整型

默认属性

hystrix.threadpool.default.keepAliveTimeMinutes

实例属性

hystrix.threadpool.HystrixCommandKey.keepAliveTimeMinutes

allowMaximumSizeToDivergeFromCoreSize,此属性使配置maximumSize生效。该值可以等于或大于coreSize。

配置名称

allowMaximumSizeToDivergeFromCoreSize

默认值

false

可选值

true

默认属性

hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize

实例属性

hystrix.threadpool.HystrixCommandKey.allowMaximumSizeToDivergeFromCoreSize

metrics.rollingStats.timeInMilliseconds,此属性设置统计滚动窗口的持续时间(以毫秒为单位)。这是线程池指标标准保留的时间。

配置名称

metrics.rollingStats.timeInMilliseconds

默认值

10000

可选值

整型

默认属性

hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds

实例属性

hystrix.threadpool.HystrixCommandKey.metrics.rollingStats.timeInMilliseconds

metrics.rollingStats.numBuckets,此属性设置滚动统计窗口分为的存储分区数。

配置名称

metrics.rollingStats.numBuckets

默认值

10

可选值

整型

默认属性

hystrix.threadpool.default.metrics.rollingStats.numBuckets

实例属性

hystrix.threadpool.HystrixCommandKey.metrics.rollingStats.numBuckets

 collapser控制HystrixCollapser行为。有以下三个属性

maxRequestsInBatch,此属性设置在触发批处理执行之前,批处理中允许的最大请求数。

配置名称

metrics.rollingStats.numBuckets

默认值

MAX_VALUE

可选值

整型

默认属性

hystrix.collapser.default.maxRequestsInBatch

实例属性

hystrix.collapser.HystrixCommandKey.maxRequestsInBatch

timerDelayInMilliseconds,此属性设置创建批处理后触发其执行的毫秒数。

配置名称

timerDelayInMilliseconds

默认值

10

可选值

整型

默认属性

hystrix.collapser.default.timerDelayInMilliseconds

实例属性

hystrix.collapser.HystrixCommandKey.timerDelayInMilliseconds

requestCache.enabled,该属性表示是否为HystrixCollapser.execute() and HystrixCollapser.queue()调用启用缓存

配置名称

requestCache.enabled

默认值

10

可选值

整型

默认属性

hystrix.collapser.default.requestCache.enabled

实例属性

hystrix.collapser.HystrixCommandKey.requestCache.enabled

本篇主要介绍了Hystrix所使用的配置,本来还想简单的介绍一下Hystrix的原理,但是篇幅有限,这里就不再介绍了,等闲下来再补一篇Hystrix的原理与高级应用,下一篇会接好Hystrix的监控,以及集群监控,有关配置的详细描述可以参考github wiki https://github.com/Netflix/Hystrix/wiki/Configuration 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值