hystrix参数详解

1.Hystrix参数

HystrixCommand.Setter核心参数

HystrixCommandGroupKey:区分一组服务,一般以接口为粒度。
HystrixCommandKey:区分一个方法,一般以方法为粒度。
HystrixThreadPoolKey:一个HystrixThreadPoolKey下的所有方法共用一个线程池。
HystrixThreadPoolProperties:线程池配置
HystrixCommandProperties:基本配置
基于以上组合

  • 接口+线程池粒度隔离线程
  • 方法+线程池粒度隔离线程
  • 接口+线程池&方法+线程池。混合隔离粒度

Command Properties

Execution相关参数详解

  • hystrix.command.default.execution.isolation.strategy 隔离策略,默认是Thread,可选Thread|Semaphore。thread用于线程池的隔离,一般适用于同步请求。semaphore是信号量模式,适用于异步请求
  • hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds 命令执行超时时间,默认1000ms
  • hystrix.command.default.execution.timeout.enabled 执行是否启用超时,默认启用true
  • hystrix.command.default.execution.isolation.thread.interruptOnTimeout 发生超时是是否中断,默认true
  • hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests 最大并发请求数,默认10,该参数当使用ExecutionIsolationStrategy.SEMAPHORE策略时才有效。如果达到最大并发请求数,请求会被拒绝。理论上选择semaphore size的原则和选择thread size一致,但选用semaphore时每次执行的单元要比较小且执行速度快(ms级别),否则的话应该用thread。

Fallback 降级配置

这些参数可以应用于Hystrix的THREAD和SEMAPHORE策略
- hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests 如果并发数达到该设置值,请求会被拒绝和抛出异常并且fallback不会被调用。默认10
- hystrix.command.default.fallback.enabled 当执行失败(run方法抛异常)或者请求被拒绝(资源不足),是否会尝试调用hystrixCommand.getFallback() 。默认true

Circuit Breaker 熔断器配置

  • hystrix.command.default.circuitBreaker.enabled 用来跟踪circuit的健康性,如果未达标则让request短路。默认true
  • hystrix.command.default.circuitBreaker.requestVolumeThreshold 一个rolling window内最小的请求数。如果设为20,那么当一个rolling window的时间内(比如说1个rolling window是10秒)收到19个请求,即使19个请求都失败,也不会触发circuit break。默认20
  • hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds 触发短路的时间值,当该值设为5000时,则当触发circuit break后的5000毫秒内都会拒绝request,也就是5000毫秒后才会关闭circuit。默认5000
  • hystrix.command.default.circuitBreaker.errorThresholdPercentage错误比率阀值,如果错误率>=该值,circuit会被打开,并短路所有请求触发fallback。默认50,一般服务错误率达到10%时,服务已经不可用了,所以一般建议设置到10以下。
  • hystrix.command.default.circuitBreaker.forceOpen 强制打开熔断器,如果打开这个开关,那么拒绝所有request,默认false
  • hystrix.command.default.circuitBreaker.forceClosed 强制关闭熔断器 如果这个开关打开,circuit将一直关闭且忽略circuitBreaker.errorThresholdPercentage

Metrix 健康统计配置

  • hystrix.command.default.metrics.rollingStats.timeInMilliseconds 设置统计的时间窗口值的,毫秒值,circuit break 的打开会根据1个rolling window的统计来计算。若rolling window被设为10000毫秒,则rolling window会被分成n个buckets,每个bucket包含success,failure,timeout,rejection的次数的统计信息。默认10000
  • hystrix.command.default.metrics.rollingStats.numBuckets 设置一个rolling window被划分的数量,若numBuckets=10,rolling window=10000,那么一个bucket的时间即1秒。必须符合rolling window % numberBuckets == 0。默认10
  • hystrix.command.default.metrics.rollingPercentile.enabled 执行时是否enable指标的计算和跟踪,默认true
  • hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds 设置rolling percentile window的时间,默认60000
  • hystrix.command.default.metrics.rollingPercentile.numBuckets 设置rolling percentile window的numberBuckets。逻辑同上。默认6
  • hystrix.command.default.metrics.rollingPercentile.bucketSize 如果bucket size=100,window=10s,若这10s里有500次执行,只有最后100次执行会被统计到bucket里去。增加该值会增加内存开销以及排序的开销。默认100
  • hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds 记录health 快照(用来统计成功和错误绿)的间隔,默认500ms

Request Context 相关参数

  • hystrix.command.default.requestCache.enabled 默认true,需要重载getCacheKey(),返回null时不缓存
  • hystrix.command.default.requestLog.enabled 记录日志到HystrixRequestLog,默认true

Collapser Properties 命令合并配置

  • hystrix.collapser.default.maxRequestsInBatch 单次批处理的最大请求数,达到该数量触发批处理,默认Integer.MAX_VALUE
  • hystrix.collapser.default.timerDelayInMilliseconds 触发批处理的延迟,也可以为创建批处理的时间+该值,默认10
  • hystrix.collapser.default.requestCache.enabled 是否对HystrixCollapser.execute() and HystrixCollapser.queue()的cache,默认true

ThreadPool 线程池配置

  • hystrix.threadpool.default.coreSize 并发执行的核心线程数,默认10。不能设置为0,初始化setter的时候会出现异常。
  • hystrix.threadpool.default.maximumSize 并发执行的最大线程数,默认10。
  • hystrix.threadpool.default.maxQueueSize BlockingQueue的最大队列数,当设为-1,会使用SynchronousQueue,值为正时使用LinkedBlcokingQueue。
  • hystrix.threadpool.default.queueSizeRejectionThreshold 队列截断阈值。即使maxQueueSize没有达到,达到queueSizeRejectionThreshold该值后,请求也会被拒绝。如果maxQueueSize == -1,该字段将不起作用。
  • hystrix.threadpool.default.keepAliveTimeMinutes 线程空闲存活时间。如果corePoolSize和maxPoolSize设成一样(默认实现)该设置无效。
  • hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds 线程池统计指标的时间,默认10000。
  • hystrix.threadpool.default.metrics.rollingStats.numBuckets 将rolling window划分为n个buckets,默认10。

建议设置值:
timeoutInMilliseconds:依赖外部接口时,推荐配置比rpc超时时间稍短,否则可能无法发挥作用。
maxConcurrentRequests:估算值:(单机QPS*响应时间)*2/1000,2为预留一倍值,可以自行调整。
coreSize:估算值:(单机qps*响应时间)*1.5/1000,1.5为预留0.5倍buffer,该值可以适当减少,因为线程池会有排队队列。
maxQueueSize:仅在allowMaximumSizeToDivergeFromCoreSize(是否开启动态线程数)为true时才生效。建议设置core的两倍大小。

2.Hytrix常见事件

run()

  • EMIT:值传递,初始化参数,不触发getFallback()
  • SUCCESS:run()成功,不触发getFallback()
  • FAILURE:run()抛异常,触发getFallback()
  • TIMEOUT:run()超时,触发getFallback()
  • BAD_REQUEST:run()抛出HystrixBadRequestException,不触发getFallback()
  • SHORT_CIRCUITED:断路器开路,触发getFallback()
  • THREAD_POOL_REJECTED:线程池耗尽,触发getFallback()
  • SEMAPHORE_REJECTED:信号量拒绝,触发getFallback()

getFallback()

  • FALLBACK_EMIT:Fallback值传递,不抛异常
  • FALLBACK_SUCCESS:getFallback()成功,不抛异常
  • FALLBACK_FAILURE:getFallback()失败,抛异常
  • FALLBACK_REJECTION:调用getFallback()的线程数超量,抛异常
  • FALLBACK_MISSING:没有实现getFallback(),抛出异常

参考:

https://github.com/Netflix/Hystrix/wiki
https://blog.csdn.net/tongtong_use/article/details/78611225
http://hot66hot.iteye.com/blog/2155036

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值