2,CircuitBreakerConfig(熔断器配置)
CircuitBreakerConfig类中封装了与熔断器相关的配置属性,属性包括:请求调用失败的阈值、熔断器在打开状态时的持续时间、熔断器在半开状态下的Ring Buffer大小、熔断器在关闭状态下的Ring Buffer大小、是否记录请求调用失败的断言。Ring Buffer的原理在后面研究CircuitBreakerMetrics部分时再详细讲解,现在只需要了解,它是一个存储每次请求调用成功与否的环形缓存区。
另外CircuitBreakerConfig类通过Builder模式构造CircuitBreakerConfig实例及流式的设置配置属性值。
需主要关注的源码如下:
public class CircuitBreakerConfig {
//请求调用失败的阈值,百分比。默认是50%
public static final int DEFAULT_MAX_FAILURE_THRESHOLD = 50; // Percentage
//熔断器在打开状态时的持续时间。默认是60秒
public static final int DEFAULT_WAIT_DURATION_IN_OPEN_STATE = 60; // Seconds
//熔断器在半开状态下的ring buffer大小。默认10
public static final int DEFAULT_RING_BUFFER_SIZE_IN_HALF_OPEN_STATE = 10;
//熔断器在关闭状态下的ring buffer大小。默认100
public static final int DEFAULT_RING_BUFFER_SIZE_IN_CLOSED_STATE = 100;
//是否记录请求调用失败的断言,默认所有异常都记录。
public static final Predicate
......
/** 构造者模式 */
public static class Builder {
private Predicate
private Predicate
//请求调用失败,存储异常记录的集合
private Class<? extends Throwable>[] recordExceptions = new Class[0];
//请求调用失败,忽略异常记录的集合
private Class<? extends Throwable>[] ignoreExceptions = new Class[0];
private float failureRateThreshold = DEFAULT_MAX_FAILURE_THRESHOLD;
private int ringBufferSizeInHalfOpenState = DEFAULT_RING_BUFFER_SIZE_IN_HALF_OPEN_STATE;
private int ringBufferSizeInClosedState = DEFAULT_RING_BUFFER_SIZE_IN_CLOSED_STATE;
private Duration waitDurationInOpenState = Duration.ofSeconds(DEFAULT_WAIT_DURATION_IN_OPEN_STATE);
private boolean automaticTransitionFromOpenToHalfOpenEnabled = false;
/** 构造CircuitBreakerConfig实例 */
public CircuitBreakerConfig build() {
buildErrorRecordingPredicate();
CircuitBreakerConfig config = new CircuitBreakerConfig();
config.waitDurationInOpenState = waitDurationInOpenState;
config.failureRateThreshold = failureRateThreshold;
config.ringBufferSizeInClosedState = ringBufferSizeInClosedState;
config.ringBufferSizeInHalfOpenState = ringBufferSizeInHalfOpenState;
config.recordFailurePredicate = errorRecordingPredicate;
config.automaticTransitionFromOpenToHalfOpenEnabled = automaticTransitionFromOpenToHalfOpenEnabled;
return config;
}
/** 异常记录集合与异常忽略集合取交集,如果有值则为false,否则为true。*/
private void buildErrorRecordingPredicate() {
this.errorRecordingPredicate =
getRecordingPredicate()
.and(buildIgnoreExceptionsPredicate()
.orElse(DEFAULT_RECORD_FAILURE_PREDICATE));
}
......
}
......
}
下一篇文章《Resilience4j源码解析-2.3 CircuitBreaker模块之有限状态机》讲解熔断器的核心理念-有限状态机,及状态转换部分。
源码的中文注释地址:
https://github.com/Justin02180218/resilience4j
==大家可以关注我的微信公众号,后续文章将在公众号中同步更新==