Command Properties
执行相关
executionIsolationStrategy
设置方法执行的隔离策略。可选线程池或者信号量。
默认情况下:HystrixCommands
使用线程池颗粒策略(THREAD);HystrixObservableCommands
使用信号量隔离策略(SEMAPHORE)。
一般来说,建议使用线程池隔离策略,除非线程管理的耗费(请求排队、调度、上下文切换),大大超过服务处理时间的情况下——比如不依赖网络访问只依赖本地计算的服务——才使用信号量隔离方式。
线程池隔离 | 信号量隔离 | |
---|---|---|
线程 | 与调用程非相同线程 | 与调用线程相同 |
开销 | 排队、调度、上下文开销等 | 无线程切,销低 |
异步 | 支持 | 不支持 |
并发支持 | 支持(最大线程池大小) | 支持(最大信号量上限) |
Default Value | THREAD |
---|---|
Possible Values | THREAD,SEMAPHORE |
DefaultProperty | hystrix.command.default.execution.isolation.srategy |
Instance Property | hystrix.command.HystrixCommandKey.execution.isolation.strategy |
How to Set Instance Default: | // to use thread |
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 Value | 10 |
---|---|
Default Property | hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests |
InstanceProperty | hystrix.command.HystrixCommandKey.execution.isolation.semaphore.maxConcurrentRequests |
How to Set Instance Default | HystrixCommandProperties.Setter().withExecutionIsolationSemaphoreMaxConcurrentRequests(int value) |
executionIsolationThreadInterruptOnTimeout
当服务请求发生超时,HystrixCommand.run()
线程是否会被中断
Default Value | true |
---|---|
Default Property | hystrix.command.default.execution.isolation.tread.interruptOnTimeout |
InstanceProperty | hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnTimeout |
How to Set Instance Default | HystrixCommandProperties.Setter().withExecutionIsolationThreadInterruptOnTimeout(boolean value) |
executionTimeoutEnabled
HystrixCommand.run()
是否有超时限制
Default Value | true |
---|---|
Default Property | hystrix.command.default.execution.timeout.enaled |
Instance Property | hystrix.command.HystrixCommandKey.execution.timeout.enabled |
HowtoSet Instance Default | HystrixCommandProperties.Setter().withExecutionTimeoutEnabled(boolean value) |
executionTimeoutInMilliseconds
设置调用者等待命令执行的超时限制,超过此时间,HystrixCommand被标记为TIMEOUT,并执行fallback逻辑。
Default Value | 1000 |
---|---|
Default Property | hystrix.command.default.execution.isolation.tread.timeoutInMilliseconds |
Instance Property | hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds |
How to Set Instance Default | HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(int value) |
断路器相关
执行Command时,Hystrix先检查断路器(Circuit Breaker)是否打开。
如果断路器开启,则Command不会被执行,转而执行fallback逻辑(如果fallback逻辑存在并开启了)。
如果断路器未开启,则执行Command(前提是
ThreadPool
或Semaphore
有空余配额,否则执行fallback)。
circuitBreakerEnabled
断路器是否可用
Default Value | true |
---|---|
Default Property | hystrix.command.default.circuitBreaker.enabled |
Instance Property | hystrix.command.HystrixCommandKey.circuitBreaker.enabled |
How to SetInstance Default | HystrixCommandProperties.Setter().withCircuitBreakerEnabled(boolean value) |
circuitBreakerForceClosed
断路器强制关闭:如果为true,则强制关闭。无论错误率达到了多少,所有的请求都会被放行。
Default Value | false |
---|---|
Default Property | hystrix.command.default.circuitBreaker.forceClosed |
Instance Property | hystrix.command.HystrixCommandKey.circuitBreaker.forceClosed |
How toSet Instance Default | HystrixCommandProperties.Setter().withCircuitBreakerForceClosed(boolean value) |
circuitBreakerForceOpen
断路器强制开启:如果为true,则强制开启,所有的请求都会被拒绝。
Default Value | false |
---|---|
Default Property | hystrix.command.default.circuitBreaker.forceOpen |
Instance Property | hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen |
How toSetInstance Default | HystrixCommandProperties.Setter().withCircuitBreakerForceOpen(boolean value) |
circuitBreakerRequestVolumeThreshold
时间窗口内最小请求数,当小于这个请求数,即使全部失败也不会熔断。
比如设置此值为20,那么如果当前窗口为19(小于等于20),及时19个请求都失败了,断路器也不会打开。
Default Value | 20 |
---|---|
Default Property | hystrix.command.default.circuitBreaker.requestVolumeThreshold |
InstanceProperty | hystrix.command.HystrixCommandKey.circuitBreaker.requestVolumeThreshold |
How to Set Instance Default | HystrixCommandProperties.Setter().withCircuitBreakerRequestVolumeThreshold(int value) |
circuitBreakerSleepWindowInMilliseconds
断路器开启后,此时间间隔内会拒绝所有请求,直到此时间过后开始试探是否仍需保持熔断
Default Value | 5000 |
---|---|
Default Property | hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds |
InstanceProperty | hystrix.command.HystrixCommandKey.circuitBreaker.sleepWindowInMilliseconds |
How to Set Instance Default | HystrixCommandProperties.Setter().withCircuitBreakerSleepWindowInMilliseconds(int value) |
Fallback相关
有4种情况会尝试进入Fallback逻辑,可参见Command执行流程图
construct()
或run()
方法抛出异常- 断路器打开
ThreadPool
或Semaphore
配额用尽- Command超时
注:在执行抛出HystrixBadRequestException
时不进入Fallback如果fallback方法不存在或者fallback方法也抛出了异常,则会通过一个异常通知告知调用者,通知的方式根据调用的方式不同而不同,具体参见Hystrix官方文档How-it-Works_get-the-fallback
fallbackEnabled
fallback方法是否可用,如果为true,当请求失败或被拒绝时,会调用HystrixCommand.getFallback()
方法
Default Value | true |
---|---|
Default Property | hystrix.command.default.fallback.enabled |
Instance Property | hystrix.command.HystrixCommandKey.fallback.enabled |
How to SetInstance Default | HystrixCommandProperties.Setter().withFallbackEnabled(boolean value) |
fallbackIsolationSemaphoreMaxConcurrentRequests
调用线程产生的HystrixCommand.getFallback()
最大次数,超过此次数请求会被拒绝,fallback不会被执行,而是直接抛出异常。
Default Value | 10 |
---|---|
Default Property | hystrix.command.default.fallback.isolation.smaphore.maxConcurrentRequests |
Instance Property | hystrix.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的新增和淘汰。
Default Value | 10000 |
---|---|
Default Property | hystrix.command.default.metrics.rollingStats.timeInMilliseconds |
Instance Property | hystrix.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 Value | 10 |
---|---|
PossibleValues | 任意可以整除metrics.rollingStats.timeInMilliseconds 的整数,然而,整除的结果应该是几百或几千毫秒,小于100毫秒时的性能尚未进行官方测试。 |
Default Property | hystrix.command.default.metrics.rollingStats.numBuckets |
Instance Property | hystrix.command.HystrixCommandKey.metrics.rollingStats.numBuckets |
How to Set Instance Default | HystrixCommandProperties.Setter().withMetricsRollingStatisticalWindowBuckets(int value) |
metricsRollingPercentileEnabled
是否统计执行延时,并且计算各个百分比分位数,如前50%,90%等的执行延时。若此值为false,则所有求和计算(求平均值、求百分比)将会返回-1。
Default Value | true |
---|---|
Default Property | hystrix.command.default.metrics.rollingPercentile.enabled |
Instance Property | hystrix.command.HystrixCommandKey.metrics.rollingPercentile.enabled |
How to Set Instance Default | HystrixCommandProperties.Setter().withMetricsRollingPercentileEnabled(boolean value) |
metricsRollingPercentileWindowInMilliseconds
设置rolling window被持久化保存的时间,这样才能计算一些请求耗时的百分比。相当于是一个大的rolling window,专门用于计算请求执行耗时的百分比。
注:此值不允许热修改。
Default Value | 60000 |
---|---|
Default Property | hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds |
Instance Property | hystrix.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 Value | 6 |
---|---|
Possible Values | 任意可以整除metrics.rollingPercentile.timeInMilliseconds 的整数,然而,整除的结果应该是几千毫秒,小于1000毫秒时的性能尚未进行官方测试。 |
Default Property | hystrix.command.default.metrics.rollingPercentile.numBuckets |
Instance Property | hystrix.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 Value | 100 |
---|---|
Default Property | hystrix.command.default.metrics.rollingPercentile.bucketSize |
Instance Property | hystrix.command.HystrixCommandKey.metrics.rollingPercentile.bucketSize |
How to Set Instance Default | HystrixCommandProperties.Setter().withMetricsRollingPercentileBucketSize(int value) |
metricsHealthSnapshotIntervalInMilliseconds
每次计算成功和错误百分比(并影响断路器状态)的间隔时间。
在高运算服务的情况下,计算成功和错误百分比的过程可能是占用cpu的,可通过此参数来控制计算的频度。
Default Value | 500 |
---|---|
Default Property | hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds |
Instance Property | hystrix.command.HystrixCommandKey.metrics.healthSnapshot.intervalInMilliseconds |
How to Set Instance Default | HystrixCommandProperties.Setter().withMetricsHealthSnapshotIntervalInMilliseconds(int value) |
请求相关
requestCacheEnabled
设置是否缓存请求。
缓存是request-scope的。hystrix支持将一个请求结果缓存起来,在本request内,下一个具有相同key的HystrixCommand
将直接从缓存中取出结果,减少请求开销。
Default Value | true |
---|---|
Default Property | hystrix.command.default.requestCache.enabled |
Instance Property | hystrix.command.HystrixCommandKey.requestCache.enabled |
How to SetInstance Default | HystrixCommandProperties.Setter().withRequestCacheEnabled(boolean value) |
requestLogEnabled
设置HystrixCommand
执行和事件是否打印到HystrixRequestLog
中。
Default Value | true |
---|---|
Default Property | hystrix.command.default.requestLog.enabled |
Instance Property | hystrix.command.HystrixCommandKey.requestLog.enabled |
How to Set InstanceDefault | HystrixCommandProperties.Setter().withRequestLogEnabled(boolean value) |
ThreadPool Properties
coreSize
大多数时候,默认值为10就很好(通常可以做得更小)
合适的大小为:
每秒的高峰访问次数 * 99%的访问平均延时 + 部分余量
例如: 30 * 0.2 + 4 = 10 个线程
Default Value | 10 |
---|---|
Default Property | hystrix.threadpool.default.coreSize |
Instance Property | hystrix.threadpool.HystrixThreadPoolKey.coreSize |
How to Set Instance Default | HystrixThreadPoolProperties.Setter().withCoreSize(int value) |
maximumSize
此属性设置最大线程池大小。 在并发数达到此值之前不会拒绝HystrixCommands。
注:此属性仅在allowMaximumSizeToDivergeFromCoreSize为true时才会生效。
Default Value | 10 |
---|---|
Default Property | hystrix.threadpool.default.maximumSize |
Instance Property | hystrix.threadpool.HystrixThreadPoolKey.maximumSize |
How to Set Instance Default | HystrixThreadPoolProperties.Setter().withMaximumSize(int value) |
maxQueueSize
请求等待队列长度。-1代表使用SynchronousQueue,正数代表使用LinkedBlockingQueue
注 : 在SynchronousQueue和LinkedBlockingQueue之间切换需要重启
Default Value | −1 |
---|---|
Default Property | hystrix.threadpool.default.maxQueueSize |
Instance Property | hystrix.threadpool.HystrixThreadPoolKey.maxQueueSize |
How to Set Instance Default | HystrixThreadPoolProperties.Setter().withMaxQueueSize(int value) |
queueSizeRejectionThreshold
此属性设置队列大小拒绝阈值——一个虚假的最大队列长度值——即使尚未达到maxQueueSize,请求也会被拒绝。
此属性的存在是因为无法动态更改BlockingQueue的maxQueueSize,此属性允许您动态更改队列大小来拒绝请求。
注:当maxQueueSize == -1
时,此属性不启用。
Default Value | 5 |
---|---|
Default Property | hystrix.threadpool.default.queueSizeRejectionThreshold |
Instance Property | hystrix.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 Value | 1 |
---|---|
Default Property | hystrix.threadpool.default.keepAliveTimeMinutes |
Instance Property | hystrix.threadpool.HystrixThreadPoolKey.keepAliveTimeMinutes |
How to Set Instance Default | HystrixThreadPoolProperties.Setter().withKeepAliveTimeMinutes(int value) |
allowMaximumSizeToDivergeFromCoreSize
此属性在1.5.9中添加。 此属性会令maximumSize
配置生效。 该值可以等于或高于coreSize。 设置coreSize <maximumSize时,会创建并维护一个可以达到maximumSize并发量的线程池,但在不活跃时,会将线程返回给系统。
Default Value | false |
---|---|
Default Property | hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize |
Instance Property | hystrix.threadpool.HystrixThreadPoolKey.allowMaximumSizeToDivergeFromCoreSize |
How to Set Instance Default | HystrixThreadPoolProperties.Setter().withAllowMaximumSizeToDivergeFromCoreSize(boolean value) |
metrics.rollingStats.timeInMilliseconds
此属性设置statistical rolling窗口的持续时间(以毫秒为单位)。 与Command Properties中的配置同理,但这里是为线程池保留statistical rolling窗口的时间。
Default Value | 10000 |
---|---|
Default Property | hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds |
Instance Property | hystrix.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 Value | 10 |
---|---|
Possible Values | 任意可以整除metrics.rollingStats.timeInMilliseconds的整数,然而,整除的结果应该是几百或几千毫秒,小于100毫秒时的性能尚未进行官方测试。 |
Default Property | hystrix.threadpool.default.metrics.rollingStats.numBuckets |
InstanceProperty | hystrix.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被合并,仅占用了一条线程、只发起了一次请求。
maxRequestsInBatch
触发一次批量请求之前,囤积的单个请求的最大个数。
Default Value | Integer.MAX_VALUE |
---|---|
DefaultProperty | hystrix.collapser.default.maxRequestsInBatch |
Instance Property | hystrix.collapser.HystrixCollapserKey.maxRequestsInBatch |
How to Set Instance Default | HystrixCollapserProperties.Setter().withMaxRequestsInBatch(int value) |
timerDelayInMilliseconds
从创建批量请求,到触发批量请求之间的时间(毫秒),在此时间窗口内,会囤积单个请求到HystrixCollapser
中。
Default Value | 10 |
---|---|
Default Property | hystrix.collapser.default.timerDelayInMilliseconds |
Instance Property | hystrix.collapser.HystrixCollapserKey.timerDelayInMilliseconds |
How to Set Instance Default | HystrixCollapserProperties.Setter().withTimerDelayInMilliseconds(int value) |
requestCache.enabled
设置requestCache是否作用于HystrixCollapser.execute()
和 HystrixCollapser.queue()
Default Value | true |
---|---|
Default Property | hystrix.collapser.default.requestCache.enabled |
Instance Property | hystrix.collapser.HystrixCollapserKey.requestCache.enabled |
How to Set Instance Default | HystrixCollapserProperties.Setter().withRequestCacheEnabled(boolean value) |