question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to configure resilience4j.circuitbreaker in @Bean without properties.yml

See original GitHub issue

Thanks for raising a Resilience4j issue. Please provide a brief description of your problem along with the versions you are using. If possible, please also consider putting together a complete JUnit test that reproduces the issue.

Resilience4j version:

Java version: 8

Problem description:

Hi, I’m trying to configure resilience without the need to put the settings in the properties.yml. I created my configuration bean but the circuit is not working

@Slf4j
@Configuration
@Component
@RefreshScope
public class CircuitBreakerAutoConfiguration {
  
  @Bean
  public CircuitBreakerRegistry defaultGenesisCircuitBreaker(
      @Value("${application.circuit-breaker.genesis.default."
          + "ringBufferSizeInClosedState:30}")
          int ringBufferSizeInClosedState,
      @Value("${application.circuit-breaker.genesis.default."
          + "ringBufferSizeInHalfOpenState:10}")
          int ringBufferSizeInHalfOpenState,
      @Value("${application.circuit-breaker.genesis.default."
          + "failureRateThreshold:50}")
          long failureRateThreshold,
      @Value("${application.circuit-breaker.genesis.default."
          + "waitDurationInOpenState:10000}")
          long waitDurationInOpenState) {
    log.info("Configuring custom Genesis Circuit Breaker genesis.default");
    return CircuitBreakerRegistry.of(circuitBreakerConfig());
  }

  @Bean
  public CircuitBreakerConfig circuitBreakerConfig() {
    log.info("circuitBreakerConfig");
    return CircuitBreakerConfig.custom()
            .minimumNumberOfCalls(2)
            .failureRateThreshold(50)
            .waitDurationInOpenState(Duration.ofMillis(1000))
            .permittedNumberOfCallsInHalfOpenState(2)
            .slidingWindowSize(2)
            .build();
  }

  @Bean
  public CircuitBreaker defaultCircuitBreaker() {
    CircuitBreakerConfig config = CircuitBreakerConfig.custom()
            .minimumNumberOfCalls(2)
            .build();
    return CircuitBreaker.of("default", config);
  }
}

I need to abstract this configuration to put it within a framework of my own. The only way it works is when I put the settings in the properties

resilience4j.circuitbreaker:
  configs:
    default:
      registerHealthIndicator: false
      slidingWindowSize: 10
      minimumNumberOfCalls: 5

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:21 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
Jememecommented, Apr 20, 2020

Like @RobWin indicated, I Autowired the CircuitBreakerRegistry instead of creating my own through CircuitBreakerRegistry.of(config). This allowed me to use registry.circuitBreaker(name, config) to add new circuit breakers with my custom configurations in my code without any yaml entries.

0reactions
sreesabari94commented, Oct 4, 2022

why not using customizers for circuit breaker configuration overriding ? which is supported already through resilience4j spring boot starter :

@Bean
public CircuitBreakerConfigCustomizer testCustomizer() {
    return CircuitBreakerConfigCustomizer
        .of("test", builder -> builder.slidingWindowSize(100));
}

Reference : https://resilience4j.readme.io/docs/getting-started-3

How to use this for multiple circuit breaker instances?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Resilience4J Circuit Breakers - Spring
You can configure CircuitBreaker and TimeLimiter instances in your application's configuration properties file. Property configuration has higher priority than ...
Read more >
Bean Configuration for Circuit Breaker of Resilience4J Using ...
The above config would work. Try setting a minimumNumberOfCalls to lower value(10) default value is 100. The circuit breaker needs to encounter ...
Read more >
Getting Started - resilience4j
You can configure your CircuitBreaker, Retry, RateLimiter, Bulkhead, Thread pool bulkhead and TimeLimiter instances in Spring Boot's application.yml config file ...
Read more >
Resilience4j Tutorial with Spring Boot | Circuit Breaker, Retry ...
In this tutorial, we will implement Resilience4j with Spring boot and different modules available for it. We will see the theory part and ......
Read more >
Circuit Breaker using Resilience4j | by Rupali Gupta - Medium
Add the required configurations to application.yml file or application.properties file resilience4j: circuitbreaker: configs: default:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found