CircuitBreaker 不从 yaml 文件加载默认值
CircuitBreaker not loading defaults from yaml file
我有一个 Spring Boot 2,Java 8 应用程序使用 Resilience4j(1.3 版)CircuitBreaker (CB)。我的应用程序 yaml 中的默认 CB 属性如下所示 -
resilience4j.circuitbreaker:
configs:
default:
registerHealthIndicator: true
slidingWindowSize: 10
minimumNumberOfCalls: 5
permittedNumberOfCallsInHalfOpenState: 3
automaticTransitionFromOpenToHalfOpenEnabled: true
waitDurationInOpenState: 10000
failureRateThreshold: 50
eventConsumerBufferSize: 10
recordExceptions:
- org.springframework.web.client.HttpServerErrorException
- java.util.concurrent.TimeoutException
- java.io.IOException
ignoreExceptions:
- org.springframework.web.client.HttpClientErrorException
我希望我创建的每个 CB 最终都具有这些默认值,但事实并非如此。
// Case 1: Does not load defaults from yaml
@Bean
CircuitBreaker xCircuitBreaker(CircuitBreakerRegistry circuitBreakerRegistry) {
return circuitBreakerRegistry.circuitBreaker("xCircuitBreaker");
}
// Case 2: Does not load defaults from yaml
@Bean
CircuitBreaker yCircuitBreaker() {
return CircuitBreaker.ofDefaults("yCircuitBreaker");
}
// Case 3: Works
@Bean
public CircuitBreaker zCircuitBreaker(CircuitBreakerRegistry circuitBreakerRegistry,
@Value("${resilience4j.circuitbreaker.configs.default.slidingWindowSize}") Integer slidingWindowSize,
@Value("${resilience4j.circuitbreaker.configs.default.permittedNumberOfCallsInHalfOpenState}") Integer permittedNumberOfCallsInHalfOpenState,
@Value("${resilience4j.circuitbreaker.configs.default.minimumNumberOfCalls}") Integer minimumNumberOfCalls,
@Value("${resilience4j.circuitbreaker.configs.default.failureRateThreshold}") Integer failureRateThreshold,
@Value("${resilience4j.circuitbreaker.configs.default.waitDurationInOpenState}") Integer waitDurationInOpenState,
@Value("${resilience4j.circuitbreaker.configs.default.automaticTransitionFromOpenToHalfOpenEnabled}") Boolean automaticTransitionFromOpenToHalfOpenEnabled) {
CircuitBreakerConfig cfg = CircuitBreakerConfig.custom()
.slidingWindowSize(slidingWindowSize)
.minimumNumberOfCalls(minimumNumberOfCalls)
.permittedNumberOfCallsInHalfOpenState(permittedNumberOfCallsInHalfOpenState)
.failureRateThreshold(failureRateThreshold)
.waitDurationInOpenState(Duration.ofMillis(waitDurationInOpenState))
.automaticTransitionFromOpenToHalfOpenEnabled(automaticTransitionFromOpenToHalfOpenEnabled)
.recordException(recordFailurePredicate())
.recordExceptions(HttpServerErrorException.class)
.ignoreExceptions(HttpClientErrorException.class)
.build();
return circuitBreakerRegistry.circuitBreaker("zCircuitBreaker", cfg);
}
我希望我不必求助于案例 3。在案例 1 和案例 2 中我做错了什么吗?出于此测试的目的,我使用了 InMemoryCircuitBreakerRegistry
。感谢您的帮助。
谢谢
请使用 Resilience4j 中的 Spring 引导启动器,不要通过 bean 手动创建断路器。一切都是即时自动为您完成的。
我有一个 Spring Boot 2,Java 8 应用程序使用 Resilience4j(1.3 版)CircuitBreaker (CB)。我的应用程序 yaml 中的默认 CB 属性如下所示 -
resilience4j.circuitbreaker:
configs:
default:
registerHealthIndicator: true
slidingWindowSize: 10
minimumNumberOfCalls: 5
permittedNumberOfCallsInHalfOpenState: 3
automaticTransitionFromOpenToHalfOpenEnabled: true
waitDurationInOpenState: 10000
failureRateThreshold: 50
eventConsumerBufferSize: 10
recordExceptions:
- org.springframework.web.client.HttpServerErrorException
- java.util.concurrent.TimeoutException
- java.io.IOException
ignoreExceptions:
- org.springframework.web.client.HttpClientErrorException
我希望我创建的每个 CB 最终都具有这些默认值,但事实并非如此。
// Case 1: Does not load defaults from yaml
@Bean
CircuitBreaker xCircuitBreaker(CircuitBreakerRegistry circuitBreakerRegistry) {
return circuitBreakerRegistry.circuitBreaker("xCircuitBreaker");
}
// Case 2: Does not load defaults from yaml
@Bean
CircuitBreaker yCircuitBreaker() {
return CircuitBreaker.ofDefaults("yCircuitBreaker");
}
// Case 3: Works
@Bean
public CircuitBreaker zCircuitBreaker(CircuitBreakerRegistry circuitBreakerRegistry,
@Value("${resilience4j.circuitbreaker.configs.default.slidingWindowSize}") Integer slidingWindowSize,
@Value("${resilience4j.circuitbreaker.configs.default.permittedNumberOfCallsInHalfOpenState}") Integer permittedNumberOfCallsInHalfOpenState,
@Value("${resilience4j.circuitbreaker.configs.default.minimumNumberOfCalls}") Integer minimumNumberOfCalls,
@Value("${resilience4j.circuitbreaker.configs.default.failureRateThreshold}") Integer failureRateThreshold,
@Value("${resilience4j.circuitbreaker.configs.default.waitDurationInOpenState}") Integer waitDurationInOpenState,
@Value("${resilience4j.circuitbreaker.configs.default.automaticTransitionFromOpenToHalfOpenEnabled}") Boolean automaticTransitionFromOpenToHalfOpenEnabled) {
CircuitBreakerConfig cfg = CircuitBreakerConfig.custom()
.slidingWindowSize(slidingWindowSize)
.minimumNumberOfCalls(minimumNumberOfCalls)
.permittedNumberOfCallsInHalfOpenState(permittedNumberOfCallsInHalfOpenState)
.failureRateThreshold(failureRateThreshold)
.waitDurationInOpenState(Duration.ofMillis(waitDurationInOpenState))
.automaticTransitionFromOpenToHalfOpenEnabled(automaticTransitionFromOpenToHalfOpenEnabled)
.recordException(recordFailurePredicate())
.recordExceptions(HttpServerErrorException.class)
.ignoreExceptions(HttpClientErrorException.class)
.build();
return circuitBreakerRegistry.circuitBreaker("zCircuitBreaker", cfg);
}
我希望我不必求助于案例 3。在案例 1 和案例 2 中我做错了什么吗?出于此测试的目的,我使用了 InMemoryCircuitBreakerRegistry
。感谢您的帮助。
谢谢
请使用 Resilience4j 中的 Spring 引导启动器,不要通过 bean 手动创建断路器。一切都是即时自动为您完成的。