I am using both @CircuitBreaker
and @Retry
annotations on a service method. When I apply both the configurations, the retry configurations are not taking affect.
Below is the configuration:
resilience4j:
circuitbreaker:
instances:
inventorymicroservice:
registerHealthIndicator: true
ringBufferSizeInClosedState: 5
ringBufferSizeInHalfOpenState: 3
waitDurationInOpenState: 30000
failureRateThreshold: 50
slowCallRateThreshold: 50
recordExceptions:
- java.io.IOException
- java.util.concurrent.TimeoutException
- java.net.ConnectException
- org.springframework.web.reactive.function.client.WebClientRequestException
retry:
instances:
retryConfig:
maxAttempts: 3
waitDuration: 10s
enableExponentialBackoff: true
exponentialBackoffMultiplier: 2
retryExceptions:
- org.springframework.web.client.HttpServerErrorException
- java.io.IOException
- java.io.IOException
- java.util.concurrent.TimeoutException
- java.net.ConnectException
- org.springframework.web.reactive.function.client.WebClientRequestException
Service method:
@CircuitBreaker(name = "inventorymicroservice", fallbackMethod = "fallBack")
@Retry(name = "retryConfig", fallbackMethod = "fallBack")
public Order saveOrder(Order order){
Order savedOrder = this.orderRepository.save(order);
log.info("Calling the inventory service to update the quantity :: ");
//ResponseEntity<Integer> integerResponseEntity = this.restTemplate.postForEntity("http://localhost:9222/api/inventory", null, Integer.class);
Integer response = this.webClient
.post()
.uri("/api/inventory")
.retrieve()
.bodyToMono(Integer.class)
.block();
log.info("Response from the inventory microservice :: {}", response);
return savedOrder;
}
private Order fallBack(Throwable exception){
log.error("Exception while invoking the REST endpoint :: ", exception.getMessage());
return Order.builder().build();
}
Where am I going wrong? Also, how to convert this configuration to programmatic configuration using functional programming.
The default Resilience4j aspect order is
Your CircuitBreaker has a fallback, so it never throws an exception, so Retry never sees a failed invocation to retry.
Remove the Retry fallback, and change the aspect order so CircuitBreaker does its work after Retry.