参数配置
参数说明 |
值 |
备注 |
groupKey |
productStockOpLog |
group标识,一个group使用一个线程池 |
commandKey |
addProductStockOpLog |
command标识 |
fallbackMethod |
addProductStockOpLogFallback |
fallback方法,两者需要返回值和参数相同 |
超时时间设置 |
400ms |
执行策略,在THREAD模式下,达到超时时间,可以中断 For most circuits, you should try to set their timeout values close to the 99.5th percentile of a normal healthy system so they will cut off bad requests and not let them take up system resources or affect user behavior. |
统计窗口(10s)内最少请求数 |
10 |
熔断策略 |
熔断多少秒后去尝试请求 |
5s |
熔断策略,默认值 |
熔断阀值 |
10% |
熔断策略:一个统计窗口内有10%的请求处理失败,会触发熔断 |
线程池coreSize |
10 |
默认值(推荐值).在当前项目中,需要做依赖隔离的方法为发送一条MQ消息,发送MQ消息方法的TP99耗时在1ms以下,近2周单机QPS最高值在18左右,经过灰度验证了午高峰后(当日QPS>上周末QPS),ActiveThreads<=2,rejected=0,经过压测后得出结论:线程池大小为10足以应对2000QPS,前提发送MQ消息时耗时正常(该部分为实际项目中的情况,在此不做详述) |
线程池maxQueueSize |
-1 |
即线程池队列为SynchronousQueue |
参数说明
其他参数可参见 https://github.com/Netflix/Hystrix/wiki/Con
分类 |
参数 |
作用 |
默认值 |
备注 |
基本参数 |
groupKey |
表示所属的group,一个group共用线程池 |
getClass().getSimpleName(); |
基本参数 |
commandKey |
|
当前执行方法名 |
Execution ( 控制HystrixCommand.run()的执行策略) |
execution.isolation.strategy |
隔离策略,有THREAD和SEMAPHORE THREAD |
以下几种可以使用SEMAPHORE模式: 只想控制并发度 外部的方法已经做了线程隔离 调用的是本地方法或者可靠度非常高、耗时特别小的方法(如medis) |
Execution |
execution.isolation.thread.timeoutInMilliseconds |
超时时间 |
1000ms |
默认值:1000 在THREAD模式下,达到超时时间,可以中断 在SEMAPHORE模式下,会等待执行完成后,再去判断是否超时 设置标准: 有retry,99meantime+avg meantime 没有retry,99.5meantime |
Execution |
execution.timeout.enabled |
是否打开超时 |
true |
Execution |
execution.isolation.thread.interruptOnTimeout |
是否打开超时线程中断 |
true |
THREAD模式有效 |
Execution |
execution.isolation.semaphore.maxConcurrentRequests |
信号量最大并发度 |
10 |
SEMAPHORE模式有效 |
Fallback ( 设置当fallback降级发生时的策略) |
fallback.isolation.semaphore.maxConcurrentRequests |
fallback最大并发度 |
10 |
Fallback |
fallback.enabled |
fallback是否可用 |
true |
Circuit Breaker (配置熔断的策略) |
circuitBreaker.enabled |
是否开启熔断 |
true |
Circuit Breaker |
circuitBreaker.requestVolumeThreshold |
一个统计窗口内熔断触发的最小个数/10s |
20 |
Circuit Breaker |
circuitBreaker.sleepWindowInMilliseconds |
熔断多少秒后去尝试请求 |
5000ms |
Circuit Breaker |
circuitBreaker.errorThresholdPercentage |
失败率达到多少百分比后熔断 |
50 |
主要根据依赖重要性进行调整 |
Circuit Breaker |
circuitBreaker.forceOpen |
是否强制开启熔断 |
Circuit Breaker |
circuitBreaker.forceClosed |
是否强制关闭熔断 |
|
如果是强依赖,应该设置为true |
Metrics (设置关于HystrixCommand执行需要的统计信息) |
metrics.rollingStats.timeInMilliseconds |
设置统计滚动窗口的长度,以毫秒为单位。用于监控和熔断器。 |
10000 |
滚动窗口被分隔成桶(bucket),并且进行滚动。 例如这个属性设置10s(10000),一个桶是1s。 |
Metrics |
metrics.rollingStats.numBuckets 设置统计窗口的桶数量 |
10 |
metrics.rollingStats.timeInMilliseconds必须能被这个值整除 |
Metrics |
metrics.rollingPercentile.enabled |
设置执行时间是否被跟踪,并且计算各个百分比,50%,90%等的时间 |
true |
Metrics |
metrics.rollingPercentile.timeInMilliseconds |
设置执行时间在滚动窗口中保留时间,用来计算百分比 |
60000ms |
Metrics |
metrics.rollingPercentile.numBuckets |
设置rollingPercentile窗口的桶数量 |
6 |
metrics.rollingPercentile.timeInMilliseconds必须能被这个值整除 |
Metrics |
metrics.rollingPercentile.bucketSize |
此属性设置每个桶保存的执行时间的最大值。 |
100 |
如果设置为100,但是有500次求情,则只会计算最近的100次 |
Metrics |
metrics.healthSnapshot.intervalInMilliseconds |
采样时间间隔 |
500 |
Request Context ( 设置HystrixCommand使用的HystrixRequestContext相关的属性) |
requestCache.enabled |
设置是否缓存请求,request-scope内缓存 |
true |
Request Context |
requestLog.enabled |
设置HystrixCommand执行和事件是否打印到HystrixRequestLog中 |
|
|
ThreadPool Properties(配置HystrixCommand使用的线程池的属性) |
coreSize |
设置线程池的core size,这是最大的并发执行数量。 |
10 |
设置标准:coreSize = requests per second at peak when healthy × 99th percentile latency in seconds + some breathing room 大多数情况下默认的10个线程都是值得建议的 |
ThreadPool Properties |
maxQueueSize |
最大队列长度。设置BlockingQueue的最大长度 |
-1 |
默认值:-1 如果使用正数,队列将从SynchronousQueue改为LinkedBlockingQueue |
ThreadPool Properties |
queueSizeRejectionThreshold |
设置拒绝请求的临界值 |
5 |
此属性不适用于maxQueueSize = - 1时 设置设个值的原因是maxQueueSize值运行时不能改变,我们可以通过修改这个变量动态修改允许排队的长度 |
ThreadPool Properties |
keepAliveTimeMinutes |
设置keep-live时间 |
1分钟 |
这个一般用不到因为默认corePoolSize和maxPoolSize是一样的。 |