Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

spring boot - CircuitBreaker not loading defaults from yaml file

I have a Spring Boot 2, Java 8 application using Resilience4j (version 1.3) CircuitBreaker (CB). The default CB properties in my application yaml look like this -

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

I was hoping every CB I create would end up with these default values but it does not appear to be the case.

// 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);
}

I am hoping I don't have to resort to case 3. Is there something I am doing incorrectly in Case 1 and 2? For the purposes of this testing, I am using an InMemoryCircuitBreakerRegistry. I appreciate your help.

Thank you

question from:https://stackoverflow.com/questions/65909665/circuitbreaker-not-loading-defaults-from-yaml-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Please use the Spring Boot starter from Resilience4j and don't create a CircuitBreaker manually via a bean. Everything is done automatically for you on-the-fly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...