返回 导航

SpringBoot / Cloud

hangge.com

SpringCloud - 服务容错保护组件Hystrix的使用详解8(常用的配置属性)

作者:hangge | 2020-07-21 08:10

八、属性配置方式与优先级

Hystrix 提供了非常丰富和灵活的配置属性,这些属性都存在下面 4 个不同优先级别的配置(优先级由低到高):
  • 全局默认配置:即代码中定义的默认值
比如超时时长默认为 1000ms

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000

@HystrixCommand(commandProperties = {
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "10000")
})
public String hello() {
    return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
}

hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds=10000

九、Command 属性的相关配置

Command 属性主要用来控制 HystrixCommand 命令的行为。它由如下 5 中不同类型的属性配置。

1,execution 配置

execution 配置控制的是 即 HystrixCommand.run() 的执行。
(1)execution.isolation.strategy 用来设置 HystrixCommand.run() 的执行时的隔离策略,有以下两种策略
// 设置所有实例的默认值
hystrix.command.default.execution.isolation.strategy=..
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.execution.isolation.strategy=...

(2)execution.isolation.thread.timeoutInMilliseconds 设置调用者执行的超时时间(单位毫秒,默认值:1000
// 设置所有实例的默认值
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds=...

(3)execution.timeout.enabled 设置 HystrixCommand.run() 的执行是否启用超时时间(默认值:true
// 设置所有实例的默认值
hystrix.command.default.execution.timeout.enabled=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.execution.timeout.enabled=...

(4)execution.isolation.thread.interruptOnTimeout 设置当 HystrixCommand.run() 的执行超时时,是否要将它中断(默认值:true
// 设置所有实例的默认值
hystrix.command.default.execution.isolation.thread.interruptOnTimeout=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnTimeout=...

(5)execution.isolation.thread.interruptOnCancel 设置是否在取消任务执行时,中断 HystrixCommand.run() 的执行(默认值:false
// 设置所有实例的默认值
hystrix.command.default.execution.isolation.thread.interruptOnCancel=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnCancel 

(6)execution.isolation.semaphore.maxConcurrentRequests 设置当 HystrixCommand.run() 使用 SEMAPHORE 的隔离策略时,设置最大的并发量(默认值:10
// 设置所有实例的默认值
hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.execution.isolation.semaphore.maxConcurrentRequests=...

2,fallback 配置

fallback 配置属性用来控制 HystrixCommand.getFallback() 如何执行。这些属性对隔离策略 THREADSEMAPHORE 都起作用.
(1)fallback.isolation.semaphore.maxConcurrentRequests 属性设置从调用线程允许 HystrixCommand.getFallback() 方法允许的最大并发请求数(默认值:10) 。
  • 如果达到最大的并发量,则接下来的请求会被拒绝并且抛出异常(因为没有后续的 fallback 可以被调用了)
// 设置所有实例的默认值
hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.fallback.isolation.semaphore.maxConcurrentRequests=...

(2)fallback.enabled 属性用来设置服务降级策略是否启用(默认值为 true)。
  • 如果设置为 false,那么当请求失败或者拒绝发生时,将不会调用 HystrixCommand.getFallback() 来执行服务降级逻辑。
// 设置所有实例的默认值
hystrix.command.default.fallback.enabled=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.fallback.enabled=...

3,CircuitBreaker 配置

CircuitBreaker 为断路器的属性配置,用来控制 HystrixCircuitBreaker 的行为。
(1)circuitBreaker.enabled 是否开启断路器功能(默认值为 true
// 设置所有实例的默认值
hystrix.command.default.circuitBreaker.enabled=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.circuitBreaker.enabled=...

(2)circuitBreaker.requestVolumeThreshold 属性设置滚动窗口中将使断路器熔断的最小请求数量(默认值为 20)。
  • 假设此属性值为 20,则在窗口时间内(如 10s 内),如果只收到 19 个请求且都失败了,则断路器也不会开启。
// 设置所有实例的默认值
hystrix.command.default.circuitBreaker.requestVolumeThreshold=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.circuitBreaker.requestVolumeThreshold=...

(3)circuitBreaker.sleepWindowInMilliseconds 属性用来设置当断路器打开之后的休眠时间窗(默认为 5000)。
  • 休眠时间窗结束之后,会将断路器置为“半开”状态,尝试熔断的请求命令,如果依然失败就将断路器继续设置为“打开”状态,如果成功就设置为“关闭”状态
// 设置所有实例的默认值
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.circuitBreaker.sleepWindowInMilliseconds=...

(4)circuitBreaker.errorThresholdPercentage 设置断路器打开的错误百分比的条件(默认值 50)。
  • 在滚动时间窗中,在请求数量超过 circuitBreaker.requestVolumeThreshold 阀值的前提下,如果失败比率超过这个值,则把断路器设置为“打开”状态,否则就设置为“关闭”状态
// 设置所有实例的默认值
hystrix.command.default.circuitBreaker=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.circuitBreaker.errorThresholdPercentage=...

(5)circuitBreaker.forceOpen 如果设置 true,则强制使断路器进入“打开”状态,它会拒绝所有的请求(默认值为 false)。
  • 此值会覆盖 circuitBreaker.forceClosed 的值
// 设置所有实例的默认值
hystrix.command.default.circuitBreaker.forceOpen=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen=...

(6)circuitBreaker.forceClosed 如果设置 true,则强制使断路器进行关闭状态,此时会允许执行所有请求,无论是否失败的次数达到 circuitBreaker.errorThresholdPercentage 值(默认值为 false
// 设置所有实例的默认值
hystrix.command.default.circuitBreaker.forceClosed=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.circuitBreaker.forceClosed=...

4,metrics 配置

metrics 配置属性均与 HystrixCommand HystrixObservableCommand 执行中捕获的指标信息有关。
(1)metrics.rollingStats.timeInMilliseconds 属性用来设置滚动时间窗的长度,单位为毫秒(默认值 10000
  • 该时间用于断路器判断健康度时需要收集信息的持续时间,断路器在收集指标信息的时候会根据设置的时间窗长度拆分成多个“”来累计各个度量值,每个“”记录一段时间内的采集指标。)
  • 比如此值为 10s,将窗口分成 10 个桶,每个桶记录 1s 时间内的指标信息,则统计信息如下图:
// 设置所有实例的默认值
hystrix.command.default.metrics.rollingStats.timeInMilliseconds=....
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.metrics.rollingStats.timeInMilliseconds=...

(2)metrics.rollingStats.numBuckets 属性用来设置滚动时间窗统计指标信息时划分“”的数量(默认值为 10)。
  • metrics.rollingStats.timeInMilliseconds 参数必须能被 metrics.rollingStats.numBuckets 参数整除,不然将抛出异常。
  • 在高并发的环境里,每个桶的时间长度建议大于 100ms
// 设置所有实例的默认值
hystrix.command.default.metrics.rollingStats.numBuckets=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.metrics.rollingStats.numBuckets=...

(3)metrics.rollingPercentile.enabled 设置对命令执行的延迟是否使用百非位数来跟踪计算(默认值为 true
  • 如果设置为 false,那么所有的概率统计都将返回 -1
// 设置所有实例的默认值
hystrix.command.default.metrics.rollingPercentile.enabled=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.metrics.rollingPercentile.enabled=...

(4)metrics.rollingPercentile.timeInMilliseconds 属性设置统计滚动百分比窗口的持续时间,单位为毫秒(默认值:60000
// 设置所有实例的默认值
hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.metrics.rollingPercentile.timeInMilliseconds=...

(5)metrics.rollingPercentile.numBuckets 设置统计滚动百分比窗口的“”数量。
  • metrics.rollingPercentile.timeInMilliseconds 参数必须能被 metrics.rollingPercentile.numBuckets 参数整除,不然将抛出异常。
  • 在高并发的环境里,每个桶的时间长度建议大于 1000ms
// 设置所有实例的默认值
hystrix.command.default.metrics.rollingPercentile.numBuckets=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.metrics.rollingPercentile.numBuckets=...

(6)metrics.rollingPercentile.bucketSize 属性设置在执行过程中,每个“”中保留的最大执行次数(默认值为 100
  • 如果在滚动时间窗内发生超过该设定值的执行次数,就从最初的位置开始重写。
  • 例如将该值设置为 100,滚动窗口为 10 秒,若在 10 秒内一个“”中发生了 500 次执行,那么该“”中只保留最后的 100 次执行的统计。
  • 注意:增加该值的大小将会增加内存量的消耗,并增加排序百分位数所需的计算时间。
// 设置所有实例的默认值
hystrix.command.default.metrics.rollingPercentile.bucketSize=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.metrics.rollingPercentile.bucketSize=...

(7)metrics.healthSnapshot.intervalInMilliseconds 属性用来设置采集影响断路器状态的健康快照(请求的成功、错误百分比)的间隔等待时间(默认值为 500
// 设置所有实例的默认值
hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.metrics.healthSnapshot.intervalInMilliseconds=...

5,requestContext 配置

requestContext 配置属性涉及 HystrixCommand 使用的 HystrixRequestContextHystrix 的上下文)的设置
(1)requestCache.enabled 使用用来配置是否开启请求缓存功能(默认值为 true
// 设置所有实例的默认值
hystrix.command.default.requestCache.enabled=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.requestCache.enabled=...

(2)requestLog.enabled 设置 HystrixCommand 的执行和事件是否打印日志到 HystrixRequestLog 中(默认值为 true
// 设置所有实例的默认值
hystrix.command.default.requestLog.enabled=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.requestLog.enabled=...

十、collapser 属性的相关配置

collapser 属性用来控制命令合并相关的行为。
(1)maxRequestsInBatch 参数设置一次请求合并批处理中允许的最大请求数(默认值为 Interger.MAX_VALUE
// 设置所有实例的默认值
hystrix.collapser.default.maxRequestsInBatch=...
// 设置实例HystrixCommandKey的此属性值
hystrix.collapser.HystrixCollapserKey.maxRequestsInBatch=...

(2)timerDelayInMilliseconds 参数用来设置批处理过程中每个命令延迟的事件,单位为毫秒(默认值为 10
// 设置所有实例的默认值
hystrix.collapser.default.timerDelayInMilliseconds=...
// 设置实例HystrixCommandKey的此属性值
hystrix.collapser.HystrixCollapserKey.timerDelayInMilliseconds=...

(3)requestCache.enabled 参数用来设置批处理过程中是否开启请求缓存。
// 设置所有实例的默认值
hystrix.collapser.default.requestCache.enabled=...
// 设置实例HystrixCommandKey的此属性值
hystrix.collapser.HystrixCollapserKey.requestCache.enabled=...

十一、threedPool 属性的相关配置

threedPool 属性用来控制 Hystrix 命令所属线程池的配置。
(1)coreSize 参数用来设置执行命令线程池的核心线程数,该值也就是命令执行的并发量(默认值为 10
// 设置所有实例的默认值
hystrix.threadpool.default.coreSize=...
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolKey.coreSize=...

(2)maxQueueSize 参数用来设置线程池的最大队列大小(默认值为 -1)。
  • 当设置为 -1 时,线程池将使用 SyncharonousQueue 实现的队列,否则将使用 LinkedBlockingQueue 实现的队列。
// 设置所有实例的默认值
hystrix.threadpool.default.maximumSize=...
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolKey.maximumSize=...

(3)queueSizeRejectionThreshold 参数用来为队列设置拒绝阀值。通过该参数,即使队列没有达到最大值也能拒绝请求(默认值为 5
  • 该参数主要是对 LinkedBlockingQueue 队列的补充,因为 LinkedBlockingQueue 队列不能动态修改它的对象大小,而通过该属性就可以调整拒绝请求的队列大小了。
// 设置所有实例的默认值
hystrix.threadpool.default.queueSizeRejectionThreshold=...
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolKey.queueSizeRejectionThreshold=...

(4)metrics.rollingStats.timeInMilliseconds 参数用来设置滚动时间窗口的长度,单位为毫秒(默认值为 10000
  • 该滚动时间窗的长度用于线程池的指标度量,它会被分成多个“”来统计指标。
// 设置所有实例的默认值
hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds=true
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolKey.metrics.rollingStats.timeInMilliseconds=true

(5)metrics.rollingStats.numBuckets 参数用来设置滚动时间窗被划分成“”的数量。 
  • 注意metrics.rollingStats.timeInMilliseconds 参数的设置必须能够被 metrics.rollingStats.numBuckets 参数整除,不然会抛出异常。
// 设置所有实例的默认值
hystrix.threadpool.default.metrics.rollingStats.numBuckets=...
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolProperties.metrics.rollingStats.numBuckets=...
评论

全部评论(0)

回到顶部