Resilience4j on spring-boot2 - circuit breaker not opening
Asked Answered
J

2

7

Following the getting started guide (https://resilience4j.readme.io/docs/getting-started-3) and the demo project (https://github.com/resilience4j/resilience4j-spring-boot2-demo) I wanted to test it by myself creating a simpler test application.

Here is the code:


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ServiceConfiguration {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

Controller:


@RestController
public class ServiceController {

    @Autowired
    private  AlbumService albumService;


    @GetMapping("/albums")
    ResponseEntity<String> getAlbums() {
       return albumService.getAlbums();
    }

}

And the service class:

@Slf4j
@Service
public class AlbumService {

    @Autowired
    private  RestTemplate restTemplate;

    @CircuitBreaker(name = "albumService", fallbackMethod = "getDefaultAlbumList")
    public ResponseEntity<String> getAlbums() {
        String url = MockNeat.secure().probabilites(String.class)
                .add(0.7, "https://wrong-url.com")
                .add(0.3, "https://jsonplaceholder.typicode.com/albums").val();
        return new ResponseEntity<>(restTemplate.getForObject(url, String.class), HttpStatus.OK);
    }

    private ResponseEntity<String> getDefaultAlbumList(Exception ex) throws URISyntaxException, IOException {
        log.info("Recovered: " + ex.getMessage());
        return new ResponseEntity<>(new String(Files.readAllBytes(Paths.get(getClass().getClassLoader().getResource("fallback-album-list.json").toURI()))), HttpStatus.OK);
    }
}

Finally here the application file:

resilience4j:
  circuitbreaker:
    configs:
      default:
        registerHealthIndicator: true
        slidingWindowSize: 10
        minimumNumberOfCalls: 5
        permittedNumberOfCallsInHalfOpenState: 3
        automaticTransitionFromOpenToHalfOpenEnabled: true
        waitDurationInOpenState: 5s
        failureRateThreshold: 50
        eventConsumerBufferSize: 10
    instances:
      albumService:
        baseConfig: default

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always
  metrics:
    distribution:
      percentiles-histogram:
        http:
          server:
            request: true
        resielence4j:
          circuitbreaker:
            calls: true

I have introduced a random error with 70% probability to test the circuit breaker. However, the circuit never opens and I always get error. I have no idea what I am missing! Any help?

Jealousy answered 20/5, 2020 at 15:32 Comment(0)
J
12

Found the problem! I have forgotten to include spring-aop as dependency! Here is my pom.xml in case someone encounters the same problem:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>r4j</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>r4j</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <repositories>
        <repository>
            <id>jcenter</id>
            <url>https://jcenter.bintray.com/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>net.andreinc.mockneat</groupId>
            <artifactId>mockneat</artifactId>
            <version>0.3.9</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-spring-boot2</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>2.3.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.3.0.RELEASE</version>
        </dependency>
    </dependencies>

</project>
Jealousy answered 21/5, 2020 at 8:29 Comment(2)
Same issue, and I also was missing that from my pom, but adding it fixed nothing. How did you manage to find out this was your problem? Wondering if whatever debugging or reference material helped you find your answer can help me find mine...Salenasalene
Adding "aop" resolved the issue. The fallback method getting invoked.Biddick
P
0
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-spring-boot3</artifactId>
            <version>2.0.2</version>
        </dependency>

you have to add both @Slf4j (resilience4j-spring-boot3) and AOP dependency

Pterodactyl answered 30/8 at 11:20 Comment(1)
This is a duplicate answer. The OP already came to the same conclusion and explained that Spring AOP was missing in his answer.Aubarta

© 2022 - 2024 — McMap. All rights reserved.