Change timeout for Spring Cloud Circuit Breaker at runtime?

1.6k views Asked by At

I'm using Spring Cloud Circuit Breaker 2.0.0 (resilience4j implementation) for circuit breaking and timeouts in my application. I've created the following configuration:

@Bean
public Customizer<Resilience4JCircuitBreakerFactory> defaultCustomizer() {
    return factory -> 
        factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
                .timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(10)).build())
                .circuitBreakerConfig(..)
                .build());
}

Now I want to write an integration test to verify that my behavior is correct when a timeout occurs. For this to work, I'd like to temporarily change the timeout duration specified in the configuration above to something like 1 millisecond instead of 10 seconds.

So my question is: How can I change the value of the timeout of the TimeLimiterConfig(temporarily) when I'm writing a Spring Boot integration test?

1

There are 1 answers

0
Felipe On BEST ANSWER

You can use the @Value Spring annotation that retrieves the value at a configuration file from your resource folder src/main/resources/common.properties.

@Bean
public Customizer<Resilience4JCircuitBreakerFactory> defaultCustomizer(
    @Value("${duration.milli:600}") int durationMilli) {
    return factory -> 
        factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
                .timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofMilli(durationMilli)).build())
                .circuitBreakerConfig(..)
                .build());
}

Then you set the value at src/main/resources/common.properties

duration.milli=600

When you are doing your test you can configure another resource file at the test folder src/test/resources/common.properties with a different value.

duration.milli=1