Hystrix
在2018年11月20日以后已经中止维护,最后一个提交记录是:Latest commit 3cb2158 on 20 Nov 2018
,最后一个正式版本为1.5.18
。鉴于目前所在公司的技术栈是Spring Cloud
,熔断和降级组件主要用的仍是Hystrix
,这里就Hystrix
的完整列表作一个分析记录,方便之后能够随时查询。本文主要参考:Hystrix Configuration。其中,命令配置是针对HystrixCommand
,主要包括命令执行(execution)配置、命令降级(fallback)配置、熔断器(circuit breaker)配置、度量统计(metrics)配置和请求上下文配置。java
HystrixCommandKey
、HystrixCommandGroupKey
和HystrixThreadPoolKey
三个KEY是HystrixCommand
的重要标识。下面分别分析一下它们的含义。git
HystrixCommandKey
是Hystrix
命令的惟一标识,准确来讲是HystrixCommand
实例或者HystrixObservableCommand
实例的惟一标识。它是必须的,若是不自定义配置,它会经过下面方式肯定默认值:github
[HystrixCommand或者HystrixObservableCommand的具体子类].getClass().getSimpleName();
编程式配置以下:编程
HystrixCommandKey.Factory.asKey("Your Key"); public Command() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Group Key")) .andCommandKey(HystrixCommandKey.Factory.asKey("Command Key"))); }
注意一点:大部分Hystrix
的配置都是和HystrixCommandKey
绑定,因此HystrixCommandKey
是比较重要的。缓存
HystrixCommandGroupKey
是用于对Hystrix
命令进行分组,分组以后便于统计展现于仪表盘、上传报告和预警等等,也就是说,HystrixCommandGroupKey
是Hystrix
内部进行度量统计时候的分组标识,数据上报和统计的最小维度就是分组的KEY。HystrixCommandGroupKey
是必须配置的,配置方式以下:网络
HystrixCommandGroupKey.Factory.asKey("Group Key") public Command() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Group Key"))); }
HystrixThreadPoolKey
主要标识用于监控、度量和缓存等等做用的HystrixThreadPool
实例。一个HystrixCommand
会和一个独立的HystrixThreadPool
实例关联,也就是说一类HystrixCommand
老是在同一个HystrixThreadPool
实例中执行。若是不显式配置HystrixThreadPoolKey
,那么会使用HystrixCommandGroupKey
的值去配置HystrixThreadPoolKey
。HystrixThreadPoolKey
的配置方式以下:多线程
HystrixThreadPoolKey.Factory.asKey("ThreadPoolKey") public Command() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("xxx")) .andCommandKey(HystrixCommandKey.Factory.asKey("YYY")) .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("ThreadPoolKey"))); }
隔离策略决定Hystrix
命令执行的时候采用什么类型的策略进行依赖隔离。并发
项 | 值 |
---|---|
默认值 | THREAD (见ExecutionIsolationStrategy.THREAD ) |
可选值 | THREAD ,SEMAPHORE |
默认全局配置 | hystrix.command.default.execution.isolation.strategy |
实例配置 | hystrix.command.[HystrixCommandKey].execution.isolation.strategy |
执行隔离策略到底选择线程池(THREAD
)仍是信号量(SEMAPHORE
)?文档中给出的建议是:ide
使用
HystrixCommand
的时候建议用THREAD
策略,使用HystrixObservableCommand
的时候建议使用SEMAPHORE
策略。性能
使用
THREAD
策略让HystrixCommand
在线程中执行能够提供额外的保护层,以防止由于网络超时致使的延时失败。
通常状况下,只有这种特殊例子下
HystrixCommand
会搭配SEMAPHORE
策略使用:调用的频次过高(例如每一个实例每秒数百次调用),这种状况若是选用THREAD
策略有可能致使超过线程隔离的上限(有可能须要太多的线程或者命令太多线程不足够用于隔离请求),这种状况通常是非网络请求调用。
笔者想说的是:建议选用默认值,由于目前不多遇到使用信号量隔离的场景。
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.execution.isolation.strategy=THREAD # 实例配置 hystrix.command.CustomCommand.execution.isolation.strategy=THREAD
决定HystrixCommand#run()
执行时是否容许超时,只有设置为true的时候,下面提到的“超时时间上限”才会有效。
项 | 值 |
---|---|
默认值 | true |
可选值 | true ,false |
默认全局配置 | hystrix.command.default.execution.timeout.enabled |
实例配置 | hystrix.command.[HystrixCommandKey].execution.timeout.enabled |
建议(笔者备注) | 保持选用默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionTimeoutEnabled(true))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.execution.timeout.enabled=true # 实例配置 hystrix.command.CustomCommand.execution.timeout.enabled=true
HystrixCommand
执行时候超时的最大上限,单位是毫秒,若是命令执行耗时超过此时间值那么会进入降级逻辑。这个配置生效的前提是hystrix.command.default.execution.timeout.enabled
或者hystrix.command.[HystrixCommandKey].execution.timeout.enabled
为true。
项 | 值 |
---|---|
默认值 | 1000 |
可选值 | - |
默认全局配置 | hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds |
实例配置 | hystrix.command.[HystrixCommandKey].execution.isolation.thread.timeoutInMilliseconds |
建议(笔者备注) | 保持选用默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionTimeoutInMilliseconds(1000))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000 # 实例配置 hystrix.command.CustomCommand.execution.isolation.thread.timeoutInMilliseconds=1000
此配置项决定HystrixCommand#run()
执行的时候调用超时的状况下是否中断。
项 | 值 |
---|---|
默认值 | true |
可选值 | true 、false |
默认全局配置 | hystrix.command.default.execution.isolation.thread.interruptOnTimeout |
实例配置 | hystrix.command.[HystrixCommandKey].execution.isolation.thread.interruptOnTimeout |
建议(笔者备注) | 保持选用默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionIsolationThreadInterruptOnTimeout(true))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.execution.isolation.thread.interruptOnTimeout=true # 实例配置 hystrix.command.CustomCommand.execution.isolation.thread.interruptOnTimeout=true
此配置项决定HystrixCommand#run()
执行的时候取消调用的状况下是否中断。
项 | 值 |
---|---|
默认值 | false |
可选值 | true 、false |
默认全局配置 | hystrix.command.default.execution.isolation.thread.interruptOnCancel |
实例配置 | hystrix.command.[HystrixCommandKey].execution.isolation.thread.interruptOnCancel |
建议(笔者备注) | 保持选用默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionIsolationThreadInterruptOnFutureCancel(false))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.execution.isolation.thread.interruptOnCancel=false # 实例配置 hystrix.command.CustomCommand.execution.isolation.thread.interruptOnCancel=false
此配置项决定使用HystrixCommand#run()
方法和ExecutionIsolationStrategy.SEMAPHORE
隔离策略下并发请求数量的最高上限。
项 | 值 |
---|---|
默认值 | 10 |
可选值 | - |
默认全局配置 | hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests |
实例配置 | hystrix.command.[HystrixCommandKey].execution.isolation.semaphore.maxConcurrentRequests |
建议(笔者备注) | 必须根据实际状况设定此值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE) .withExecutionIsolationSemaphoreMaxConcurrentRequests(100))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests=100 # 实例配置 hystrix.command.CustomCommand.execution.isolation.semaphore.maxConcurrentRequests=100
命令降级配置控制HystrixCommand#getFallback()
的执行逻辑,全部命令降级配置对策略ExecutionIsolationStrategy.THREAD
或者ExecutionIsolationStrategy.SEMAPHORE
都生效。
这个属性用于控制一个HystrixCommand#getFallback()
实例方法在执行线程中调用的最大上限,若是超过此上限,降级逻辑不会执行而且会抛出一个异常。
项 | 值 |
---|---|
默认值 | 10 |
可选值 | - |
默认全局配置 | hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests |
实例配置 | hystrix.command.[HystrixCommandKey].fallback.isolation.semaphore.maxConcurrentRequests |
建议(笔者备注) | 必须根据实际状况设定此值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withFallbackIsolationSemaphoreMaxConcurrentRequests(20))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests=20 # 实例配置 hystrix.command.CustomCommand.fallback.isolation.semaphore.maxConcurrentRequests=20
此属性控制当HystrixCommand
执行失败以后是否调用HystrixCommand#getFallback()
。
项 | 值 |
---|---|
默认值 | true |
可选值 | false 、true |
默认全局配置 | hystrix.command.default.fallback.enabled |
实例配置 | hystrix.command.[HystrixCommandKey].fallback.enabled |
建议(笔者备注) | 建议保持默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withFallbackEnabled(true))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.fallback.enabled=true # 实例配置 hystrix.command.CustomCommand.fallback.enabled=true
断路器配置用于控制HystrixCircuitBreaker
实例的行为。
此属性肯定断路器是否用于跟踪健康情况,以及当断路器打开的时候是否用于短路请求(使请求快速失败进入降级逻辑)。
项 | 值 |
---|---|
默认值 | true |
可选值 | false 、true |
默认全局配置 | hystrix.command.default.circuitBreaker.enabled |
实例配置 | hystrix.command.[HystrixCommandKey].circuitBreaker.enabled |
建议(笔者备注) | 建议保持默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withCircuitBreakerEnabled(true))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.circuitBreaker.enabled=true # 实例配置 hystrix.command.CustomCommand.circuitBreaker.enabled=true
此属性设置将使断路器打开的滑动窗口中的最小请求数量。
例如,若是值是20,那么若是在滑动窗口中只接收到19个请求(好比一个10秒的窗口),即便全部19个请求都失败了,断路器也不会打开。
项 | 值 |
---|---|
默认值 | 20 |
可选值 | - |
默认全局配置 | hystrix.command.default.circuitBreaker.requestVolumeThreshold |
实例配置 | hystrix.command.[HystrixCommandKey].circuitBreaker.requestVolumeThreshold |
建议(笔者备注) | 建议保持默认值,若是部分接口不能容忍默认阈值能够单独配置 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withCircuitBreakerRequestVolumeThreshold(10))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.circuitBreaker.requestVolumeThreshold=10 # 实例配置 hystrix.command.CustomCommand.circuitBreaker.requestVolumeThreshold=10
此属性设置断路器打开后拒绝请求的时间量,每隔一段时间(sleepWindowInMilliseconds
,单位是毫秒)容许再次尝试(也就是放行一个请求)肯定是否应该关闭断路器。
项 | 值 |
---|---|
默认值 | 5000 |
可选值 | - |
默认全局配置 | hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds |
实例配置 | hystrix.command.[HystrixCommandKey].circuitBreaker.sleepWindowInMilliseconds |
建议(笔者备注) | 建议保持默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withCircuitBreakerSleepWindowInMilliseconds(5000))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=5000 # 实例配置 hystrix.command.CustomCommand.circuitBreaker.sleepWindowInMilliseconds=5000
此属性设置一个错误百分比,当请求错误率超过设定值,断路器就会打开。
项 | 值 |
---|---|
默认值 | 50 |
可选值 | - |
默认全局配置 | hystrix.command.default.circuitBreaker.errorThresholdPercentage |
实例配置 | hystrix.command.[HystrixCommandKey].circuitBreaker.errorThresholdPercentage |
建议(笔者备注) | 建议保持默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withCircuitBreakerErrorThresholdPercentage(50))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.circuitBreaker.errorThresholdPercentage=50 # 实例配置 hystrix.command.CustomCommand.circuitBreaker.errorThresholdPercentage=50
注意:
circuitBreaker.requestVolumeThreshold
针对错误请求数量。circuitBreaker.errorThresholdPercentage
针对错误请求百分比。此属性控制断路器是否强制打开,强制打开断路器会使全部请求直接进入降级逻辑,也就是包裹在HystrixCommand#run()
的逻辑不会执行。circuitBreaker.forceOpen
属性和circuitBreaker.forceClosed
属性互斥。
项 | 值 |
---|---|
默认值 | false |
可选值 | false 、true |
默认全局配置 | hystrix.command.default.circuitBreaker.forceOpen |
实例配置 | hystrix.command.[HystrixCommandKey].circuitBreaker.forceOpen |
建议(笔者备注) | 建议保持默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withCircuitBreakerForceOpen(true))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.circuitBreaker.forceOpen=true # 实例配置 hystrix.command.CustomCommand.circuitBreaker.forceOpen=true
此属性控制断路器是否强制关闭,强制关闭断路器会致使全部和断路器相关的配置和功能都失效,HystrixCommand#run()
抛出异常会正常进入降级逻辑。circuitBreaker.forceClosed
属性和circuitBreaker.forceOpen
属性互斥。
项 | 值 |
---|---|
默认值 | false |
可选值 | false 、true |
默认全局配置 | hystrix.command.default.circuitBreaker.forceClosed |
实例配置 | hystrix.command.[HystrixCommandKey].circuitBreaker.forceClosed |
建议(笔者备注) | 建议保持默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withCircuitBreakerForceClosed(true))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.circuitBreaker.forceClosed=true # 实例配置 hystrix.command.CustomCommand.circuitBreaker.forceClosed=true
度量统计配置会对HystrixCommand
或者HystrixObservableCommand
执行时候的统计数据收集动做生效。
项 | 值 |
---|---|
默认值 | 10000 |
可选值 | - |
默认全局配置 | hystrix.command.default.metrics.rollingStats.timeInMilliseconds |
实例配置 | hystrix.command.[HystrixCommandKey].metrics.rollingStats.timeInMilliseconds |
建议(笔者备注) | 建议保持默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withMetricsRollingStatisticalWindowInMilliseconds(10000))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.metrics.rollingStats.timeInMilliseconds=10000 # 实例配置 hystrix.command.CustomCommand.metrics.rollingStats.timeInMilliseconds=10000
项 | 值 |
---|---|
默认值 | 10 |
可选值 | 须要知足metrics.rollingStats.timeInMilliseconds % metrics.rollingStats.numBuckets == 0 ,要尽可能小,不然有可能影响性能 |
默认全局配置 | hystrix.command.default.metrics.rollingStats.numBuckets |
实例配置 | hystrix.command.[HystrixCommandKey].metrics.rollingStats.numBuckets |
建议(笔者备注) | 建议保持默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withMetricsRollingStatisticalWindowBuckets(100))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.metrics.rollingStats.numBuckets=10 # 实例配置 hystrix.command.CustomCommand.metrics.rollingStats.numBuckets=10
项 | 值 |
---|---|
默认值 | true |
可选值 | true 、false |
默认全局配置 | hystrix.command.default.metrics.rollingPercentile.enabled |
实例配置 | hystrix.command.[HystrixCommandKey].metrics.rollingPercentile.enabled |
建议(笔者备注) | 建议保持默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withMetricsRollingPercentileEnabled(true))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.metrics.rollingPercentile.enabled=true # 实例配置 hystrix.command.CustomCommand.metrics.rollingPercentile.enabled=true
项 | 值 |
---|---|
默认值 | 60000 |
可选值 | - |
默认全局配置 | hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds |
实例配置 | hystrix.command.[HystrixCommandKey].metrics.rollingPercentile.timeInMilliseconds |
建议(笔者备注) | 建议保持默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withMetricsRollingPercentileWindowInMilliseconds(60000))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds=60000 # 实例配置 hystrix.command.CustomCommand.metrics.rollingPercentile.timeInMilliseconds=60000
项 | 值 |
---|---|
默认值 | 6 |
可选值 | 知足metrics.rollingPercentile.timeInMilliseconds % metrics.rollingPercentile.numBuckets == 0 ,要尽可能小,不然有可能影响性能 |
默认全局配置 | hystrix.command.default.metrics.rollingPercentile.numBuckets |
实例配置 | hystrix.command.[HystrixCommandKey].metrics.rollingPercentile.numBuckets |
建议(笔者备注) | 建议保持默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withMetricsRollingPercentileWindowBuckets(6))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.metrics.rollingPercentile.numBuckets=6 # 实例配置 hystrix.command.CustomCommand.metrics.rollingPercentile.numBuckets=6
项 | 值 |
---|---|
默认值 | 100 |
可选值 | - |
默认全局配置 | hystrix.command.default.metrics.rollingPercentile.bucketSize |
实例配置 | hystrix.command.[HystrixCommandKey].metrics.rollingPercentile.bucketSize |
建议(笔者备注) | 建议保持默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withMetricsRollingPercentileBucketSize(100))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.metrics.rollingPercentile.bucketSize=100 # 实例配置 hystrix.command.CustomCommand.metrics.rollingPercentile.bucketSize=100
项 | 值 |
---|---|
默认值 | 500 |
可选值 | - |
默认全局配置 | hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds |
实例配置 | hystrix.command.[HystrixCommandKey].metrics.healthSnapshot.intervalInMilliseconds |
建议(笔者备注) | 建议保持默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withMetricsHealthSnapshotIntervalInMilliseconds(500))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds=500 # 实例配置 hystrix.command.CustomCommand.metrics.healthSnapshot.intervalInMilliseconds=500
请求上下文属性主要涉及到HystrixRequestContext
和HystrixCommand
的使用。
项 | 值 |
---|---|
默认值 | true |
可选值 | true 、false |
默认全局配置 | hystrix.command.default.requestCache.enabled |
实例配置 | hystrix.command.[HystrixCommandKey].requestCache.enabled |
建议(笔者备注) | 建议保持默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withRequestCacheEnabled(true))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.requestCache.enabled=true # 实例配置 hystrix.command.CustomCommand.requestCache.enabled=true
项 | 值 |
---|---|
默认值 | true |
可选值 | true 、false |
默认全局配置 | hystrix.command.default.requestLog.enabled |
实例配置 | hystrix.command.[HystrixCommandKey].requestLog.enabled |
建议(笔者备注) | 建议保持默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withRequestLogEnabled(true))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.command.default.requestLog.enabled=true # 实例配置 hystrix.command.CustomCommand.requestLog.enabled=true
请求合成器配置主要控制HystrixCollapser
的行为。
项 | 值 |
---|---|
默认值 | Integer.MAX_VALUE |
可选值 | - |
默认全局配置 | hystrix.collapser.default.maxRequestsInBatch |
实例配置 | hystrix.collapser.[HystrixCollapserKey].maxRequestsInBatch |
建议(笔者备注) | 建议保持默认值 |
编程式配置:
public class CustomHystrixCollapser extends HystrixCollapser<List<String>, String, String> { public CustomHystrixCollapser(Setter setter) { super(Setter.withCollapserKey(HystrixCollapserKey.Factory.asKey("CustomHystrixCollapser")) .andCollapserPropertiesDefaults(HystrixCollapserProperties.Setter() .withMaxRequestsInBatch(10))); } @Override public String getRequestArgument() { return null; } @Override protected HystrixCommand<List<String>> createCommand(Collection<CollapsedRequest<String, String>> collapsedRequests) { return null; } @Override protected void mapResponseToRequests(List<String> batchResponse, Collection<CollapsedRequest<String, String>> collapsedRequests) { } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.collapser.default.maxRequestsInBatch=10 # 实例配置 hystrix.collapser.CustomHystrixCollapser.maxRequestsInBatch=10
项 | 值 |
---|---|
默认值 | 10 |
可选值 | - |
默认全局配置 | hystrix.collapser.default.timerDelayInMilliseconds |
实例配置 | hystrix.collapser.[HystrixCollapserKey].timerDelayInMilliseconds |
建议(笔者备注) | 建议保持默认值 |
编程式配置:
public class CustomHystrixCollapser extends HystrixCollapser<List<String>, String, String> { public CustomHystrixCollapser(Setter setter) { super(Setter.withCollapserKey(HystrixCollapserKey.Factory.asKey("CustomHystrixCollapser")) .andCollapserPropertiesDefaults(HystrixCollapserProperties.Setter() .withTimerDelayInMilliseconds(10))); } @Override public String getRequestArgument() { return null; } @Override protected HystrixCommand<List<String>> createCommand(Collection<CollapsedRequest<String, String>> collapsedRequests) { return null; } @Override protected void mapResponseToRequests(List<String> batchResponse, Collection<CollapsedRequest<String, String>> collapsedRequests) { } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.collapser.default.timerDelayInMilliseconds=10 # 实例配置 hystrix.collapser.CustomHystrixCollapser.timerDelayInMilliseconds=10
项 | 值 |
---|---|
默认值 | true |
可选值 | true 、false |
默认全局配置 | hystrix.collapser.default.requestCache.enabled |
实例配置 | hystrix.collapser.[HystrixCollapserKey].requestCache.enabled |
建议(笔者备注) | 建议保持默认值 |
编程式配置:
public class CustomHystrixCollapser extends HystrixCollapser<List<String>, String, String> { public CustomHystrixCollapser(Setter setter) { super(Setter.withCollapserKey(HystrixCollapserKey.Factory.asKey("CustomHystrixCollapser")) .andCollapserPropertiesDefaults(HystrixCollapserProperties.Setter() .withTimerDelayInMilliseconds(10))); } @Override public String getRequestArgument() { return null; } @Override protected HystrixCommand<List<String>> createCommand(Collection<CollapsedRequest<String, String>> collapsedRequests) { return null; } @Override protected void mapResponseToRequests(List<String> batchResponse, Collection<CollapsedRequest<String, String>> collapsedRequests) { } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.collapser.default.requestCache.enabled=true # 实例配置 hystrix.collapser.CustomHystrixCollapser.requestCache.enabled=true
Hystrix
使用的是JUC线程池ThreadPoolExecutor
,线程池相关配置直接影响ThreadPoolExecutor
实例。Hystrix
的命令执行选用了线程池策略,那么就是经过线程池隔离执行的,最好为每个分组设立独立的线程池。笔者在生产实践的时候,通常把HystrixCommandGroupKey
和HystrixThreadPoolKey
设置为一致。
项 | 值 |
---|---|
默认值 | 10 |
可选值 | - |
默认全局配置 | hystrix.threadpool.default.coreSize |
实例配置 | hystrix.threadpool.[HystrixThreadPoolKey].coreSize |
建议(笔者备注) | 根据真实状况自行配置和调整 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() .withCoreSize(10))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.threadpool.default.coreSize=10 # 实例配置 hystrix.threadpool.CustomCommand.coreSize=10
此属性只有在allowMaximumSizeToDivergeFromCoreSize
为true
的时候才生效。
项 | 值 |
---|---|
默认值 | 10 |
可选值 | - |
默认全局配置 | hystrix.threadpool.default.maximumSize |
实例配置 | hystrix.threadpool.[HystrixThreadPoolKey].maximumSize |
建议(笔者备注) | 根据真实状况自行配置和调整 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() .withMaximumSize(10))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.threadpool.default.maximumSize=10 # 实例配置 hystrix.threadpool.CustomCommand.maximumSize=10
此属性配置为-1时使用的是SynchronousQueue
,配置为大于1的整数时使用的是LinkedBlockingQueue
。
项 | 值 |
---|---|
默认值 | -1 |
可选值 | -1 或者大于0的整数 |
默认全局配置 | hystrix.threadpool.default.maxQueueSize |
实例配置 | hystrix.threadpool.[HystrixThreadPoolKey].maxQueueSize |
建议(笔者备注) | 根据真实状况自行配置和调整 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() .withMaxQueueSize(-1))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.threadpool.default.maxQueueSize=-1 # 实例配置 hystrix.threadpool.CustomCommand.maxQueueSize=-1
当maxQueueSize
配置为-1的时候,此配置项不生效。
项 | 值 |
---|---|
默认值 | 5 |
可选值 | 大于0的整数 |
默认全局配置 | hystrix.threadpool.default.queueSizeRejectionThreshold |
实例配置 | hystrix.threadpool.[HystrixThreadPoolKey].queueSizeRejectionThreshold |
建议(笔者备注) | 根据真实状况自行配置和调整 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() .withQueueSizeRejectionThreshold(5))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.threadpool.default.queueSizeRejectionThreshold=5 # 实例配置 hystrix.threadpool.CustomCommand.queueSizeRejectionThreshold=5
当allowMaximumSizeToDivergeFromCoreSize
为true
而且maximumSize
大于coreSize
时此配置才生效。
项 | 值 |
---|---|
默认值 | 1 |
可选值 | 大于0的整数 |
默认全局配置 | hystrix.threadpool.default.keepAliveTimeMinutes |
实例配置 | hystrix.threadpool.[HystrixThreadPoolKey].keepAliveTimeMinutes |
建议(笔者备注) | 根据真实状况自行配置和调整 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() .withKeepAliveTimeMinutes(1))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.threadpool.default.keepAliveTimeMinutes=1 # 实例配置 hystrix.threadpool.CustomCommand.keepAliveTimeMinutes=1
项 | 值 |
---|---|
默认值 | false |
可选值 | true 、false |
默认全局配置 | hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize |
实例配置 | hystrix.threadpool.[HystrixThreadPoolKey].allowMaximumSizeToDivergeFromCoreSize |
建议(笔者备注) | 根据真实状况自行配置和调整 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() .withAllowMaximumSizeToDivergeFromCoreSize(true))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=true # 实例配置 hystrix.threadpool.CustomCommand.allowMaximumSizeToDivergeFromCoreSize=true
项 | 值 |
---|---|
默认值 | 10000 |
可选值 | - |
默认全局配置 | hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds |
实例配置 | hystrix.threadpool.[HystrixThreadPoolKey].metrics.rollingStats.timeInMilliseconds |
建议(笔者备注) | 建议使用默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() .withMetricsRollingStatisticalWindowInMilliseconds(10000))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds=10000 # 实例配置 hystrix.threadpool.CustomCommand.metrics.rollingStats.timeInMilliseconds=10000
项 | 值 |
---|---|
默认值 | 10 |
可选值 | 知足metrics.rollingStats.timeInMilliseconds % metrics.rollingStats.numBuckets == 0 ,值要尽可能少,不然会影响性能 |
默认全局配置 | hystrix.threadpool.default.metrics.rollingStats.numBuckets |
实例配置 | hystrix.threadpool.[HystrixThreadPoolKey].metrics.rollingStats.numBuckets |
建议(笔者备注) | 建议使用默认值 |
编程式配置:
public class CustomCommand extends HystrixCommand<String> { public CustomCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CustomCommand")) .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() .withMetricsRollingStatisticalWindowBuckets(10))); } @Override protected String run() throws Exception { return null; } }
配置文件中(Properties)配置:
# 下面配置二选一 # 默认全局配置 hystrix.threadpool.default.metrics.rollingStats.numBuckets=10 # 实例配置 hystrix.threadpool.CustomCommand.metrics.rollingStats.numBuckets=10
(本文完 e-a-201890602 1:00 AM c-3-d)