Resilience4j in Spring Boot

Spring Boot + Resilience4j is a popular combo for building fault-tolerant, resilient microservices.
Resilience4j provides resilience patterns like:

  • CircuitBreaker – prevents cascading failures by stopping calls when downstream is unhealthy.
  • Retry – automatically retries failed calls.
  • RateLimiter – limits number of requests.
  • Bulkhead – limits concurrent calls.
  • TimeLimiter – sets timeouts for calls.

✅ Step 1: Add Dependencies

In your pom.xml (Maven):

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

    <!-- Resilience4j -->
    <dependency>
        <groupId>io.github.resilience4j</groupId>
        <artifactId>resilience4j-spring-boot2</artifactId>
        <version>2.2.0</version>
    </dependency>

    <!-- For circuit breaker metrics (optional with Actuator) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

✅ Step 2: Configure Resilience4j in application.yml

resilience4j:
  circuitbreaker:
    instances:
      myServiceCB:
        registerHealthIndicator: true
        slidingWindowSize: 5
        failureRateThreshold: 50
        waitDurationInOpenState: 10s
        permittedNumberOfCallsInHalfOpenState: 3
  retry:
    instances:
      myServiceRetry:
        maxAttempts: 3
        waitDuration: 2s

✅ Step 3: Create a Service with CircuitBreaker + Retry

import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.retry.annotation.Retry;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class MyService {

    private final RestTemplate restTemplate = new RestTemplate();
    private static final String SERVICE_NAME = "myServiceCB";

    @CircuitBreaker(name = SERVICE_NAME, fallbackMethod = "fallbackResponse")
    @Retry(name = "myServiceRetry")
    public String callExternalService() {
        String url = "http://localhost:8081/external/api";
        return restTemplate.getForObject(url, String.class);
    }

    // Fallback method
    public String fallbackResponse(Throwable t) {
        return "External service unavailable. Please try later.";
    }
}

✅ Step 4: Expose an API Endpoint

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    private final MyService myService;

    public MyController(MyService myService) {
        this.myService = myService;
    }

    @GetMapping("/test")
    public String test() {
        return myService.callExternalService();
    }
}

✅ Step 5: Run and Test

  1. Start your Spring Boot app.
  2. Call /test.
  3. If the external service is up → returns response.
  4. If down → it will retry 3 times, then trigger the fallback.

Leave a Reply