# 1.resilience4j涉及的相关配置类
public class CommonProperties {
Map<String, String> tags = new HashMap<>();
public Map<String, String> getTags() {
return tags;
}
public void setTags(Map<String, String> tags) {
this.tags = tags;
}
}
父类CommonProperties涉及的子类包含RateLimiterConfigurationProperties、CircuitBreakerConfigurationProperties、RetryConfigurationProperties、TimeLimiterConfigurationProperties等。
## 1.1.熔断器相关配置
通过`CircuitBreakerConfiguration`初始化类`CircuitBreakerRegistry`,该类存储了全部CircuitBreaker实例。并且CircuitBreaker实例所需的配置信息是通过`CommonProperties的子类`转化为`CircuitBreakerConfig`后得到的。
> 类CircuitBreakerConfig存在的配置项只是CommonProperties的子类中部分配置项内容。
public class CircuitBreakerConfigurationProperties extends CommonProperties {
//这个是可以针对特定业务线配置,但是其name值如下图中ID属性值。目前本人尚未解决是否可以简化name值的套路
private Map<String, InstanceProperties> instances = new HashMap<>();
private Map<String, InstanceProperties> configs = new HashMap<>();
public Optional<InstanceProperties> findCircuitBreakerProperties(String name) {
InstanceProperties instanceProperties = instances.get(name);
if (instanceProperties == null) {
instanceProperties = configs.get("default");
}
return Optional.ofNullable(instanceProperties);
}
public CircuitBreakerConfig createCircuitBreakerConfig(String backendName,
InstanceProperties instanceProperties,
CompositeCustomizer<CircuitBreakerConfigCustomizer> compositeCircuitBreakerCustomizer) {
if (StringUtils.isNotEmpty(instanceProperties.getBaseConfig())) {
InstanceProperties baseProperties = configs.get(instanceProperties.getBaseConfig());
if (baseProperties == null) {
throw new ConfigurationNotFoundException(instanceProperties.getBaseConfig());
}
return buildConfigFromBaseConfig(instanceProperties, baseProperties,
compositeCircuitBreakerCustomizer,
backendName);
}
return buildConfig(custom(), instanceProperties, compositeCircuitBreakerCustomizer,
backendName);
}
}
?
如上配置初始化过程只是将公共配置项复制到最终的CircuitBreakerConfig中,在属性configs中还是配置文件中配置的原始内容。
![在这里插入图片描述](https://img-blog.csdnimg.cn/a68d34093cd549918b469bfad3856b39.png)
resilience4j:
timelimiter:
configs:
default:
timeoutDuration: 1s
circuitbreaker:
configs:
myfans:
baseConfig: common // 公共配置项
registerHealthIndicator: true // 独有配置项
default:
baseConfig: common
registerHealthIndicator: true // 独有配置项
common:
minimumNumberOfCalls: 3
slidingWindowSize: 10
automaticTransitionFromOpenToHalfOpenEnabled: true
如上所示,即类CircuitBreakerConfigurationProperties中configs属性中对应三个名为myfans、default、common的配置项内容。初始化CircuitBreakerConfig中配置时并不会将common中内容添加到myfans、default中。