Hystrix属性配置
-
Command可配参数
设置隔离策略
execution.isolation.strategy = THREAD
设置超时时间
execution.isolation.thread.timeoutInMilliseconds = 1000
信号量隔离策略设置最大并发请求数(仅在信号量隔离策略下生效)
execution.isolation.semaphore.maxConcurrentRequests = 10
设置最大Fallback数量
fallback.isolation.semaphore.maxConcurrentRequests = 10
设置熔断器滑动窗口最小任务
circuitBreaker.requestVolumeThreshold = 20
设置熔断器持续时间
circuitBreaker.sleepWindowInMilliseconds = 5000
设置触发熔断器的失败任务阈值(百分比)
circuitBreaker.errorThresholdPercentage = 50
设置Metrics监视器的范围时间(过去多少ms内)
metrics.rollingStats.timeInMilliseconds = 10000
设置监视器内桶的数量(将监视器范围划分为若干块)
metrics.rollingStats.numBuckets= 10
-
ThreadPool可配参数
设置线程池容量
coreSize = 10
设置阻塞队列长度(优先级高于queueSizeRejectionThreshold,且一旦初始化就不能更改 )
maxQueueSize = -1
动态设置阻塞队列长度
queueSizeRejectionThreshold = 5
空闲线程存活时间
keepAliveTimeMinutes= 1
线程池监控窗口时间范围(10s内)
metrics.rollingStats.timeInMilliseconds = 10000
设置线程池监控滑动窗口的桶数量
metrics.rollingStats.numBuckets = 500
Note:窗口时间必须为桶数量的整数倍,否则会抛出异常
建议设置 选择设置 了解含义
属性
|
含义
|
默认值
|
备注
|
设置方法
|
---|---|---|---|---|
isolation.thread.timeoutInMilliseconds | 线程超时时间 | 1000ms | 在THREAD模式下,达到超时时间,可以中断 在SEMAPHORE模式下,会等待执行完成后,再去判断是否超时 | HystrixCommandProperties.Setter() .withExecutionTimeoutInMilliseconds(int value) |
GroupKey | 组名称 | getClass().getSimpleName() | | |
CommandKey | 命令名称 | 当前执行方法名 | | |
threadpool.coreSize | 核心线程池大小 | 10 | HystrixThreadPoolProperties.Setter().withCoreSize(int value) | |
isolation.strategy | 依赖隔离策略 | THREAD | 以下几种可以使用SEMAPHORE模式:
| // to use semaphore isolation HystrixCommandProperties.Setter() .withExecutionIsolationStrategy(ExecutionIsolationStrategy.SEMAPHORE) 线程池 VS 信号量 默认使用线程池,信号量只用于调用量比较大,单独的线程的开销太高; 这通常仅适用于非网络呼叫 |
circuitBreaker.requestVolumeThreshold | 熔断最小并发量 | 20 | HystrixCommandProperties.Setter() .withCircuitBreakerRequestVolumeThreshold(int value) | |
circuitBreaker.sleepWindowInMilliseconds | 5000 | HystrixCommandProperties.Setter() .withCircuitBreakerSleepWindowInMilliseconds(int value) | ||
circuitBreaker.errorThresholdPercentage | 50 | HystrixCommandProperties.Setter() .withCircuitBreakerErrorThresholdPercentage(int value) | ||
circuitBreaker.forceOpen | false | 手动熔断降级 | HystrixCommandProperties.Setter() .withCircuitBreakerForceOpen(boolean value) | |
circuitBreaker.forceClose | false | 核心依赖需要设置成true | HystrixCommandProperties.Setter() .withCircuitBreakerForceClosed(boolean value) | |
requestCache.enabled | 是否打开请求缓存 | false | HystrixCollapserProperties.Setter() .withRequestCacheEnabled(boolean value) | |
threadpool.maxQueueSize | 队列大小 | -1 |
HystrixThreadPoolProperties.Setter() .withMaxQueueSize(int value) |
关于Hystrix线程池:
- 默认使用同步队列, 使用
LinkedBlockingQueue时可以设置
- v1.5.9之前, coreSize=maxSize, 之后设置allowMaximumSizeToDivergeFromCoreSize可以将两者设置不同
- 设置尽量小的线程池, 使用推荐方法预估线程池
Thread Size = peak healthy × 99th percentile latency in seconds + some breathing room(线程池大小 = 峰值QPS * 99耗时 + 预留空间 )
超时设定标准:
With retry,Time = 99th mean-time + 50th mean-time
Without Retry,Time = 99.5meantime
资源附录
URL
|
描述
|
---|---|
https://github.com/Netflix/Hystrix | github官网 |
https://xieyanze.gitbooks.io/hystrix-document/ | 官方wiki汉译版 |
http://www.sczyh30.com/posts/Microservice/netflix-hystrix-how-it-works-summary/ | HystrixCircuitBreaker源码分析 |
TODO:
1.fallback失败会怎么处理? 没有fallback,hysrix会把执行或者超时的异常抛出去。fallback执行出错, 也会把异常直接抛出去。 2.熔断条件 默认20个请求怎么理解? A.设置窗口(每10s)的最小请求数量(默认是20),只有在数量超过设定值(20)时,才有可能触发熔断,否则永远不会熔断(即熔断器打开);可以说是熔断器是否打开的先决条件。
B.在请求数量超过设定值(20)时,才回去判断失败率(超时、线程拒绝、信号量拒绝、失败)是否达到熔断器设定的健康值,达到了熔断器熔断。
3.@override 作用? A.该注解表示子类的方法是重写的父类的方法,加上该注解会校验子类方法书写的合法性,比方说拼写错误、参数不一致等情况。
B.不加@override注解,重写了父类的方法,也不会有问题。新建的子类对象,最终调用的也是子类重写的方法。(多态)
C.为了避免出错,在重写父类方法时,建议加上@override注解。
4.超时异常是怎么抛出来的, 超时后是否走finally ?
Hystrix通过定时器控制超时, 超时异常抛出不会走run方法正常流程,直接走fallback。如果没有设置fallback则直接抛异常。