Resilience4j轻量级容错框架使用说明
约 1442 字大约 5 分钟
手册容错熔断限流
2025-03-07
官方文档地址:https://resilience4j.readme.io/v1.7.0/docs/circuitbreaker
resilience4j框架提供了 熔断 、 高频控制 、 隔离 、 限流 、 限时 、 重试 等多种高可用处理机制。
circuitbreaker(断路器)
CircuitBreaker断路器是由一个有限状态机实现的,
包含三个一般性状态: CLOSED , OPEN , HALF_OPEN (关闭、打开、半开)
CLOSED (关闭):说明次数断路器是关闭的,服务调用是正常的。
OPEN (打开):说明断路器是打开的,表示调用的服务出现了异常,会暂停调用服务,转为执行指定的服务降级方法。
HALF_OPEN (半开):此时断路器处于运行状态,但是还是会允许一定数量的请求,去调用服务,其他的请求,转为执行指定的服务降级方法。(例:一共有100个请求,现在断路器状态为半开,那么此时就只允许10个请求去调用服务,其他90个请求去执行降级方法)(允许请求的数量由配置属性permittedNumberOfCalls InHalfOpenState控制,默认为10)
两个特定状态: DISABLED , FORCED_OPEN (禁用、强开)
配置属性说明
配置属性 | 默认值 | 说明 |
---|---|---|
failureRateThreshold | 50 | Configures the failure rate threshold in percentage. When the failure rate is equal or greater than the threshold the CircuitBreaker transitions to open and starts short-circuiting calls.以百分比为单位配置失败率阈值。当故障率等于或大于阈值时, CircuitBreaker 断路器的状态转换为 OPEN ,并开始调用短路。 |
slowCallRateThreshold | 100 | Configures a threshold in percentage. The CircuitBreaker considers a call as slow when the call duration is greater than slowCallDurationThreshold When the percentage of slow calls is equal or greater the threshold, the CircuitBreaker transitions to open and starts short-circuiting calls.配置以百分比为单位的阈值。当调用持续时间大于 slowCallDurationThreshold 时, CircuitBreaker 认为调用是慢速调用。当慢速调用的百分比等于或大于阈值时,CircuitBreaker 断路器的状态转换为 OPEN ,并开始调用短路。 |
slowCallDurationThreshold | 60000 [ms] | Configures the duration threshold above which calls are considered as slow and increase the rate of slow calls.配置时长阈值,超过该阈值的调用会被视为慢速调用,并增加慢速调用的比率(可以理解为慢速调用次数的次数增加了,增加到一定数量后就会打开断路器)。 |
permittedNumberOfCalls InHalfOpenState | 10 | Configures the number of permitted calls when the CircuitBreaker is half open.配置断路器处于 半开状态HALF_OPEN 时,允许的调用数量。 |
maxWaitDurationInHalfOpenState | 0 [ms] | Configures a maximum wait duration which controls the longest amount of time a CircuitBreaker could stay in Half Open state, before it switches to open. Value 0 means Circuit Breaker would wait infinitely in HalfOpen State until all permitted calls have been completed.配置最大等待时间,控制断路器在切换到 打开状态OPEN 之前,保持 半开状态HALF_OPEN 的最长时间。值0表示断路器将无限地等待半打开状态,直到所有允许的调用都完成。 |
slidingWindowType | COUNT_BASED | Configures the type of the sliding window which is used to record the outcome of calls when the CircuitBreaker is closed. Sliding window can either be count-based or time-based. If the sliding window is COUNT_BASED, the last slidingWindowSize calls are recorded and aggregated. If the sliding window is TIME_BASED, the calls of the last slidingWindowSize seconds recorded and aggregated.配置滑动窗口的类型,该窗口用于记录断路器关闭时调用的结果。滑动窗口可以是 基于计数COUNT_BASED 或 基于时间TIME_BASED 的。如果滑动窗口是COUNT_BASED ,最后的 slidingWindowSize 调用将被记录和汇总。如果滑动窗口是 TIME_BASED 的,那么最后一个 slidingWindowSize 的调用将被记录并汇总。 |
slidingWindowSize | 100 | Configures the size of the sliding window which is used to record the outcome of calls when the CircuitBreaker is closed.配置滑动窗口的大小,该窗口用于记录断路器关闭时调用的结果。 |
minimumNumberOfCalls | 100 | Configures the minimum number of calls which are required (per sliding window period) before the CircuitBreaker can calculate the error rate or slow call rate. For example, if minimumNumberOfCalls is 10, then at least 10 calls must be recorded, before the failure rate can be calculated. If only 9 calls have been recorded the CircuitBreaker will not transition to open even if all 9 calls have failed.配置在断路器可以计算错误率或慢速调用率之前所需的最小调用数(每个滑动窗口周期)。例如,如果minimumNumberOfCalls为10,那么在计算故障率之前,必须记录至少10个调用。如果只记录了9个调用,即使所有9个调用都失败了,断路器也不会切换到打开。 |
waitDurationInOpenState | 60000 [ms] | The time that the CircuitBreaker should wait before transitioning from open to half-open.断路器从 OPEN 转换到 HALF_OPEN 之前应等待的时间。 |
automaticTransition FromOpenToHalfOpenEnabled | false | If set to true it means that the CircuitBreaker will automatically transition from open to half-open state and no call is needed to trigger the transition. A thread is created to monitor all the instances of CircuitBreakers to transition them to HALF_OPEN once waitDurationInOpenState passes. Whereas, if set to false the transition to HALF_OPEN only happens if a call is made, even after waitDurationInOpenState is passed. The advantage here is no thread monitors the state of all CircuitBreakers. |
recordExceptions | empty | A list of exceptions that are recorded as a failure and thus increase the failure rate. Any exception matching or inheriting from one of the list counts as a failure, unless explicitly ignored via ignoreExceptions . If you specify a list of exceptions, all other exceptions count as a success, unless they are explicitly ignored by ignoreExceptions . |
ignoreExceptions | empty | A list of exceptions that are ignored and neither count as a failure nor success. Any exception matching or inheriting from one of the list will not count as a failure nor success, even if the exceptions is part of recordExceptions . |
recordFailurePredicate | throwable -> true By default all exceptions are recored as failures. | A custom Predicate which evaluates if an exception should be recorded as a failure. The Predicate must return true if the exception should count as a failure. The Predicate must return false, if the exception should count as a success, unless the exception is explicitly ignored by ignoreExceptions . |
ignoreExceptionPredicate | throwable -> false By default no exception is ignored. | A custom Predicate which evaluates if an exception should be ignored and neither count as a failure nor success. The Predicate must return true if the exception should be ignored. The Predicate must return false, if the exception should count as a failure. |