• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

浅出SpringCloud原理和「Netflix系列:Hystrix」针对于限流熔断组件Hystrix的基本参数和实现原理

武飞扬头像
洛神灬殇
帮助1

[每日一句]

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

[温馨提示]

承接第一篇文章🏹【深入浅出SpringCloud原理及实战】「Netflix系列之Hystrix」针对于限流熔断组件Hystrix的基本参数和实现原理介绍分析

[背景介绍]

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

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

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

  • Hystrix实现了资源隔离机制

[熔断器状态]

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

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

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

[配置介绍]

主要参考: https://github.com/Netflix/Hystrix/wiki/Configuration

Hystrix属性的4中优先级

  1. 内置全局默认值(Global default from code)

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

  1. 动态全局默认属性(Dynamic global default property)

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

  1. 内置实例默认值(Instance default from code)

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

  1. 动态配置实例属性(Dynamic instance property)

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

优先级:1 < 2 < 3 < 4

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

[命令属性]

execution.isolation.strategy

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

  • THREAD — 固定大小线程池中,以单独线程执行,并发请求数受限于线程池大小。

  • SEMAPHORE — 在调用线程中执行,通过信号量来限制并发量。

  • 默认值:THREAD(ExecutionIsolationStrategy.THREAD)

  • 可选值:THREAD,SEMAPHORE

总结:隔离策略,默认是Thread, 可选Thread|Semaphore

默认属性:hystrix.command.default.execution.isolation.strategy

实例属性:hystrix.command.HystrixCommandKey.execution.isolation.strategy

实例默认的设置:


// to use thread isolation

HystrixCommandProperties.Setter()

.withExecutionIsolationStrategy(ExecutionIsolationStrategy.THREAD)

// to use semaphore isolation

HystrixCommandProperties.Setter()

.withExecutionIsolationStrategy(ExecutionIsolationStrategy.SEMAPHORE)

execution.isolation.thread.timeoutInMilliseconds

设置调用者等待命令执行的超时限制,超过此时间,HystrixCommand被标记为TIMEOUT,并执行回退逻辑。

注意:超时会作用在HystrixCommand.queue(),即使调用者没有调用get()去获得Future对象。

默认值:命令执行超时时间,默认1000ms

默认属性: 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

  • 最大并发请求数,默认10,该参数当使用ExecutionIsolationStrategy.SEMAPHORE策略时才有效。如果达到最大并发请求数,请求会被拒绝。理论上选择semaphore size的原则和选择thread size一致,但选用semaphore时每次执行的单元要比较小且执行速度快(ms级别),否则的话应该用thread。

  • 设置当使用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

  • 如果并发数达到该设置值,请求会被拒绝和抛出异常并且fallback不会被调用

  • 设置调用线程产生的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()。

  • 当执行失败或者请求被拒绝,是否会尝试调用hystrixCommand.getFallback() 。默认true

  • 默认值:true

默认属性:hystrix.command.default.fallback.enabled

实例属性:hystrix.command.HystrixCommandKey.fallback.enabled

实例默认的设置:

HystrixCommandProperties.Setter().withFallbackEnabled(boolean value)

断路器(Circuit Breaker)

circuitBreaker.enabled

设置断路器是否起作用,用来跟踪circuit的健康性,如果未达标则让request短路。默认true

  • 默认值:true
默认属性:hystrix.command.default.circuitBreaker.enabled

实例属性:hystrix.command.HystrixCommandKey.circuitBreaker.enabled

实例默认的设置:

HystrixCommandProperties.Setter().withCircuitBreakerEnabled(boolean value)

circuitBreaker.requestVolumeThreshold

设置在一个滚动窗口中,打开断路器的最少请求数。

  • 比如:如果值是20,在一个窗口内(比如10秒),收到19个请求,即使这19个请求都失败了,断路器也不会打开。

  • 一个rolling window内最小的请求数。如果设为20,那么当一个rolling window的时间内(比如说1个rolling window是10秒)收到19个请求,即使19个请求都失败,也不会触发circuit break。默认20

默认值:20


默认属性:hystrix.command.default.circuitBreaker.requestVolumeThreshold

实例属性:hystrix.command.HystrixCommandKey.circuitBreaker.requestVolumeThreshold

实例默认的设置:

HystrixCommandProperties.Setter().withCircuitBreakerRequestVolumeThreshold(int value)

circuitBreaker.sleepWindowInMilliseconds

设置在回路被打开,拒绝请求到再次尝试请求并决定回路是否继续打开的时间,触发短路的时间值,当该值设为5000时,则当触发circuit break后的5000毫秒内都会拒绝request,也就是5000毫秒后才会关闭circuit。默认5000

  • 默认值:5000(毫秒)
默认属性:hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds

实例属性:hystrix.command.HystrixCommandKey.circuitBreaker.sleepWindowInMilliseconds

实例默认的设置:

HystrixCommandProperties.Setter().withCircuitBreakerSleepWindowInMilliseconds(int value)

circuitBreaker.errorThresholdPercentage

设置打开回路并启动回退逻辑的错误比率,错误比率阀值,如果错误率>=该值,circuit会被打开,并短路所有请求触发fallback。默认50

  • 默认值:50
默认属性:hystrix.command.default.circuitBreaker.errorThresholdPercentage

实例属性:hystrix.command.HystrixCommandKey.circuitBreaker.errorThresholdPercentage

实例默认的设置:

HystrixCommandProperties.Setter().withCircuitBreakerErrorThresholdPercentage(int value)

circuitBreaker.forceOpen

如果该属性设置为true,强制断路器进入打开状态,将会拒绝所有的请求,强制打开熔断器,如果打开这个开关,那么拒绝所有request,默认false

该属性优先级比circuitBreaker.forceClosed高。

  • 默认值:false

默认属性:hystrix.command.default.circuitBreaker.forceOpen

实例属性:hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen

实例默认的设置:

HystrixCommandProperties.Setter().withCircuitBreakerForceOpen(boolean value)

circuitBreaker.forceClosed

如果该属性设置为true,强制断路器进入关闭状态,将会允许所有的请求,无视错误率,强制关闭熔断器 如果这个开关打开,circuit将一直关闭且忽略circuitBreaker.errorThresholdPercentage

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

实例默认的设置

HystrixCollapserProperties.Setter().withTimerDelayInMilliseconds(int value)

requestCache.enabled

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

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

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

实例默认的设置

HystrixCollapserProperties.Setter().withRequestCacheEnabled(boolean value)

线程池属性

  • 每秒最大支撑的请求数 (99%平均响应时间 缓存值),比如:每秒能处理1000个请求,99%的请求响应时间是60ms,那么公式是:(0.060 0.012)

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

coreSize

设置核心线程池大小。

  • 默认值:10

    • 线程数默认值10适用于大部分情况(有时可以设置得更小),如果需要设置得更大,那有个基本得公式可以follow:
    • requests per second at peak when healthy × 99th percentile latency in seconds some breathing room
默认属性: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

  • 设置统计的滚动窗口的时间段大小。该属性是线程池保持指标时间长短。

  • 设置统计的时间窗口值的,毫秒值,circuit break 的打开会根据1个rolling window的统计来计算。若rolling window被设为10000毫秒,则rolling window会被分成n个buckets,每个bucket包含success,failure,timeout,rejection的次数的统计信息。

默认值: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,否则会抛出异常。

设置一个rolling window被划分的数量,若numBuckets=10,rolling window=10000,那么一个bucket的时间即1秒。必须符合rolling window % numberBuckets == 0。默认10

默认值:10

可能的值:任何能被metrics.rollingStats.timeInMilliseconds整除的值。

默认属性:hystrix.threadpool.default.metrics.rollingStats.numBuckets

实例属性:hystrix.threadpool.HystrixThreadPoolProperties.metrics.rollingStats.numBuckets

实例默认的设置:HystrixThreadPoolProperties.Setter()

.withMetricsRollingStatisticalWindowBuckets(int value)

其他配置属性

  • 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 快照(用来统计成功和错误绿)的间隔,默认500ms33 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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgjjech
系列文章
更多 icon
同类精品
更多 icon
继续加载