Service Resilience using Resilience4j

Service Resilience using Resilience4j Circuit Breaker – Microservices Essentials

In a microservices environment, a single failing service can cascade and cause other services to fail. To prevent this, we use Resilience4j to implement Circuit Breaker patterns that provide graceful degradation and stability. Service Resilience — ensuring that your system remains responsive and fault-tolerant under failure conditions

In this tutorial,

Build two Spring Boot microservices: UserService and ProductService

Add Circuit Breaker (Resilience4j) for service resilience (no Eureka involved)

✅ Prerequisites:

  • Java 17 installed
  • Maven installed
  • IDE (IntelliJ / VS Code / Eclipse)
  • Postman or browser to test API

2.1 Create Spring Boot Project

Use Spring Initializr:

  • Project: Maven
  • Language: Java
  • Spring Boot: 3.1.8
  • Group: com.example
  • Artifact: product-service
  • Dependencies:
    • Spring Web
    • Spring Boot Actuator

Click Generate, extract ZIP, and open in IDE.

2.3 ProductController.java

package com.example.productservice.controller;

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

@RestController
public class ProductController {
@GetMapping("/products")
public String getProducts() {
return "List of products from PRODUCT-SERVICE";
}
}

2.4 application.properties

server.port=8082
spring.application.name=product-service

3.1 Create Another Spring Boot Project

Use Spring Initializr:

  • Artifact: user-service
  • Dependencies:
    • Spring Web
    • Spring Boot Actuator
    • Resilience4j
    • Spring Boot Starter AOP (required by Resilience4j)

3.2 pom.xml for user-service

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>

3.3 UserController.java

package com.example.userservice.controller;

import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class UserController {

private final RestTemplate restTemplate;

@Value("${product.service.url}")
private String productServiceUrl;

public UserController() {
this.restTemplate = new RestTemplate();
}

@GetMapping("/users")
@CircuitBreaker(name = "productServiceBreaker", fallbackMethod = "productFallback")
public String getUsers() {
String productResponse = restTemplate.getForObject(productServiceUrl + "/products", String.class);
return "User Data + " + productResponse;
}

public String productFallback(Throwable t) {
return "Product Service is down. Please try again later.";
}
}

3.4 application.properties

server.port=8081
spring.application.name=user-service
product.service.url=http://localhost:8082
  1. Start product-service (port 8082)
  2. Start user-service (port 8081)
  3. Call: http://localhost:8081/users
    → You should see User Data + List of products from PRODUCT-SERVICE
  1. Stop product-service
  2. Call: http://localhost:8081/users
    → You should now see:
    "Product Service is down. Please try again later."

Add to application.properties:

management.endpoints.web.exposure.include=*

You can then check:

http://localhost:8081/actuator/circuitbreakers
http://localhost:8081/actuator/health

Github code : https://github.com/infotechseo/resilience4j-circuit-breaker

Similar Posts