5

I am using Resilience4j Circuit breaker version: '1.4.0 with Spring boot version 2.0.6 and my problem is - fallback method is not working. the call is not going to the fallback method. Below is my code :

@Override
@CircuitBreaker(name="mainService",fallbackMethod = "callFallback")
public JSONObject callService(JSONObject rawRequest) throws TimeoutException {
      ...
       throw new TimeoutException("Time occured while calling 
       service");
      ...
}

-- and fallback method :

private JSONObject callFallback(JSONObject rawRequest,Throwable t){

    System.out.println("Inside fallback method callNsFallback, 
        cause"+t.toString());

        logger.info("Inside fallback method callFallback, 
        cause - {}",t.toString());

    return rawRequest;
}

--Configurations in application.yml

resilience4j:
circuitbreaker:
    configs:
    default:
        registerHealthIndicator: true
        ringBufferSizeInClosedState: 5
        ringBufferSizeInHalfOpenState: 3
        slidingWindowSize: 10
        minimumNumberOfCalls: 5
        permittedNumberOfCallsInHalfOpenState: 3
        automaticTransitionFromOpenToHalfOpenEnabled: true
        waitDurationInOpenState: 1s
        failureRateThreshold: 50
        eventConsumerBufferSize: 10
        recordExceptions:
        - java.util.concurrent.TimeoutException
    instances:
    mainService:
        baseConfig: default
vbvT
  • 71
  • 1
  • 8
  • Does it throw CallNotPermittedException without the fallback method? – Saif Jul 21 '20 at 21:36
  • Are you only capturing TimeoutException? – Saif Jul 21 '20 at 21:37
  • 2
    Thanks @SaifUrRahman for replying. Its didn't throw CallNotPermittedException and I was using only TimeoutException. The problem is resolved by adding 'spring- aop' dependancy with resilience4J – vbvT Jul 22 '20 at 05:16
  • 1
    Ok. Could you please share your pom.xml as an answer? – Saif Jul 22 '20 at 08:51

2 Answers2

2

The possible reasons could be you are missing some required dependencies. Here I am sharing the dependencies and the yaml configuration which were working for me.

pom.xml

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

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

application.yml

resilience4j:
  circuitbreaker:
    instances:
      menu-service:
        registerHealthIndicator: true
        eventConsumerBufferSize: 10
        automaticTransitionFromOpenToHalfOpenEnabled: true
        failureRateThreshold: 50
        minimumNumberOfCalls: 5
        permittedNumberOfCallsInHalfOpenState: 1
        slidingWindowSize: 10
        waitDurationInOpenState: 2s
        slidingWindowType: COUNT_BASED
        
        
management:
  health:
    circuitbreakers:
      enabled: true
  endpoints:
    web:
      exposure:
        include: health
  endpoint:
    health:
      show-details: always

1

I have been missing below dependency. It worked after adding and fallback method was called!

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
AddeusExMachina
  • 592
  • 1
  • 11
  • 22