Netflix Hystrix - 隔离参数

289 阅读5分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。


线程池隔离参数

参数作用默认值备注
groupKey服务名(相同服务用一个名称,如商品、用户等等)getClass0.getSimpleName在consumer里面为每个provider服务,设置group标识,一个group使用一个线程池
commandKey接口(服务下面的接口,如购买商品)当前执行方法名consumer的接口名称
threadPoolKey线程池的名称:配置全局唯一标识线程池的名称,相同线程池名称的线程池是同一个。默认是分组名groupKey配置全局唯一标识线程池的名称,相同线程池名称的线程池是同一个。
coreSize线程池大小:这是最大的并发执行数量。10设置标准:每秒最大支撑的请求数(99%平均响应时间+一个缓冲值) 比如:每秒能处理1000个请求,99%的请求响应时间10是60ms,那么公式是:1000*(0.060+0.012)
maxQueueSize最大队列长度:设置BlockingQueue的最大长度默认值:-1如果使用正数,队列将从同步队列(SynchronousQueue)改为阻塞队列(LinkedBlockingQueue)
queueSizeRejectionThreshold拒绝请求:设置拒绝请求的临界值5此属性不适用于maxQueueSize=-1时。设置设个值的原因是maxOueueSize值运行时不能改变,我们可以通过修改这个变量动态修改允许排队的长度
keepAliveTimeMinutes线程存活时间:设置存活时间,单位分钟。1分钟控制一个线程从实用完成到被释放的时间

信号量隔离参数

参数作用默认值备注
execution.isolation.strategy隔离等策略配置项THREAD只有2种THREAD和SEMAPHORE
execution.isolation.thread.timeoutInMilliseconds超时时间1000ms1.在THREAD模式下,达到超时时间,自动中断;2.在SEMAPHORE模式下,会等待执行完成后,再去判断是否超时
execution.isolation.thread.interruptOnTimeout是否打开超时线程中断TRUETHREAD模式有效
execution.isolation.semaphore.maxConcurrentRequests信号量量最大并发度10SEMAPHORE模式有效
fallback.isolation.semaphore.maxConcurrentRequestsfallbaack最大并发度10SEMAPHORE模式有效

线程池和信号量的区别?

线程池隔离信号量隔离
线程请求线程和调用provider线程不是同一条线程请求线程和调用provider线程是同一条线程
开销排队、调度、上下文开销等无线程切换,开销低
异步支持不支持
并发支持支持(最大线程池大小)支持(最大信号量上限)
传递Header无法传递http Header可以传递http Header
支持超时能支持超时不支持超时

1.什么情况下,用线程池隔离?

请求并发量大,并且耗时长(请求耗时长一般是计算量大,或读数据库):采用线程隔离策略,这样的话,可以保证大量的容器(tomcat)线程可用,不会由于服务原因,一直处于阻塞或等待状态,快速失败返回。

2.什么情况下,用信号量隔离?

请求并发是大,并目耗时短(请求耗时短可能是计算量小,或读缓存):采用信号量隔离策略,因为这类服务的返回通常会非常的快,不会占用容器线程太长时间,而且也减少了线程切换的一些开销,提高了缓存服务的效率。

Hystrix yml

# 命令配置.默认全局配置.命令执行(execution)配置.
# 是否允许超时:true(默认值)、false
hystrix.command.default.execution.timeout.enabled=true
# 命令配置.默认全局配置.命令执行(execution)配置.
# 隔离策略:THREAD(默认值)、SEMAPHORE
hystrix.command.default.execution.isolation.strategy=THREAD
# 命令配置.默认全局配置.命令执行(execution)配置.
# 当发生取消时,执行是否应该中断:true、false(默认值)
hystrix.command.default.execution.isolation.thread.interruptOnCancel=false
# 命令配置.默认全局配置.命令执行(execution)配置.
# 在发生超时时是否应中断:true(默认值)、false
hystrix.command.default.execution.isolation.thread.interruptOnTimeout=true
# 命令配置.默认全局配置.命令执行(execution)配置.
# 超时时间上限:单位是毫秒 默认1000
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000
# 命令配置.默认全局配置.命令执行(execution)配置.
# 最大并发请求上限(SEMAPHORE):10(默认值);
hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests=10
# =================================================== #
#### # 命令配置.默认全局配置.命令降级(fallback)配置 # ####
# =================================================== #
# 命令配置.默认全局配置.命令降级(fallback)配置.
# 是否开启降级:false、true(默认值)
hystrix.command.default.fallback.enabled=true
# 命令配置.默认全局配置.命令降级(fallback)配置.
# 最大并发降级请求处理上限:10(默认值)
hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests=10
hystrix.command.default.circuitBreaker.enabled=true
hystrix.command.default.circuitBreaker.requestVolumeThreshold=10
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=5000
hystrix.command.default.circuitBreaker.errorThresholdPercentage=50
hystrix.command.default.circuitBreaker.forceOpen=false
hystrix.command.default.circuitBreaker.forceClosed=false
hystrix.command.default.metrics.rollingStats.timeInMilliseconds=10000
hystrix.command.default.metrics.rollingStats.numBuckets=10
hystrix.command.default.metrics.rollingPercentile.enabled=true
hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds=60000
hystrix.command.default.metrics.rollingPercentile.numBuckets=6
hystrix.command.default.metrics.rollingPercentile.bucketSize=100
hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds=500
hystrix.command.default.requestCache.enabled=true
hystrix.command.default.requestLog.enabled=true
##### 命令配置.实例配置.命令降级(fallback)配置. ####
##### 命令配置.实例配置.命令降级(fallback)配置. ####
hystrix.command.ProductFacadeServiceFeign#getById(Long).requestLog.enabled=true
hystrix.command.ProductFacadeServiceFeign#getById(Long).requestCache.enabled=true
hystrix.command.ProductFacadeServiceFeign#getById(Long).metrics.healthSnapshot.intervalInMilliseconds=500
hystrix.command.ProductFacadeServiceFeign#getById(Long).metrics.rollingPercentile.bucketSize=100
hystrix.command.ProductFacadeServiceFeign#getById(Long).metrics.rollingPercentile.numBuckets=6
hystrix.command.ProductFacadeServiceFeign#getById(Long).metrics.rollingPercentile.timeInMilliseconds=60000
hystrix.command.ProductFacadeServiceFeign#getById(Long).metrics.rollingPercentile.enabled=true
hystrix.command.ProductFacadeServiceFeign#getById(Long).metrics.rollingStats.numBuckets=10
hystrix.command.ProductFacadeServiceFeign#getById(Long).metrics.rollingStats.timeInMilliseconds=10000
hystrix.command.ProductFacadeServiceFeign#getById(Long).circuitBreaker.forceClosed=false
hystrix.command.ProductFacadeServiceFeign#getById(Long).circuitBreaker.forceOpen=false
hystrix.command.ProductFacadeServiceFeign#getById(Long).circuitBreaker.errorThresholdPercentage=50
hystrix.command.ProductFacadeServiceFeign#getById(Long).circuitBreaker.sleepWindowInMilliseconds=5000
hystrix.command.ProductFacadeServiceFeign#getById(Long).circuitBreaker.requestVolumeThreshold=10
hystrix.command.ProductFacadeServiceFeign#getById(Long).circuitBreaker.enabled=true
hystrix.command.ProductFacadeServiceFeign#getById(Long).fallback.enabled=true
hystrix.command.ProductFacadeServiceFeign#getById(Long).fallback.isolation.semaphore.maxConcurrentRequests=10
hystrix.command.ProductFacadeServiceFeign#getById(Long).execution.timeout.enabled=true
hystrix.command.ProductFacadeServiceFeign#getById(Long).execution.isolation.semaphore.maxConcurrentRequests=10
hystrix.command.ProductFacadeServiceFeign#getById(Long).execution.isolation.thread.interruptOnCancel=false
hystrix.command.ProductFacadeServiceFeign#getById(Long).execution.isolation.thread.interruptOnTimeout=true
hystrix.command.ProductFacadeServiceFeign#getById(Long).execution.isolation.thread.timeoutInMilliseconds=1000
hystrix.command.ProductFacadeServiceFeign#getById(Long).execution.isolation.strategy=THREAD=THREAD
hystrix.collapser.default.maxRequestsInBatch=10
hystrix.collapser.default.timerDelayInMilliseconds=10
hystrix.collapser.default.requestCache.enabled=true
hystrix.collapser.ProductFacadeServiceFeign#getById(Long).requestCache.enabled=true
hystrix.collapser.ProductFacadeServiceFeign#getById(Long).timerDelayInMilliseconds=10
hystrix.collapser.ProductFacadeServiceFeign#getById(Long).maxRequestsInBatch=10
hystrix.threadpool.default.coreSize=10
hystrix.threadpool.default.maximumSize=10
hystrix.threadpool.default.maxQueueSize=-1
hystrix.threadpool.default.queueSizeRejectionThreshold=5
hystrix.threadpool.default.keepAliveTimeMinutes=1
hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=true
hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds=10000
hystrix.threadpool.default.metrics.rollingStats.numBuckets=10
hystrix.threadpool.ProductFacadeServiceFeign#getById(Long).metrics.rollingStats.numBuckets=10
hystrix.threadpool.ProductFacadeServiceFeign#getById(Long).metrics.rollingStats.timeInMilliseconds=10000
hystrix.threadpool.ProductFacadeServiceFeign#getById(Long).allowMaximumSizeToDivergeFromCoreSize=true
hystrix.threadpool.ProductFacadeServiceFeign#getById(Long).keepAliveTimeMinutes=1
hystrix.threadpool.ProductFacadeServiceFeign#getById(Long).queueSizeRejectionThreshold=5
hystrix.threadpool.ProductFacadeServiceFeign#getById(Long).maxQueueSize=-1
hystrix.threadpool.ProductFacadeServiceFeign#getById(Long).maximumSize=10
hystrix.threadpool.ProductFacadeServiceFeign#getById(Long).coreSize=10

命令执行状态

hystrix命令的状态是不可逆的,每个hystrix命令只能使用一次:

1. 初始状态(NOT_STARTED)
2. 创建(OBSERVABLE_CHAIN_CREATED),toObservable开始执行后变为OBSERVABLE_CHAIN_CREATED状态
3.用户代码执行( USER_CODE_EXECUTED),用户调用开始执行前变为USER_CODE_EXECUTED
4.UNSUBSCRIBED,
5.TERMINAL

hystrix命令执行过程中线程状态:

1. 初始状态(NOT_USING_THREAD)。
2. 开始(STARTED),通过线程池方式执行命令前变为STARTED。
3. UNSUBSCRIBED,
4. TERMINAL

hystrix命令执行过程中超时状态

1. NOT_EXECUTED
2. COMPLETED
3. TIMED_OUT
protected enum TimedOutStatus {
    NOT_EXECUTED, COMPLETED, TIMED_OUT
}

protected enum CommandState {
    NOT_STARTED, OBSERVABLE_CHAIN_CREATED, USER_CODE_EXECUTED, UNSUBSCRIBED, TERMINAL
}

protected enum ThreadState {
    NOT_USING_THREAD, STARTED, UNSUBSCRIBED, TERMINAL
}