【Hystrix技术指南】(2)参数配置的详细介绍

发布时间 2023-08-08 12:27:39作者: 程序分享

推荐超值课程:点击获取

也许你度过了很糟糕的一天,但这并不代表你会因此度过糟糕的一生。

  • 分布式系统的规模和复杂度不断增加,随着而来的是对分布式系统可用性的要求越来越高。在各种高可用设计模式中,【熔断、隔离、降级、限流】是经常被使用的。而相关的技术,Hystrix本身早已算不上什么新技术,但它却是最经典的技术体系!。

  • Hystrix以实现熔断降级的设计,从而提高了系统的可用性。

  • Hystrix是一个在调用端上,实现断路器模式,以及隔舱模式,通过避免级联故障,提高系统容错能力,从而实现高可用设计的一个Java服务组件库。

  • *Hystrix实现了资源隔离机制

  • closed:closed是关闭状态,服务调用方每次请求都到服务提供方;

  • open:是open打开状态,意思是如果服务提供方的异常率或者是请求的并发量超过设置的阈值之后,就会开启熔断机制,开启熔断机制之后服务调用方所有的请求都不会在请求到服务提供方,而是直接使用本地的服务降级方法;

  • half-open:是半打开状态,服务调用方所有的请求依然会请求到服务端,hystrix也有自我恢复机制,意思是当服务提供方的熔断机制处于打开状态时,会在开启一个时间窗口,就是一定时间后或者是下一次请求的时间大于时间窗口的时间,hystrix就会重新将这次请求再次发送到服务提供方,如果成功就将状态改为half-open状态,如果失败就继续处于开启状态,并且重新刷新时间窗口的时间。

Hystrix属性的4中优先级

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

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

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

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

优先级:1 < 2 < 3 < 4

execution.isolation.strategy

设置HystrixCommand.run()的隔离策略,有两种选项:

  • THREAD — 固定大小线程池中,以单独线程执行,并发请求数受限于线程池大小。
  • SEMAPHORE — 在调用线程中执行,通过信号量来限制并发量。
  • 默认值:THREAD(ExecutionIsolationStrategy.THREAD)
  • *可选值:THREAD,SEMAPHORE
默认属性:hystrix.command.default.execution.isolation.strategy

实例属性:hystrix.command.HystrixCommandKey.execution.isolation.strategy
复制代码

实例默认的设置:


HystrixCommandProperties.Setter()

.withExecutionIsolationStrategy(ExecutionIsolationStrategy.THREAD)

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)
复制代码

回退方法

下面的属性控制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)
复制代码

断路器(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)
复制代码

请求上下文

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)
复制代码

下面的属性可以控制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
复制代码

实例默认的设置

requestCache.enabled

设置请求缓存是否对HystrixCollapser.execute()和HystrixCollapser.queue()的调用起作用。

  • 默认值:true
默认属性:hystrix.collapser.default.requestCache.enabled

实例属性:hystrix.collapser.HystrixCollapserKey.requestCache.enabled

复制代码

实例默认的设置

HystrixCollapserProperties.Setter().withRequestCacheEnabled(boolean value)
复制代码

线程池属性

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)

分享资源

资源分享
扫码关注发送:资源 获取以上资源
字节技术