Hystrix配置简单说明

1、Hystrix属性的4中优先级

  1. 内置全局默认值(Global default from code)

如果下面3种都没有设置,默认是使用此种,后面用“默认值”代指这种。

  1. 动态全局默认属性(Dynamic global default property)

可以通过属性配置来更改全局默认值,后面用“默认属性”代指这种。

  1. 内置实例默认值(Instance default from code)

在代码中,设置的属性值,后面用“实例默认”来代指这种。

  1. 动态配置实例属性(Dynamic instance property)

可以针对特定的实例,动态配置属性值,来代替前面三种,后面用“实例属性”来代指这种。

优先级:1 < 2 < 3 < 4

2、命令属性

2.1 执行
  • execution.isolation.strategy
    设置HystrixCommand.run()的隔离策略,有两种选项:
    THREAD —— 在固定大小线程池中,以单独线程执行,并发请求数受限于线程池大小。
    SEMAPHORE —— 在调用线程中执行,通过信号量来限制并发量。

    默认值:THREAD(ExecutionIsolationStrategy.THREAD)
    可选值:THREAD,SEMAPHORE
    默认属性:hystrix.command.default.execution.isolation.strategy
    实例属性:
    hystrix.command.HystrixCommandKey.execution.isolation.strategy
    实例默认的设置:

// to use thread isolation
HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(ExecutionIsolationStrategy.THREAD)
// to use semaphore isolation
HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(ExecutionIsolationStrategy.SEMAPHORE)
  • execution.isolation.thread.timeoutInMilliseconds

    设置调用者等待命令执行的超时限制,超过此时间,HystrixCommand被标记为TIMEOUT,并执行回退逻辑。
    注意:超时会作用在HystrixCommand.queue(),即使调用者没有调用get()去获得Future对象。
    默认值:1000(毫秒)
    默认属性:hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds
    实例属性:hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds
    实例默认的设置:
    HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(int value)

  • execution.timeout.enabled
    设置HystrixCommand.run()的执行是否有超时限制。
    默认值:true
    默认属性:hystrix.command.default.execution.timeout.enabled
    实例属性:hystrix.command.HystrixCommandKey.execution.timeout.enabled
    实例默认的设置:
    HystrixCommandProperties.Setter().withExecutionTimeoutEnabled(boolean value)

  • execution.isolation.thread.interruptOnTimeout
    设置HystrixCommand.run()的执行是否在超时发生时被中断。
    默认值:true
    默认属性:hystrix.command.default.execution.isolation.thread.interruptOnTimeout
    实例属性:hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnTimeout
    实例默认的设置: HystrixCommandProperties.Setter().withExecutionIsolationThreadInterruptOnTimeout(boolean value)

  • execution.isolation.thread.interruptOnCancel
    设置HystrixCommand.run()的执行但取消动作发生时候可以响应中断。
    默认值:false
    默认属性:hystrix.command.default.execution.isolation.thread.interruptOnCancel
    实例属性:hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnCancel
    实例默认的设置:
    HystrixCommandProperties.Setter().withExecutionIsolationThreadInterruptOnCancel(boolean value)

  • execution.isolation.semaphore.maxConcurrentRequests
    设置当使用ExecutionIsolationStrategy.SEMAPHORE时,HystrixCommand.run()方法允许的最大请求数。如果达到最大并发数时,后续请求会被拒绝。
    信号量应该是容器(比如Tomcat)线程池一小部分,不能等于或者略小于容器线程池大小,否则起不到保护作用。
    默认值:10
    默认属性:hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests
    实例属性:hystrix.command.HystrixCommandKey.execution.isolation.semaphore.maxConcurrentRequests
    实例默认的设置:
    HystrixCommandProperties.Setter().withExecutionIsolationSemaphoreMaxConcurrentRequests(int value)

2.2 回退

下面的属性控制HystrixCommand.getFallback()执行。
这些属性对ExecutionIsolationStrategy.THREAD和ExecutionIsolationStrategy.SEMAPHORE都有效。

  • fallback.isolation.semaphore.maxConcurrentRequests
    设置调用线程产生的HystrixCommand.getFallback()方法的允许最大请求数目。
    如果达到最大并发数目,后续请求将会被拒绝,如果没有实现回退,则抛出异常。
    默认值:10
    默认属性:hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests
    实例属性:hystrix.command.HystrixCommandKey.fallback.isolation.semaphore.maxConcurrentRequests
    实例默认:
    HystrixCommandProperties.Setter()
    .withFallbackIsolationSemaphoreMaxConcurrentRequests(int value)

  • fallback.enabled

该属性决定当故障或者拒绝发生时,一个调用将会去尝试HystrixCommand.getFallback()。
默认值:true
默认属性:hystrix.command.default.fallback.enabled
实例属性:hystrix.command.HystrixCommandKey.fallback.enabled
实例默认的设置:HystrixCommandProperties.Setter()
.withFallbackEnabled(boolean value)

2.3 断路器(Circuit Breaker)

  • circuitBreaker.enabled
    设置断路器是否起作用。
    默认值:true
    默认属性:hystrix.command.default.circuitBreaker.enabled
    实例属性:hystrix.command.HystrixCommandKey.circuitBreaker.enabled
    实例默认的设置:HystrixCommandProperties.Setter()
    .withCircuitBreakerEnabled(boolean value)

  • circuitBreaker.requestVolumeThreshold
    设置在一个滚动窗口中,打开断路器的最少请求数。
    比如:如果值是20,在一个窗口内(比如10秒),收到19个请求,即使这19个请求都失败了,断路器也不会打开。
    默认值:20
    默认属性:hystrix.command.default.circuitBreaker.requestVolumeThreshold
    实例属性:hystrix.command.HystrixCommandKey.circuitBreaker.requestVolumeThreshold
    实例默认的设置:HystrixCommandProperties.Setter()
    .withCircuitBreakerRequestVolumeThreshold(int value)

  • circuitBreaker.sleepWindowInMilliseconds
    设置在回路被打开,拒绝请求到再次尝试请求并决定回路是否继续打开的时间。
    默认值:5000(毫秒)
    默认属性:hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds
    实例属性:hystrix.command.HystrixCommandKey.circuitBreaker.sleepWindowInMilliseconds
    实例默认的设置:
    HystrixCommandProperties.Setter()
    .withCircuitBreakerSleepWindowInMilliseconds(int value)

  • circuitBreaker.errorThresholdPercentage
    设置打开回路并启动回退逻辑的错误比率。
    默认值:50
    默认属性:hystrix.command.default.circuitBreaker.errorThresholdPercentage
    实例属性:hystrix.command.HystrixCommandKey.circuitBreaker.errorThresholdPercentage
    实例默认的设置:HystrixCommandProperties.Setter()
    .withCircuitBreakerErrorThresholdPercentage(int value)

  • circuitBreaker.forceOpen
    如果该属性设置为true,强制断路器进入打开状态,将会拒绝所有的请求。
    该属性优先级比circuitBreaker.forceClosed高。
    默认值:false
    默认属性:hystrix.command.default.circuitBreaker.forceOpen
    实例属性:hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen
    实例默认的设置:HystrixCommandProperties.Setter()
    .withCircuitBreakerForceOpen(boolean value)

  • circuitBreaker.forceClosed
    如果该属性设置为true,强制断路器进入关闭状态,将会允许所有的请求,无视错误率。
    默认值:false
    默认属性:hystrix.command.default.circuitBreaker.forceClosed
    实例属性:hystrix.command.HystrixCommandKey.circuitBreaker.forceClosed
    实例默认的设置:HystrixCommandProperties.Setter()
    .withCircuitBreakerForceClosed(boolean value)

2.4 请求上下文
  • requestCache.enabled
    设置HystrixCommand.getCacheKey()是否启用,由HystrixRequestCache通过请求缓存提供去重复数据功能。
    默认值:true
    默认属性:hystrix.command.default.requestCache.enabled
    实例属性:hystrix.command.HystrixCommandKey.requestCache.enabled
    实例默认的设置:HystrixCommandProperties.Setter()
    .withRequestCacheEnabled(boolean value)

  • requestLog.enabled
    设置HystrixCommand执行和事件是否要记录日志到HystrixRequestLog。
    默认值:true
    默认属性:hystrix.command.default.requestLog.enabled
    实例属性:hystrix.command.HystrixCommandKey.requestLog.enabled
    实例默认的设置:HystrixCommandProperties.Setter()
    .withRequestLogEnabled(boolean value)

2.5压缩器属性

下面的属性可以控制HystrixCollapser行为。

  • maxRequestsInBatch
    设置触发批处理执行之前,在批处理中允许的最大请求数。
    默认值:Integer.MAX_VALUE
    默认属性:hystrix.collapser.default.maxRequestsInBatch
    实例属性:hystrix.collapser.HystrixCollapserKey.maxRequestsInBatch
    实例默认的设置:HystrixCollapserProperties.Setter()
    .withMaxRequestsInBatch(int value)

  • timerDelayInMilliseconds
    设置批处理创建到执行之间的毫秒数。
    默认值:10
    默认属性:hystrix.collapser.default.timerDelayInMilliseconds
    实例属性:hystrix.collapser.HystrixCollapserKey.timerDelayInMilliseconds
    实例默认的设置:HystrixCollapserProperties.Setter()
    .withTimerDelayInMilliseconds(int value)

  • requestCache.enabled
    设置请求缓存是否对HystrixCollapser.execute()和HystrixCollapser.queue()的调用起作用。
    默认值:true
    默认属性:hystrix.collapser.default.requestCache.enabled
    实例属性:hystrix.collapser.HystrixCollapserKey.requestCache.enabled
    实例默认的设置:HystrixCollapserProperties.Setter()
    .withRequestCacheEnabled(boolean value)

2.6 线程池属性
  • coreSize
    设置核心线程池大小。
    默认值:10
    默认属性:hystrix.threadpool.default.coreSize
    实例属性:hystrix.threadpool.HystrixThreadPoolKey.coreSize
    实例默认的设置:HystrixThreadPoolProperties.Setter()
    .withCoreSize(int value)

  • maximumSize
    1.5.9新增属性,设置线程池最大值。
    这个是在不开始拒绝HystrixCommand的情况下支持的最大并发数。这个属性起作用的前提是设置了allowMaximumSizeToDrivergeFromCoreSize。
    1.5.9之前,核心线程池大小和最大线程池大小总是相同的。

  • maxQueueSize
    设置BlockingQueue最大的队列值。
    如果设置为-1,那么使用SynchronousQueue,否则正数将会使用LinkedBlockingQueue。
    如果需要去除这些限制,允许队列动态变化,可以参考queueSizeRejectionThreshold属性。
    修改SynchronousQueue和LinkedBlockingQueue需要重启。
    默认值:-1
    默认属性:hystrix.threadpool.default.maxQueueSize
    实例属性:hystrix.threadpool.HystrixThreadPoolKey.maxQueueSize
    实例默认的设置:HystrixThreadPoolProperties.Setter()
    .withMaxQueueSize(int value)

  • queueSizeRejectionThreshold

设置队列拒绝的阈值——一个人为设置的拒绝访问的最大队列值,即使maxQueueSize还没有达到。
当将一个线程放入队列等待执行时,HystrixCommand使用该属性。
注意:如果maxQueueSize设置为-1,该属性不可用。
默认值:5
默认属性:hystrix.threadpool.default.queueSizeRejectionThreshold
实例属性:hystrix.threadpool.HystrixThreadPoolKey.queueSizeRejectionThreshold
实例默认的设置:HystrixThreadPoolProperties.Setter()
.withQueueSizeRejectionThreshold(int value)

  • keepAliveTimeMinutes
    设置存活时间,单位分钟。
    如果coreSize小于maximumSize,那么该属性控制一个线程从实用完成到被释放的时间。
    默认值:1
    默认属性:hystrix.threadpool.default.keepAliveTimeMinutes
    实例属性:hystrix.threadpool.HystrixThreadPoolKey.keepAliveTimeMinutes
    实例默认的设置:HystrixThreadPoolProperties.Setter()
    .withKeepAliveTimeMinutes(int value)

  • allowMaximumSizeToDivergeFromCoreSize

在1.5.9中新增的属性。该属性允许maximumSize起作用。
属性值可以等于或者大于coreSize值,设置coreSize小于maximumSize的线程池能够支持maximumSize的并发数,但是会将不活跃的线程返回到系统中去。(详见KeepAliveTimeMinutes)
默认值:false
默认属性:hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize
实例属性:hystrix.threadpool.HystrixThreadPoolKey.allowMaximumSizeToDivergeFromCoreSize
实例默认的设置:HystrixThreadPoolProperties.Setter()
.withAllowMaximumSizeToDivergeFromCoreSize(boolean value)

  • metrics.rollingStats.timeInMilliseconds
    设置统计的滚动窗口的时间段大小。该属性是线程池保持指标时间长短。
    默认值:10000(毫秒)
    默认属性:hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds
    实例属性:hystrix.threadpool.HystrixThreadPoolKey.metrics.rollingStats.timeInMilliseconds
    实例默认的设置:HystrixThreadPoolProperties.Setter()
    .withMetricsRollingStatisticalWindowInMilliseconds(int value)

  • metrics.rollingStats.numBuckets
    设置滚动的统计窗口被分成的桶(bucket)的数目。
    注意:”metrics.rollingStats.timeInMilliseconds % metrics.rollingStats.numBuckets == 0"必须为true,否则会抛出异常。
    默认值:10
    可能的值:任何能被metrics.rollingStats.timeInMilliseconds整除的值。
    默认属性:hystrix.threadpool.default.metrics.rollingStats.numBuckets
    实例属性:hystrix.threadpool.HystrixThreadPoolProperties.metrics.rollingStats.numBuckets
    实例默认的设置:HystrixThreadPoolProperties.Setter()
    .withMetricsRollingStatisticalWindowBuckets(int value)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值