How to create circuit breaker config from application.properties?
Asked Answered
M

1

8

I have the following configuration with which I create circuit breakers at runtime:


@Configuration
public class CircuitBreakerConfiguration
{

    public final static String DEFAULT_CIRCUIT_BREAKER_REGISTRY = "DEFAULT_CIRCUIT_BREAKER_REGISTRY";

    private CircuitBreakerConfig getCircuitBreakerConfig()
    {
        return CircuitBreakerConfig.custom()
                .failureRateThreshold(10)
                .waitDurationInOpenState(Duration.ofMillis(30000))
                .permittedNumberOfCallsInHalfOpenState(2)
                .slidingWindowType(CircuitBreakerConfig.SlidingWindowType.COUNT_BASED)
                .slidingWindowSize(5)
                .automaticTransitionFromOpenToHalfOpenEnabled(true)
                .recordExceptions(CheckAvailabilityException.class)
                .build();

    }

    @Bean
    @Qualifier(DEFAULT_CIRCUIT_BREAKER_REGISTRY)
    public CircuitBreakerRegistry getCircuitBreakerRegistry()
    {
        return CircuitBreakerRegistry.of(getCircuitBreakerConfig());
    }
}

I want to move these configurations to my application.properties file.

I tried the following to override the default configs:

resilience4j.circuitbreaker.configs.default.sliding-window-size=10
resilience4j.circuitbreaker.configs.default.sliding-window-type=COUNT_BASED
resilience4j.circuitbreaker.configs.default.failure-rate-threshold=50
resilience4j.circuitbreaker.configs.default.wait-duration-in-open-state=30s
resilience4j.circuitbreaker.configs.default.permitted-number-of-calls-in-half-open-state=2
resilience4j.circuitbreaker.configs.default.automatic-transition-from-open-to-half-open-enabled=true
resilience4j.circuitbreaker.configs.default.record-exceptions=com.example.web.domain.checkavailability.exceptions.CheckAvailabilityException

However, this doesn't seem to override the default configs too.

Manganate answered 17/8, 2020 at 0:46 Comment(5)
Circuit breaker configuration is incorrectIrisation
@Irisation where exactly?Manganate
I think you need to give the name to the circuit breaker say 'abc' then config will be resilience4j.circuitbreaker.instances.abc.sliding-window-type=COUNT_BASED and also give this same name during registering circuit breaker to circuitBreakerRegistryIrisation
Did you ever get a solution to this Saif?Crossfertilization
@LesBuchanan not yet.Manganate
H
5

Don't know if this is still an open topic, but I was struggling with a similar question and managed to find this article which offers some guidance: https://heapsteep.com/13-circuit-breaker-resilience4j/

As such, here's what I've done:

  1. create the default configs I want:
resilience4j.circuitbreaker.configs.default.register-health-indicator=true
resilience4j.circuitbreaker.configs.default.permitted-number-of-calls-in-half-open-state=3
resilience4j.circuitbreaker.configs.default.sliding-window-type=TIME_BASED
resilience4j.circuitbreaker.configs.default.minimum-number-of-calls=50
  1. create a instance that I will use in the annotation, and set up it like so:
resilience4j.circuitbreaker.instances.myInstance.baseConfig=default
  1. annotate your method:
    @Override
    @CircuitBreaker(name = "myInstance", fallbackMethod = "fallbackmethod")
    public String getName(int ID) {
        \\ ...
    }

I have been resting this setup with the @Retry function, but I would assume that it works the same way.

Headword answered 18/3, 2022 at 9:4 Comment(3)
Yes we are still using a workaround. Thanks for adding an answer. I'll definitely try to see if it's a fit for our case. Can you also add the versions of the dependencies you are using?Manganate
@Manganate we are using version 1.6.1Headword
How to enable/disable circuit breaker spring.cloud.circuitbreaker.resilience4j.enabled not workingPliske

© 2022 - 2024 — McMap. All rights reserved.