(笔记) SpringCloud之Hystrix断路器 属性详解

Command属性主要用来控制 HystrixCommand 命令的行为,主要有下面5种类型的属性配置:

  • execution配置 控制HystrixCommand.run() 的如何执行

该配置前缀为 hystrix.command.default

  • execution.isolation.strategy :该属性用来设置执行的隔离策略,有如下二个选项:
    • THREAD:通过线程池隔离的策略,在独立线程上执行,并且他的并发限制受线程池中线程数量的限制(默认)
    • SEMAPHONE:通过信号量隔离的策略,在调用线程上执行,并且他的并发限制受信号量计数的限制。
  • execution.isolation.thread.timeoutInMilliseconds:该属性用来配置 HystrixCommand 执行的超时时间,单位为毫秒,默认值 1000 ,超出此时间配置,Hystrix 会将该执行命令为 TIMEOUT 并进入服务降级处理逻辑
  • execution.timeout.enabled:该属性用来配置 HystrixCommand 执行是否启动超时时间,默认值 true,如果设置为 false,则 execution.isolation.thread.timeoutInMilliseconds 属性的配置将不起作用
  • execution.isolation.thread.interruptOnTimeout:该属性用来配置当 HystrixCommand 执行超时的时候,是否需要将他中断,默认值 true
  • execution.isolation.semaphore.maxConcurrentRequests:当隔离策略使用信号量时,该属性用来配置信号量的大小,当最大并发请求数达到该设置值,后续的请求将会被拒绝
  • fallback配置  控制HystrixCommand.getFallback() 如何执行

该配置前缀为 hystrix.command.default

  • fallback.enabled:该属性用来设置服务降级策略是否启用,默认值 true ,如果设置为false,当请求失败或拒绝发生时,将不会调用 HystrixCommand.getFallback() 来执行服务降级逻辑
  • circuitBreaker 配置  控制断路器的行为

该配置前缀为 hystrix.command.default

  • circuitBreaker.enabled:该属性用来确定当服务请求命令失败时,是否使用断路器来跟踪其健康指标和熔断请求,默认值 true
  • circuitBreaker.requestVolumeThreshold:该属性用来设置在滚动时间窗中,断路器的最小请求数。例如:默认值 20 的情况下,如果滚动时间窗(默认值 10秒)内仅收到19个请求,即使这19个请求都失败了,断路器也不会打开。
  • circuitBreaker.sleepWindowInMilliseconds:该属性用来设置当断路器打开之后的休眠时间窗。默认值 5000 毫秒,休眠时间窗结束之后,会将断路器设置为"半开"状态,尝试熔断的请求命令,如果依然失败就将断路器继续设置为"打开"状态,如果成功就设置为"关闭"状态。
  • circuitBreaker.errorThresholdPercentage:该属性用来设置断路器打开的错误百分比条件。例如,默认值为 50 的情况下,表示在滚动时间窗中,在请求数量超过 circuitBreaker.requestVolumeThreshold 阈值的请求下,如果错误请求数的百分比超过50,就把断路器设置为"打开"状态,否则就设置为"关闭"状态。
  • circuitBreaker.forceOpen:该属性用来设置断路器强制进入"打开"状态,会拒绝所有请求,该属性优先于 circuitBreaker.forceClosed
  • circuitBreaker.forceClosed:该属性用来设置断路器强制进入"关闭"状态,会接收所有请求。
  • metrics 配置 捕获和HystrixCommand 和 HystrixObservableCommand 执行信息相关的配置属性

该配置属性与HystrixCommand 和 HystrixObservableCommand 执行中捕获指标信息有关,配置前缀为 hystrix.command.default

  • metrics.rollingStats.timeInMilliseconds:该属性用于设置滚动时间窗的长度,单位毫秒,该时间用于断路器判断健康度时需要收集信息的持续时间,默认值 10000 。断路器值啊收集指标信息时候会根据设置的时间窗长度拆分成多个"桶"来累计各度量值,每个"桶"记录了一段时间内的采集指标。
  • metrics.rollingStats.numBuckets:该属性用来设置滚动时间窗统计指标信息时,划分"桶"的数量,默认值 10 。 metrics.rollingStats.timeInMilliseconds 参数的设置必须能被该参数整除,否则将抛出异常。

metrics.rollingPercentile.enabled:该属性用来设置对命令执行的延迟是否使用百分位数来跟踪和计算,默认值 true ,如果设置为 false 那么所有概要统计都将返回 -1

  • metrics.rollingPercentile.timeInMilliseconds:该属性用来设置百分位统计的滚动窗口的持续时间,单位:毫秒,默认值 60000
  • metrics.rollingPercentile.numBuckets:该属性用来设置百分位统计窗口中使用"桶"的数量,默认值 6
  • metrics.rollingPercentile.bucketSize:该属性用来设置在执行过程中每个"桶"中保留的最大执行次数,如果在滚动时间窗内发生超该设定值的执行次数,就从最初的位置开始重写,例如:设置为 100,滚动窗口为 10 秒,若在10秒内一个"桶"中发生了500次执行,那么该"桶"中只保留最后的100次执行的统计,默认值 100
  • metrics.healthSnapshot.intervalInMilliseconds:该属性用来设置采集影响断路器状态的健康快照(请求的成功、错误百分比)的间隔等待时间,默认值 500
  • requestContext 配置 设置请求上下文的属性

该配置前缀为 hystrix.command.default

  • requestCache.enabled:该属性用来配置是否开启请求缓存
  • requestLog.enabledg:该属性用来设置 HystrixCommand 的执行和事件是否打印日志到 HystrixRequestLog 中,默认值 true
  • collapser 配置 设置请求合并的属性

该配置前缀为 hystrix.collapser.default

  • maxRequestsInBatch:该属性用来设置一次请求合并批处理允许的最大请求数量,默认值 Integer.MAX_VALUE
  • timerDelayInMilliseconds:该属性用来设置批处理过程中每个命令延迟的时间,单位毫秒,默认值 10
  • requestCache.enabled:该属性用来设置批处理过程中是否开启请求缓存,默认值 true
  • threadPool 配置 设置线程池的属性

该配置前缀为 hystrix.threadpool.default

    • coreSize:该属性用来设置执行命令线程池的核心线程数,该值也就是命令执行的最大并发量,默认值 10
    • maxQueueSize:该属性用来设置线程池的最大队列大小,当设置为 -1 时,线程池将使用 SynchronousQueue 实现的队列,否则使用 LinkedBlockingQueue 实现的队列,默认值 -1
    • queueSizeRejectionThreshold :该属性用来为队列设置拒绝阈值,即使队列没有到达最大值也能拒绝请求,该属性主要对 LinkedBlockingQueue 队列的补充,默认值 5,当 maxQueueSize 属性为 -1 时候,该属性无效
    • metrics.rollingPercentile.timeInMilliseconds:该属性用来设置线程池统计的滚动窗口的持续时间,单位:毫秒,默认值 10000
    • metrics.rollingPercentile.numBuckets:该属性用来设置线程池统计窗口中使用"桶"的数量,默认值 10

总结二 :

Hystrix可以配置属性的有以下类型:

  • Execution:控制HystrixCommand.run() 的如何执行
  • Fallback: 控制HystrixCommand.getFallback() 如何执行
  • Circuit Breaker: 控制断路器的行为
  • Metrics: 捕获和HystrixCommand 和 HystrixObservableCommand 执行信息相关的配置属性
  • Request Context:设置请求上下文的属性
  • Collapser Properties:设置请求合并的属性
  • Thread Pool Properties:设置线程池的属性

2. Hystrix参数的覆盖优先级

每个Hystrix参数都有4个地方可以配置,优先级从低到高如下,如果每个地方都配置相同的属性,则优先级高的值会覆盖优先级低的值

  • 1 内置全局默认值:写死在代码里的值
  • 2 动态全局默认属性:通过属性文件配置全局的值
  • 3 内置实例默认值:写死在代码里的实例的值
  • 4 动态配置实例属性:通过属性文件配置特定实例的值

3 Hystrix配置属性详解

Hystrix可以配置属性的有以下类型:

  • Execution:控制HystrixCommand.run() 的如何执行
  • Fallback: 控制HystrixCommand.getFallback() 如何执行
  • Circuit Breaker: 控制断路器的行为
  • Metrics: 捕获和HystrixCommand 和 HystrixObservableCommand 执行信息相关的配置属性
  • Request Context:设置请求上下文的属性
  • Collapser Properties:设置请求合并的属性
  • Thread Pool Properties:设置线程池的属性

3.1. Execution

以下属性控制HystrixCommand.run() 的如何执行

1. execution.isolation.strategy  表示HystrixCommand.run()的执行时的隔离策略,有以下两种策略

  • 1 THREAD: 在单独的线程上执行,并发请求受线程池中的线程数限制
  • 2 SEMAPHORE: 在调用线程上执行,并发请求量受信号量计数限制

在默认情况下,推荐HystrixCommands 使用 thread 隔离策略,HystrixObservableCommand 使用 semaphore 隔离策略。 
只有在高并发(单个实例每秒达到几百个调用)的调用时,才需要修改HystrixCommands 的隔离策略为semaphore 。semaphore 隔离策略通常只用于非网络调用

默认值:THREAD,

// 设置所有实例的默认值
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 表示是否开启超时设置。

默认值: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.interruptOnCancel3

6. execution.isolation.semaphore.maxConcurrentRequests 当HystrixCommand.run()使用SEMAPHORE的隔离策略时,设置最大的并发量

默认值:10

// 设置所有实例的默认值
hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests=...

// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.execution.isolation.semaphore.maxConcurrentRequests=...

3.2. Fallback

以下属性控制HystrixCommand.getFallback() 如何执行。这些属性对隔离策略THREAD 和SEMAPHORE都起作用. 
1. fallback.isolation.semaphore.maxConcurrentRequests 
此属性设置从调用线程允许HystrixCommand.getFallback()方法允许的最大并发请求数 
如果达到最大的并发量,则接下来的请求会被拒绝并且抛出异常.

默认值:10

// 设置所有实例的默认值
hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.fallback.isolation.semaphore.maxConcurrentRequests=...

2. fallback.enabled  是否开启fallback功能

默认值:true

// 设置所有实例的默认值
hystrix.command.default.fallback.enabled=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.fallback.enabled=...

3.3. Circuit Breaker 控制断路器的行为

1. circuitBreaker.enabled  是否开启断路器功能

默认值:true

// 设置所有实例的默认值
hystrix.command.default.circuitBreaker.enabled=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.circuitBreaker.enabled=..

2. circuitBreaker.requestVolumeThreshold 该属性设置滚动窗口中将使断路器跳闸的最小请求数量

如果此属性值为20,则在窗口时间内(如10s内),如果只收到19个请求且都失败了,则断路器也不会开启。

默认值:20

// 设置所有实例的默认值
hystrix.command.default.circuitBreaker.requestVolumeThreshold=...

// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.circuitBreaker.requestVolumeThreshold=...

3. circuitBreaker.sleepWindowInMilliseconds  断路器跳闸后,在此值的时间的内,hystrix会拒绝新的请求,只有过了这个时间断路器才会打开闸门

默认值:5000

// 设置所有实例的默认值
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.circuitBreaker.sleepWindowInMilliseconds=...

4. circuitBreaker.errorThresholdPercentage 设置失败百分比的阈值。如果失败比率超过这个值,则断路器跳闸并且进入fallback逻辑

默认值:50

// 设置所有实例的默认值
hystrix.command.default.circuitBreaker=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.circuitBreaker.errorThresholdPercentage=...

5. circuitBreaker.forceOpen  如果设置true,则强制使断路器跳闸,则会拒绝所有的请求.此值会覆盖circuitBreaker.forceClosed的值

默认值:false

// 设置所有实例的默认值
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

3.4. Metrics 捕获和HystrixCommand 和 HystrixObservableCommand 执行信息相关的配置属性

1. metrics.rollingStats.timeInMilliseconds  设置统计滚动窗口的时间长度

如果此值为10s,将窗口分成10个桶,每个桶表示1s时间,则统计信息如下图: 
这里写图片描述

默认值: 10000

// 设置所有实例的默认值
hystrix.command.default.metrics.rollingStats.timeInMilliseconds=....

// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.metrics.rollingStats.timeInMilliseconds=...

2. metrics.rollingStats.numBuckets  设置统计滚动窗口的桶数量,

注意:以下配置必须成立,否则会抛出异常。

metrics.rollingStats.timeInMilliseconds % metrics.rollingStats.numBuckets == 0

如:10000/10、10000/20是正确的配置,但是10000/7错误的

在高并发的环境里,每个桶的时间长度建议大于100ms

默认值:10

// 设置所有实例的默认值
hystrix.command.default.metrics.rollingStats.numBuckets=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.metrics.rollingStats.numBuckets=...

3. metrics.rollingPercentile.enabled 设置执行延迟是否被跟踪,并且被计算在失败百分比中。如果设置为false,则所有的统计数据返回-1

默认值: true

// 设置所有实例的默认值
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 == 0

如: 60000/6、60000/60是正确的配置,但是10000/7错误的

在高并发的环境里,每个桶的时间长度建议大于1000ms

默认值:6

// 设置所有实例的默认值
hystrix.command.default.metrics.rollingPercentile.numBuckets=...

// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.metrics.rollingPercentile.numBuckets=...

6. metrics.rollingPercentile.bucketSize  此属性设置每个桶保存的执行时间的最大值。如果桶数量是100,统计窗口为10s,如果这10s里有500次执行,只有最后100次执行会被统计到bucket里去

默认值: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=...

3.5. Request Context 此属性控制HystrixCommand使用到的Hystrix的上下文

1. requestCache.enabled  是否开启请求缓存功能

默认值:true

// 设置所有实例的默认值
hystrix.command.default.requestCache.enabled=...

// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.requestCache.enabled=...

2. requestLog.enabled  表示是否开启日志,打印执行HystrixCommand的情况和事件

默认值:true

// 设置所有实例的默认值
hystrix.command.default.requestLog.enabled=...
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.requestLog.enabled=...

3.6. Collapser Properties 设置请求合并的属性

1. maxRequestsInBatch  设置同时批量执行的请求的最大数量

默认值:Integer.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  是否对HystrixCollapser.execute() 和 HystrixCollapser.queue()开启请求缓存

默认值:true

// 设置所有实例的默认值
hystrix.collapser.default.requestCache.enabled=...
// 设置实例HystrixCommandKey的此属性值
hystrix.collapser.HystrixCollapserKey.requestCache.enabled=...

3.7. Thread Pool Properties 设置Hystrix Commands的线程池行为,大部分情况线程数量是10。

线程池数量的计算公式如下:

最高峰时每秒的请求数量 × 99%命令执行时间 + 喘息空间

设置线程池数量的主要原则是保持线程池越小越好,因为它是减轻负载并防止资源在延迟发生时被阻塞的主要工具

1. coreSize  设置线程池的core的大小

默认值:10

// 设置所有实例的默认值
hystrix.threadpool.default.coreSize=...
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolKey.coreSize=...

2. maximumSize  设置最大的线程池的大小,只有设置allowMaximumSizeToDivergeFromCoreSize时,此值才起作用

默认值:10

// 设置所有实例的默认值
hystrix.threadpool.default.maximumSize=...
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolKey.maximumSize=...

3. maxQueueSize  设置最大的BlockingQueue队列的值。如果设置-1,则使用SynchronousQueue队列,如果设置正数,则使用LinkedBlockingQueue队列

默认值:-1

// 设置所有实例的默认值
hystrix.threadpool.default.maxQueueSize=...
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolKey.maxQueueSize=...

4. queueSizeRejectionThreshold  因为maxQueueSize值不能被动态修改,所有通过设置此值可以实现动态修改等待队列长度。即等待的队列的数量大于queueSizeRejectionThreshold时(但是没有达到maxQueueSize值),则开始拒绝后续的请求进入队列。

如果设置-1,则属性不启作用

默认值:5

// 设置所有实例的默认值
hystrix.threadpool.default.queueSizeRejectionThreshold=...
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolKey.queueSizeRejectionThreshold=...

5. keepAliveTimeMinutes  设置线程多久没有服务后,需要释放(maximumSize-coreSize )个线程

默认值:1

// 设置所有实例的默认值
hystrix.threadpool.default.keepAliveTimeMinutes=...
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolKey.keepAliveTimeMinutes=...

6. allowMaximumSizeToDivergeFromCoreSize  设置allowMaximumSizeToDivergeFromCoreSize值为true时,maximumSize才有作用 
默认值:false

// 设置所有实例的默认值
hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=....
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolKey.allowMaximumSizeToDivergeFromCoreSize=...

7. metrics.rollingStats.timeInMilliseconds 设置滚动窗口的时间

默认值:10000

// 设置所有实例的默认值
hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds=true
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolKey.metrics.rollingStats.timeInMilliseconds=true

8. metrics.rollingStats.numBuckets  设置滚动静态窗口分成的桶的数量

配置的值必须满足如下条件:

metrics.rollingStats.timeInMilliseconds % metrics.rollingStats.numBuckets == 0

默认值:10 
建议每个桶的时间长度大于100ms

// 设置所有实例的默认值
hystrix.threadpool.default.metrics.rollingStats.numBuckets=...
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolProperties.metrics.rollingStats.numBuckets=...

猜你喜欢

转载自blog.csdn.net/Weixiaohuai/article/details/79010333