4.1SpringCloud学习之-Hystrix属性参数配置

298 阅读4分钟

1.背景

今天我们学习SpringCloud的Hystrix熔断器

我们今天继续使用之前eureka-server作为服务注册中心

使用Springboot和springcloud的版本如下

  • springboot版本:2.3.5-release
  • springcloud版本:Hoxton.SR9

2.Hystrix重要参数

Execution相关的属性的配置

以下属性控制HystrixCommand.run()如何执行。 其中default表示默认情况,可以替换为具体的服务名称HystrixCommandKey 可以查看HystrixCommandProperties这个类

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.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 window10秒)收到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的统计器是由滑动窗口来实现的,我们可以这么来理解滑动窗口:一位乘客坐在正在行驶的列车的靠窗座位上,列车行驶的公路两侧种着一排挺拔的白杨树,随着列车的前进,路边的白杨树迅速从窗口滑过,我们用每棵树来代表一个请求,用列车的行驶代表时间的流逝,那么,列车上的这个窗口就是一个典型的滑动窗口,这个乘客能通过窗口看到的白杨树就是 Hystrix 要统计的数据。

桶: bucket 是 Hystrix统计滑动窗口数据时的最小单位。同样类比列车窗口,在列车速度非常快时,如果每掠过一棵树就统计一次窗口内树的数据,显然开销非常大,如果乘客将窗口分成十分,列车前进行时每掠过窗口的十分之一就统计一次数据,开销就完全可以接受了。 Hystrix 的 bucket (桶)也就是窗口 N分之一 的概念。

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 window10000,那么一个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=100window10s,若这10s里有500次执行,只有最后100次执行会被统计到bucket里去。增加该值会增加内存开销以及排序的开销。默认100
 
hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds 
 
记录health 快照(用来统计成功和错误绿)的间隔,默认500ms

ThreadPool 相关参数 可以查看HystrixThreadPoolProperties

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设成一样(默认实现)该设置无效。
 
hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds 
 
线程池统计指标的时间,默认10000
 
hystrix.threadpool.default.metrics.rollingStats.numBuckets 将rolling window划分为n个buckets,默认10

参考:

blog.csdn.net/tongtong_us…

www.imooc.com/article/765…

blog.csdn.net/qq_18603599…