4

I want to move my Circuit Breaker Configuration from application.yml file to some config java file as bean declaration beacuse it makes application.yml file to be large, Will it be possible for me to remove the configuration from applciation.yml and use configuration annotation to define circuit breaker configuration. I have config java file with following code like :

@Configuration
@Component
public class CircuitBreakConfig {

  @Bean
  public CircuitBreaker defaultCircuitBreaker() {
    CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
      .failureRateThreshold(50)
      .slidingWindowSize(5)
      .waitDurationInOpenState(Duration.ofMillis(6000))
      .recordExceptions(IOException.class, TimeoutException.class,DataIntegrityViolationException.class)
      .build();
  // Create a CircuitBreakerRegistry with a custom global configuration
  CircuitBreakerRegistry circuitBreakerRegistry = 
     CircuitBreakerRegistry.of(circuitBreakerConfig);

  // Get or create a CircuitBreaker from the CircuitBreakerRegistry 
  // with the global default configuration
   return circuitBreakerRegistry.circuitBreaker("default"); 
  }

}

In My service file I am annotating method for circuit breaker like

@CircuitBreaker(name = "default", fallbackMethod = "fallback")
    public Employee createEmployeeDefinition(Employee emp)  throws Exception{
        if(Objects.nonNull(sample)){
            return EmployeeRepository.save(sample);
        }
        return null;
    }

    public Employee fallback(Employee emp,Exception ex) throws Exception{
        System.out.println(ex.getClass()+"  "+ex.getMessage() );
        return null;
    }

With this current implementation , I am unable to reach open state in my circuit breaker.Kindly provide me some suggestions

Monesh
  • 103
  • 2
  • 8
  • The above config would work. Try setting a minimumNumberOfCalls to lower value(10) default value is 100. The circuit breaker needs to encounter 100 requests. Also waitDurationInOpenState can be increased to test as expected. Fallback method can return a default message or a exception instead of null. – Krithick S Nov 18 '22 at 16:30

1 Answers1

0

Please set minimumNumberOfCalls property

Naren
  • 1
  • 1
    Welcome to SO! This answer is very brief. Can you elaborate on it a bit, possibly with a code example? How does this solve the problem? Thanks for clarifying. – ggorlen Sep 17 '20 at 06:27