服务容错保护断路器Hystrix之五:配置--temp https://blog.csdn.net/tongtong_use/article/details/78611225

接着《服务容错保护断路器Hystrix之二:Hystrix工作流程解析》中的《2.8、关于配置》再列举重要的配置如下

一、hystrix在生产中的建议

1、保持timeout的默认值(1000ms),除非需要修改(其实通常会修改)

2、保持threadpool的的线程数为10个,除非需要更多

3、依赖标准的报警和监控系统来捕获问题

4、通过dashboards的实时监控来动态修改配置,直到满意为止

 

二、配置信息(default或HystrixCommandKey最常用的几项

  • 超时时间(默认1000ms,单位:ms)
    • hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds
      • 在调用方配置,被该调用方的所有方法的超时时间都是该值,优先级低于下边的指定配置
    • hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds
      • 在调用方配置,被该调用方的指定方法(HystrixCommandKey方法名)的超时时间是该值
  • 线程池核心线程数
    • hystrix.threadpool.default.coreSize(默认为10)
  • Queue
    • hystrix.threadpool.default.maxQueueSize(最大排队长度。默认-1,使用SynchronousQueue。其他值则使用 LinkedBlockingQueue。如果要从-1换成其他值则需重启,即该值不能动态调整,若要动态调整,需要使用到下边这个配置)
    • hystrix.threadpool.default.queueSizeRejectionThreshold(排队线程数量阈值,默认为5,达到时拒绝,如果配置了该选项,队列的大小是该队列)
      • 注意:如果maxQueueSize=-1的话,则该选项不起作用
  • 断路器
    • hystrix.command.default.circuitBreaker.requestVolumeThreshold(当在配置时间窗口内达到此数量的失败后,进行短路。默认20个)
      • For example, if the value is 20, then if only 19 requests are received in the rolling window (say a window of 10 seconds) the circuit will not trip open even if all 19 failed.
    • hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds(短路多久以后开始尝试是否恢复,默认5s)
    • hystrix.command.default.circuitBreaker.errorThresholdPercentage(出错百分比阈值,当达到此阈值后,开始短路。默认50%)
  • fallback
    • hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests(调用线程允许请求HystrixCommand.GetFallback()的最大数量,默认10。超出时将会有异常抛出,注意:该项配置对于THREAD隔离模式也起作用)

 spring-cloud的版本有 Brixton.RELEASE和Dalston.RELEASE,现在创建的新项目应该是引用Dalston.RELEASE这个版本,该版本下默认hystrix是关闭的,所以需要在属性文件中进行打开,如果没有设置为true,在后面的调试过程会发现熔断机制不生效

#开启hystrix熔断机制
feign.hystrix.enabled=true

 

hystrix的超时设置:

#开启hystrix请求超时机制   也可以设置成永久不超时
hystrix.command.default.execution.timeout.enabled=false
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000

 

每种配置都有4种优先级,以下为优先级从低到高的解释

1.基于代码的全局缺省值

2.基于properties配置表的全局配置

3.基于代码对配置更改

4.基于代码对配置动态更改

注:同一个配置,采用不同方法更改,那么配置的key会有不同

coreSize
设置核心线程池的大小(这个值和ThreadPoolExecutor的coreSize的含义不一样)。

默认值:10
默认属性:hystrix.threadpool.default.coreSize
实例属性:hystrix.threadpool.HystrixThreadPoolKey.coreSize
实例配置:HystrixThreadPoolProperties.Setter().withCoreSize(int Value)
注解使用: @HystrixCommand(threadPoolProperties = {@HystrixProperty(name = “coreSize”,value = “10”)})
maximumSize
1.5.9新增属性,设置线程池最大值。这个是在不开始拒绝HystrixCommand的情况下支持的最大并发数。这个属性起作用的前提是设置了allowMaximumSizeToDrivergeFromCoreSize。1.5.9之前,核心线程池大小和最大线程池大小总是相同的。

默认值:10
默认属性:hystrix.threadpool.default.maximumSize
实例属性:hystrix.threadpool.HystrixThreadPoolKey.maximumSize
实例配置:HystrixThreadPoolProperties.Setter().withMaximumSize(int Value)
注解使用: @HystrixCommand(threadPoolProperties = {@HystrixProperty(name = “maximumSize”,value = “10”)})
maxQueueSize
设置BlockingQueue最大的队列值。如果设置为-1,那么使用SynchronousQueue,否则正数将会使用LinkedBlockingQueue。如果需要去除这些限制,允许队列动态变化,可以参考queueSizeRejectionThreshold属性。 修改SynchronousQueue和LinkedBlockingQueue需要重启。

默认值:-1
默认属性:hystrix.threadpool.default.maxQueueSize
实例属性:hystrix.threadpool.HystrixThreadPoolKey.maxQueueSize
实例配置:HystrixThreadPoolProperties.Setter().withMaxQueueSize(int Value)
注解使用: @HystrixCommand(threadPoolProperties = {@HystrixProperty(name = “maxQueueSize”,value = “10”)})
queueSizeRejectionThreshold
设置队列拒绝的阈值—-一个人为设置的拒绝访问的最大队列值,即使当前队列元素还没达到maxQueueSize。 当将一个线程放入队列等待执行时,HystrixCommand使用该属性。注意:如果maxQueueSize设置为-1,该属性不可用。

默认值:5
默认属性:hystrix.threadpool.default.queueSizeRejectionThreshold
实例属性:hystrix.threadpool.HystrixThreadPoolKey.queueSizeRejectionThreshold
实例默认的设置:HystrixThreadPoolProperties.Setter().withQueueSizeRejectionThreshold(int Value)
注解使用: @HystrixCommand(threadPoolProperties = {@HystrixProperty(name = “queueSizeRejectionThreshold”,value = “5”)})
keepAliveTimeMinutes
设置存活时间,单位分钟。如果coreSize小于maximumSize,那么该属性控制一个线程从实用完成到被释放的时间。

默认值:1
默认属性:hystrix.threadpool.default.keepAliveTimeMinutes
实例属性:hystrix.threadpool.HystrixThreadPoolKey.keepAliveTimeMinutes
实例配置:HystrixThreadPoolProperties.Setter().withKeepAliveTimeMinutes(int Value)
注解使用: @HystrixCommand(threadPoolProperties = {@HystrixProperty(name = “keepAliveTimeMinutes”,value = “1”)})
allowMaximumSizeToDivergeFromCoreSize
在1.5.9中新增的属性。该属性允许maximumSize起作用。属性值可以等于或者大于coreSize值,设置coreSize小于maximumSize的线程池能够支持maximumSize的并发数,但是会将不活跃的线程返回到系统中去。(详见KeepAliveTimeMinutes)
* 默认值:false
* 默认属性:hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize
* 实例属性:hystrix.threadpool.HystrixThreadPoolKey.allowMaximumSizeToDivergeFromCoreSize
* 实例配置:HystrixThreadPoolProperties.Setter().withAllowMaximumSizeToDivergeFromCoreSize(boolean Value)

(7)度量属性配置
PS:不知道什么原因,计量属性的配置都是放在了线程池配置里面。可能是由于线程池隔离是计量属性隔离的基准。

metrics.rollingStats.timeInMilliseconds
设置统计的滚动窗口的时间段大小。该属性是线程池保持指标时间长短。
* 默认值:10000(毫秒)
* 默认属性:hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds
* 实例属性:hystrix.threadpool.HystrixThreadPoolKey.metrics.rollingStats.timeInMilliseconds
* 实例配置:HystrixThreadPoolProperties.Setter().withMetricsRollingStatisticalWindowInMilliseconds(int Value)
* 注解使用: @HystrixCommand(threadPoolProperties = {@HystrixProperty(name = “metrics.rollingStats.timeInMilliseconds”,value = “10000”)})

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)
* 注解使用: @HystrixCommand(threadPoolProperties = {@HystrixProperty(name = “metrics.rollingStats.numBuckets”,value = “10”)})

https://github.com/Netflix/Hystrix/wiki/Configuration#coreSize

示例说明:

hystrix.command.default和hystrix.threadpool.default中的default为默认CommandKey

Command Properties
Execution相关的属性的配置:
hystrix.command.default.execution.isolation.strategy 隔离策略,默认是Thread, 可选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。
semaphore应该占整个容器(tomcat)的线程池的一小部分。
Fallback相关的属性
这些参数可以应用于Hystrix的THREAD和SEMAPHORE策略

hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests 如果并发数达到该设置值,请求会被拒绝和抛出异常并且fallback不会被调用。默认10
hystrix.command.default.fallback.enabled 当执行失败或者请求被拒绝,是否会尝试调用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
hystrix.command.default.circuitBreaker.forceOpen 强制打开熔断器,如果打开这个开关,那么拒绝所有request,默认false
hystrix.command.default.circuitBreaker.forceClosed 强制关闭熔断器 如果这个开关打开,circuit将一直关闭且忽略circuitBreaker.errorThresholdPercentage
Metrics相关参数
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 相关参数
线程数默认值10适用于大部分情况(有时可以设置得更小),如果需要设置得更大,那有个基本得公式可以follow:
requests per second at peak when healthy × 99th percentile latency in seconds + some breathing room
每秒最大支撑的请求数 (99%平均响应时间 + 缓存值)
比如:每秒能处理1000个请求,99%的请求响应时间是60ms,那么公式是:
(0.060+0.012)

基本得原则时保持线程池尽可能小,他主要是为了释放压力,防止资源被阻塞。
当一切都是正常的时候,线程池一般仅会有1到2个线程激活来提供服务

hystrix.threadpool.default.coreSize 并发执行的最大线程数,默认10
hystrix.threadpool.default.maxQueueSize BlockingQueue的最大队列数,当设为-1,会使用SynchronousQueue,值为正时使用LinkedBlcokingQueue。该设置只会在初始化时有效,之后不能修改threadpool的queue size,除非reinitialising thread executor。默认-1。
hystrix.threadpool.default.queueSizeRejectionThreshold 即使maxQueueSize没有达到,达到queueSizeRejectionThreshold该值后,请求也会被拒绝。因为maxQueueSize不能被动态修改,这个参数将允许我们动态设置该值。if maxQueueSize == -1,该字段将不起作用
hystrix.threadpool.default.keepAliveTimeMinutes 如果corePoolSize和maxPoolSize设成一样(默认实现)该设置无效。如果通过plugin(https://github.com/Netflix/Hystrix/wiki/Plugins)使用自定义实现,该设置才有用,默认1.
hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds 线程池统计指标的时间,默认10000
hystrix.threadpool.default.metrics.rollingStats.numBuckets 将rolling window划分为n个buckets,默认10

 

三、监控hystrix

说明:hystrix为每一个commandKey提供了计数器。原理:

附:清晰大图

 

四、常见的hystrix事件类型

  • run()
    • SUCCESS:run()成功,不触发getFallback()
    • FAILURE:run()抛异常,触发getFallback()
    • TIMEOUT:run()超时,触发getFallback()
    • BAD_REQUEST:run()抛出HystrixBadRequestException,不触发getFallback()
    • SHORT_CIRCUITED:断路器开路,触发getFallback()
    • THREAD_POOL_REJECTED:线程池耗尽,触发getFallback()
    • FALLBACK_MISSING:没有实现getFallback(),抛出异常
  • getFallback()
    • FALLBACK_SUCCESS:getFallback()成功,不抛异常
    • FALLBACK_FAILURE:getFallback()失败,抛异常
    • FALLBACK_REJECTION:调用getFallback()的线程数超量,抛异常

五、Metrics storage and Dashboard

仅仅记录hystrix1.5.0及其后续版本,之前的版本的数据结构不一样。

posted on 2017-09-15 16:22  duanxz  阅读(3123)  评论(0编辑  收藏  举报