CircuitBreaker cannot be resolved to a type using Resilience4J

3.6k Views Asked by At

I am trying to build a simple CircuitBreaker in my Spring Boot (which uses Swagger).

The idea is that the first app, calls another which can be Up or Down, I want to implement a CircuitBreaker which gives an error message if the second app is down.

The main problem is that with I try to use the @CircuitBreaker annotation, says that it cannot be resolved to a type.

import...
import io.github.resilience4j.*;

public class AccountServiceImpl implements AccountService {

 Autowired private RestTemplate restTemplate;

  @CircuitBreaker( name = "test", fallbackMethod = "fallback")   
  public void createAccount(ConsentRequestDTO consent) {
    //code which makes the post
    //
  }

  public String fallback() {
    return "fallback-exceptiont";
  }
}

My pom.xml is

<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-spring-boot2</artifactId>
    <version>1.7.0</version>
  </dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
    <version>2.0.1</version>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-aop</artifactId>
  <version>2.3.1.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.github.resilience4j/resilience4j-retry -->
    <dependency>
      <groupId>io.github.resilience4j</groupId>
      <artifactId>resilience4j-circuitbreaker</artifactId>
      <version>1.7.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/io.github.resilience4j/resilience4j-core -->
    <dependency>
      <groupId>io.github.resilience4j</groupId>
      <artifactId>resilience4j-core</artifactId>
      <version>1.7.0</version>
    </dependency>

    <dependency>
      <groupId>io.github.resilience4j</groupId>
      <artifactId>resilience4j-all</artifactId>
      <version>1.7.0</version>
    </dependency>

I dont know how to solve the error. Maybe its the dependencies? I tried withouth the annotation, using

CircuitBreakerConfig config = CircuitBreakerConfig.ofDefaults()

But it gives the same error, "cannot be resolved".

1

There are 1 best solutions below

0
On

Based on the demo circuit breaker you dont need

<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-spring-boot2</artifactId>
    <version>1.7.0</version>
  </dependency>

Because you already have spring-cloud-starter-circuitbreaker-reactor-resilience4j. Use these imports may solve your issue

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext.rx</groupId>
            <artifactId>jersey-rx-client-rxjava</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext.rx</groupId>
            <artifactId>jersey-rx-client-rxjava2</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>io.projectreactor.addons</groupId>
            <artifactId>reactor-adapter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>