Hystrix配置项

Command Properties

执行相关

executionIsolationStrategy

设置方法执行的隔离策略。可选线程池或者信号量。
默认情况下:HystrixCommands使用线程池颗粒策略(THREAD);HystrixObservableCommands使用信号量隔离策略(SEMAPHORE)。
一般来说,建议使用线程池隔离策略,除非线程管理的耗费(请求排队、调度、上下文切换),大大超过服务处理时间的情况下——比如不依赖网络访问只依赖本地计算的服务——才使用信号量隔离方式。

两种隔离策略的对比
 线程池隔离信号量隔离
线程与调用程非相同线程与调用线程相同
开销排队、调度、上下文开销等无线程切,销低
异步支持不支持
并发支持支持(最大线程池大小)支持(最大信号量上限)
Default ValueTHREAD
Possible ValuesTHREAD,SEMAPHORE
DefaultPropertyhystrix.command.default.execution.isolation.srategy
Instance Propertyhystrix.command.HystrixCommandKey.execution.isolation.strategy
How to Set Instance Default:
// to use thread
isolationHystrixCommandProperties.Setter().withExecutionIsolationStrategyExecutionIsolationStratgy.THREAD)
// to use semaphore
isolationHystrixCommandProperties.Setter().withExecutionIsolationStrategyExecutionIsolationStratgy.SEMAPHORE)

executionIsolationSemaphoreMaxConcurrentRequests

设置信号量隔离时的最大并发请求数,当使用信号量隔离的时候,此配置有效。官方给出5000rps只需要2个。

semaphore should still be a small percentage of the overall container (i.e. Tomcat) thread pool, not all of or most of it, otherwise it provides no protection.
信号量的个数,与整个容器拥有线程的的个数相比,应当小很多,而不是等于或者和后者差不多,否则隔离策略就起不到保护作用了。

Default Value10
Default Propertyhystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests
InstancePropertyhystrix.command.HystrixCommandKey.execution.isolation.semaphore.maxConcurrentRequests
How to Set Instance Default
HystrixCommandProperties.Setter().withExecutionIsolationSemaphoreMaxConcurrentRequests(int value)

executionIsolationThreadInterruptOnTimeout

当服务请求发生超时,HystrixCommand.run()线程是否会被中断

Default Valuetrue
Default Propertyhystrix.command.default.execution.isolation.tread.interruptOnTimeout
InstancePropertyhystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnTimeout
How to Set Instance Default
HystrixCommandProperties.Setter().withExecutionIsolationThreadInterruptOnTimeout(boolean value)

executionTimeoutEnabled

HystrixCommand.run()是否有超时限制

Default Valuetrue
Default Propertyhystrix.command.default.execution.timeout.enaled
Instance Propertyhystrix.command.HystrixCommandKey.execution.timeout.enabled
HowtoSet Instance Default
HystrixCommandProperties.Setter().withExecutionTimeoutEnabled(boolean value)

executionTimeoutInMilliseconds

设置调用者等待命令执行的超时限制,超过此时间,HystrixCommand被标记为TIMEOUT,并执行fallback逻辑。

Default Value1000
Default Propertyhystrix.command.default.execution.isolation.tread.timeoutInMilliseconds
Instance Propertyhystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds
How to Set Instance Default
HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(int value)

断路器相关

执行Command时,Hystrix先检查断路器(Circuit Breaker)是否打开。

  • 如果断路器开启,则Command不会被执行,转而执行fallback逻辑(如果fallback逻辑存在并开启了)。

  • 如果断路器未开启,则执行Command(前提是ThreadPoolSemaphore有空余配额,否则执行fallback)。

    Command执行流程图

circuitBreakerEnabled

断路器是否可用

Default Valuetrue
Default Propertyhystrix.command.default.circuitBreaker.enabled
Instance Propertyhystrix.command.HystrixCommandKey.circuitBreaker.enabled
How to SetInstance Default
HystrixCommandProperties.Setter().withCircuitBreakerEnabled(boolean value)

circuitBreakerForceClosed

断路器强制关闭:如果为true,则强制关闭。无论错误率达到了多少,所有的请求都会被放行。

Default Valuefalse
Default Propertyhystrix.command.default.circuitBreaker.forceClosed
Instance Propertyhystrix.command.HystrixCommandKey.circuitBreaker.forceClosed
How toSet Instance Default
HystrixCommandProperties.Setter().withCircuitBreakerForceClosed(boolean value)

circuitBreakerForceOpen

断路器强制开启:如果为true,则强制开启,所有的请求都会被拒绝。

Default Valuefalse
Default Propertyhystrix.command.default.circuitBreaker.forceOpen
Instance Propertyhystrix.command.HystrixCommandKey.circuitBreaker.forceOpen
How toSetInstance Default
HystrixCommandProperties.Setter().withCircuitBreakerForceOpen(boolean value)

circuitBreakerRequestVolumeThreshold

时间窗口内最小请求数,当小于这个请求数,即使全部失败也不会熔断。
比如设置此值为20,那么如果当前窗口为19(小于等于20),及时19个请求都失败了,断路器也不会打开。

Default Value20
Default Propertyhystrix.command.default.circuitBreaker.requestVolumeThreshold
InstancePropertyhystrix.command.HystrixCommandKey.circuitBreaker.requestVolumeThreshold
How to Set Instance Default
HystrixCommandProperties.Setter().withCircuitBreakerRequestVolumeThreshold(int value)

circuitBreakerSleepWindowInMilliseconds

断路器开启后,此时间间隔内会拒绝所有请求,直到此时间过后开始试探是否仍需保持熔断

Default Value5000
Default Propertyhystrix.command.default.circuitBreaker.sleepWindowInMilliseconds
InstancePropertyhystrix.command.HystrixCommandKey.circuitBreaker.sleepWindowInMilliseconds
How to Set Instance Default
HystrixCommandProperties.Setter().withCircuitBreakerSleepWindowInMilliseconds(int value)

Fallback相关

有4种情况会尝试进入Fallback逻辑,可参见Command执行流程图

  • construct()run()方法抛出异常
  • 断路器打开
  • ThreadPoolSemaphore配额用尽
  • Command超时
    注:在执行抛出HystrixBadRequestException时不进入Fallback

如果fallback方法不存在或者fallback方法也抛出了异常,则会通过一个异常通知告知调用者,通知的方式根据调用的方式不同而不同,具体参见Hystrix官方文档How-it-Works_get-the-fallback

fallbackEnabled

fallback方法是否可用,如果为true,当请求失败或被拒绝时,会调用HystrixCommand.getFallback()方法

Default Valuetrue
Default Propertyhystrix.command.default.fallback.enabled
Instance Propertyhystrix.command.HystrixCommandKey.fallback.enabled
How to SetInstance Default
HystrixCommandProperties.Setter().withFallbackEnabled(boolean value)

fallbackIsolationSemaphoreMaxConcurrentRequests

调用线程产生的HystrixCommand.getFallback()最大次数,超过此次数请求会被拒绝,fallback不会被执行,而是直接抛出异常。

Default Value10
Default Propertyhystrix.command.default.fallback.isolation.smaphore.maxConcurrentRequests
Instance Propertyhystrix.command.HystrixCommandKey.fallback.isolation.semaphore.maxConcurrentRequests
How to Set Instance Default
HystrixCommandProperties.Setter().withFallbackIsolationSemaphoreMaxConcurrentRequests(int value)

监控指标相关

HystrixCommand执行的时候,会生成一些执行耗时等方面的统计信息。这些信息对于系统的运维来说,是很有帮助的,因为我们通过这些统计信息可以看到整个系统是怎么运行的。hystrix对每个command key都会提供一份metric,而且是秒级统计粒度的。

metricsRollingStatisticalWindowInMilliseconds

设置statistical window的时间长度,单位是毫秒,hystrix只会维持这段时间内的统计值供短路器统计使用。
注:此值不允许热修改。

举例来说,如果此值设置为10秒(10000毫秒),bucket设置为1秒一个,下图展示了statistical window的滑动和bucket的新增和淘汰。
statistical rolling window

Default Value10000
Default Propertyhystrix.command.default.metrics.rollingStats.timeInMilliseconds
Instance Propertyhystrix.command.HystrixCommandKey.metrics.rollingStats.timeInMilliseconds
How to Set Instance Default
HystrixCommandProperties.Setter().withMetricsRollingStatisticalWindowInMilliseconds(int value)

metricsRollingStatisticalWindowBuckets

统计窗口中桶的数量。
注:

  • metrics.rollingStats.timeInMilliseconds % metrics.rollingStats.numBuckets == 0 必须为真,否则会抛出异常,例如可以是10000/10或10000/20,不能是10000/7。
  • 此值不允许热修改。

Default Value10
PossibleValues任意可以整除metrics.rollingStats.timeInMilliseconds的整数,然而,整除的结果应该是几百或几千毫秒,小于100毫秒时的性能尚未进行官方测试。
Default Propertyhystrix.command.default.metrics.rollingStats.numBuckets
Instance Propertyhystrix.command.HystrixCommandKey.metrics.rollingStats.numBuckets
How to Set Instance Default
HystrixCommandProperties.Setter().withMetricsRollingStatisticalWindowBuckets(int value)

metricsRollingPercentileEnabled

是否统计执行延时,并且计算各个百分比分位数,如前50%,90%等的执行延时。若此值为false,则所有求和计算(求平均值、求百分比)将会返回-1。

Default Valuetrue
Default Propertyhystrix.command.default.metrics.rollingPercentile.enabled
Instance Propertyhystrix.command.HystrixCommandKey.metrics.rollingPercentile.enabled
How to Set Instance Default
HystrixCommandProperties.Setter().withMetricsRollingPercentileEnabled(boolean value)

metricsRollingPercentileWindowInMilliseconds

设置rolling window被持久化保存的时间,这样才能计算一些请求耗时的百分比。相当于是一个大的rolling window,专门用于计算请求执行耗时的百分比。
注:此值不允许热修改。

Default Value60000
Default Propertyhystrix.command.default.metrics.rollingPercentile.timeInMilliseconds
Instance Propertyhystrix.command.HystrixCommandKey.metrics.rollingPercentile.timeInMilliseconds
How to Set Instance Default
HystrixCommandProperties.Setter().withMetricsRollingPercentileWindowInMilliseconds(int value)

metricsRollingPercentileWindowBuckets

设置rolling percentile window被拆分成的bucket数量。
注:

  • metrics.rollingPercentile.timeInMilliseconds % metrics.rollingPercentile.numBuckets == 0 必须为真,否则会抛出异常。
  • 此值不允许热修改
Default Value6
Possible Values任意可以整除metrics.rollingPercentile.timeInMilliseconds的整数,然而,整除的结果应该是几千毫秒,小于1000毫秒时的性能尚未进行官方测试。
Default Propertyhystrix.command.default.metrics.rollingPercentile.numBuckets
Instance Propertyhystrix.command.HystrixCommandKey.metrics.rollingPercentile.numBuckets
How to Set Instance Default
HystrixCommandProperties.Setter().withMetricsRollingPercentileWindowBuckets(int value)

metricsRollingPercentileBucketSize

设置每个bucket中最多保存的请求执行次数,如果在一个bucket内,执行次数超过了这个值,那么就会覆盖此bucket,从头再开始写
举例来说,如果bucket size设置为100,而且设置每个bucket为10秒钟,但是在此10s内发生了500次请求,那么这个bucket仅会保留最后100次的执行。

注:

  • 如果调大此值,存储相关的统计值所耗费的内存也会增加,计算百分比类型指标的时间也会增加
  • 此值不允许热修改
Default Value100
Default Propertyhystrix.command.default.metrics.rollingPercentile.bucketSize
Instance Propertyhystrix.command.HystrixCommandKey.metrics.rollingPercentile.bucketSize
How to Set Instance Default
HystrixCommandProperties.Setter().withMetricsRollingPercentileBucketSize(int value)

metricsHealthSnapshotIntervalInMilliseconds

每次计算成功和错误百分比(并影响断路器状态)的间隔时间。
在高运算服务的情况下,计算成功和错误百分比的过程可能是占用cpu的,可通过此参数来控制计算的频度。

Default Value500
Default Propertyhystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds
Instance Propertyhystrix.command.HystrixCommandKey.metrics.healthSnapshot.intervalInMilliseconds
How to Set Instance Default
HystrixCommandProperties.Setter().withMetricsHealthSnapshotIntervalInMilliseconds(int value)

请求相关

requestCacheEnabled

设置是否缓存请求。
缓存是request-scope的。hystrix支持将一个请求结果缓存起来,在本request内,下一个具有相同key的HystrixCommand将直接从缓存中取出结果,减少请求开销。

Default Valuetrue
Default Propertyhystrix.command.default.requestCache.enabled
Instance Propertyhystrix.command.HystrixCommandKey.requestCache.enabled
How to SetInstance Default
HystrixCommandProperties.Setter().withRequestCacheEnabled(boolean value)

requestLogEnabled

设置HystrixCommand执行和事件是否打印到HystrixRequestLog中。

Default Valuetrue
Default Propertyhystrix.command.default.requestLog.enabled
Instance Propertyhystrix.command.HystrixCommandKey.requestLog.enabled
How to Set InstanceDefault
HystrixCommandProperties.Setter().withRequestLogEnabled(boolean value)

ThreadPool Properties

coreSize

大多数时候,默认值为10就很好(通常可以做得更小)
合适的大小为:

每秒的高峰访问次数 * 99%的访问平均延时 + 部分余量

例如: 30 * 0.2 + 4 = 10 个线程

Default Value10
Default Propertyhystrix.threadpool.default.coreSize
Instance Propertyhystrix.threadpool.HystrixThreadPoolKey.coreSize
How to Set Instance Default
HystrixThreadPoolProperties.Setter().withCoreSize(int value)

maximumSize

此属性设置最大线程池大小。 在并发数达到此值之前不会拒绝HystrixCommands。
注:此属性仅在allowMaximumSizeToDivergeFromCoreSize为true时才会生效。

Default Value10
Default Propertyhystrix.threadpool.default.maximumSize
Instance Propertyhystrix.threadpool.HystrixThreadPoolKey.maximumSize
How to Set Instance Default
HystrixThreadPoolProperties.Setter().withMaximumSize(int value)

maxQueueSize

请求等待队列长度。-1代表使用SynchronousQueue,正数代表使用LinkedBlockingQueue
注 : 在SynchronousQueue和LinkedBlockingQueue之间切换需要重启

Default Value−1
Default Propertyhystrix.threadpool.default.maxQueueSize
Instance Propertyhystrix.threadpool.HystrixThreadPoolKey.maxQueueSize
How to Set Instance Default
HystrixThreadPoolProperties.Setter().withMaxQueueSize(int value)

queueSizeRejectionThreshold

此属性设置队列大小拒绝阈值——一个虚假的最大队列长度值——即使尚未达到maxQueueSize,请求也会被拒绝。
此属性的存在是因为无法动态更改BlockingQueue的maxQueueSize,此属性允许您动态更改队列大小来拒绝请求。
注:当maxQueueSize == -1时,此属性不启用。

Default Value5
Default Propertyhystrix.threadpool.default.queueSizeRejectionThreshold
Instance Propertyhystrix.threadpool.HystrixThreadPoolKey.queueSizeRejectionThreshold
How to Set InstanceDefault
HystrixThreadPoolProperties.Setter().withQueueSizeRejectionThreshold(int value)

keepAliveTimeMinutes

在1.5.9之前,所有线程池都是固定大小的,即coreSize == maximumSize。 在1.5.9及之后,将allowMaximumSizeToDivergeFromCoreSize设置为true后,允许这两个值不同,这样线程池可以动态获取/释放线程。 如果coreSize <maximumSize,则线程在闲置了keepAliveTimeMinutes值的时间后,会被释放。

Default Value1
Default Propertyhystrix.threadpool.default.keepAliveTimeMinutes
Instance Propertyhystrix.threadpool.HystrixThreadPoolKey.keepAliveTimeMinutes
How to Set Instance Default
HystrixThreadPoolProperties.Setter().withKeepAliveTimeMinutes(int value)

allowMaximumSizeToDivergeFromCoreSize

此属性在1.5.9中添加。 此属性会令maximumSize配置生效。 该值可以等于或高于coreSize。 设置coreSize <maximumSize时,会创建并维护一个可以达到maximumSize并发量的线程池,但在不活跃时,会将线程返回给系统。

Default Valuefalse
Default Propertyhystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize
Instance Propertyhystrix.threadpool.HystrixThreadPoolKey.allowMaximumSizeToDivergeFromCoreSize
How to Set Instance Default
HystrixThreadPoolProperties.Setter().withAllowMaximumSizeToDivergeFromCoreSize(boolean value)

metrics.rollingStats.timeInMilliseconds

此属性设置statistical rolling窗口的持续时间(以毫秒为单位)。 与Command Properties中的配置同理,但这里是为线程池保留statistical rolling窗口的时间。

Default Value10000
Default Propertyhystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds
Instance Propertyhystrix.threadpool.HystrixThreadPoolKey.metrics.rollingStats.timeInMilliseconds
How to Set Instance Default
HystrixThreadPoolProperties.Setter().withMetricsRollingStatisticalWindowInMilliseconds(int value)

metrics.rollingStats.numBuckets

此属性设置滚动统计窗口划分的桶数。
注:metrics.rollingStats.timeInMilliseconds%metrics.rollingStats.numBuckets == 0必须为真,否则将引发异常。
与Command Properties中的配置同理。

Default Value10
Possible Values任意可以整除metrics.rollingStats.timeInMilliseconds的整数,然而,整除的结果应该是几百或几千毫秒,小于100毫秒时的性能尚未进行官方测试。
Default Propertyhystrix.threadpool.default.metrics.rollingStats.numBuckets
InstancePropertyhystrix.threadpool.HystrixThreadPoolProperties.metrics.rollingStats.numBuckets
How to Set InstanceDefault
HystrixThreadPoolProperties.Setter().withMetricsRollingStatisticalWindowBuckets(int value)

Collapser Properties

  • HystrixCommand执行之前可以使用请求合并器(HystrixCollapser,一个抽象的父类)来把多个请求合并成一个然后对后端依赖系统发起调用。
  • 启用请求合并的成本是在执行实际命令之前的延迟。最大的成本是批处理窗口的大小,默认是10ms。
  • 这个成本是否值得取决于正在执行的命令,高延迟命令不会受到少量附加平均延迟的影响。而且,命令的并发量也是关键:如果很少有超过1个或2个请求被组合在一起,那么这个成本就是不值得的。事实上,如果一个单线程不断执行顺序请求,那么请求的合并将会是一个主要的性能瓶颈,每一次单个请求都会等待10ms的合并窗口时间。
  • 但是,如果一个特定的命令同时被大量使用,这个成本是值得的,因为hystrix可以节省很多线程和连接资源
  • 下图展示了不使用和使用Collapser时的区别,可以看到,使用后多个command被合并,仅占用了一条线程、只发起了一次请求。
    collapser示意图

maxRequestsInBatch

触发一次批量请求之前,囤积的单个请求的最大个数。

Default ValueInteger.MAX_VALUE
DefaultPropertyhystrix.collapser.default.maxRequestsInBatch
Instance Propertyhystrix.collapser.HystrixCollapserKey.maxRequestsInBatch
How to Set Instance Default
HystrixCollapserProperties.Setter().withMaxRequestsInBatch(int value)

timerDelayInMilliseconds

从创建批量请求,到触发批量请求之间的时间(毫秒),在此时间窗口内,会囤积单个请求到HystrixCollapser中。

Default Value10
Default Propertyhystrix.collapser.default.timerDelayInMilliseconds
Instance Propertyhystrix.collapser.HystrixCollapserKey.timerDelayInMilliseconds
How to Set Instance Default
HystrixCollapserProperties.Setter().withTimerDelayInMilliseconds(int value)

requestCache.enabled

设置requestCache是否作用于HystrixCollapser.execute()HystrixCollapser.queue()

Default Valuetrue
Default Propertyhystrix.collapser.default.requestCache.enabled
Instance Propertyhystrix.collapser.HystrixCollapserKey.requestCache.enabled
How to Set Instance Default
HystrixCollapserProperties.Setter().withRequestCacheEnabled(boolean value)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值