javaspring-bootmicroservicescircuit-breaker

Spring Boot 3 ignores @CircuitBreaker


I am learning microservices and trying to implement circuit breaker pattern in one of my microservices. At the beginning i wanted to pick Netflix Hystrix library but after some googling i found out that it is now deprecated and is no longer supported by Spring Boot (CMIIW). So I opt for Resillience4j library.

I have method in one of the services that imitates the slow run on random call (it runs 11s). So I want CircuitBreaker to kill the slow running call

The thing is that when i imported all required dependencies Spring Boot 3 just ignores @CircuitBreaker annotation which I put above the target method.

I'm using Spring Boot 3.3.2 and Spring Cloud 2023.0.1

Here is the dependencies i have in my pom.xml

// another deps not connected with the problem

<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-spring-boot3</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

And here is the slow running method which is called when of the endpoints is hit:

    private void randomlyRunLong() {
        Random rand = new Random();
        int randomNum = rand.nextInt((3 - 1) + 1) + 1;
        if (randomNum == 3) sleep();
    }

    private void sleep() {
        try {
            Thread.sleep(11000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @CircuitBreaker(name = "getLicenseByOrgCircuitBreaker")
    public List<License> getLicensesByOrg(String organizationId) {
        randomlyRunLong();

        return licenseRepository.findByOrganizationId(organizationId);
    }

I also tried to configure circuit breaker through application.yaml, but these settings don't bring any effect:

resilience4j:
    circuitbreaker:
        configs:
            default:
                slow-call-duration-threshold: 10s
                failure-rate-threshold: 1
                slow-call-rate-threshold: 1

Here is the screen from Postman Response in Postman
As it can be seen, there is no interruption from circuit breaker.

I will appreciate any ideas, so if anyone can help i will be grateful

UPD: Here is the response from the /health endpoint after 3 calls of the long running method: Postman response

(P.S. English is not my mother language so SRY for possible mistakes)


Solution

  • So, the problem was in my inattention while writing configuration for the circuit breaker.

    minimumNumberOfCalls - Configures the minimum number of calls which are required (per sliding window period) before the CircuitBreaker can calculate the error rate or slow call rate.

    I didn't specified this setting in my application.yaml file so it took its default value (100), that's why circuit breaker didn't calculate the error rate or slow call rate.

    When i specified desirable value for this setting, everything start working as I expected.

    So, my advice for everyone is to be careful when adjusting your circuit breaker settings and read the official docs