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)
🛠️ Step 1: Setup Prerequisites
✅ Prerequisites:
- Java 17 installed
- Maven installed
- IDE (IntelliJ / VS Code / Eclipse)
- Postman or browser to test API
📁 Step 2: Create product-service Microservice
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
📁 Step 3: Create user-service Microservice
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
Step 4: Test Without Circuit Breaker
- Start
product-service(port 8082) - Start
user-service(port 8081) - Call:
http://localhost:8081/users
→ You should seeUser Data + List of products from PRODUCT-SERVICE
Step 5: Test Circuit Breaker
- Stop
product-service - Call:
http://localhost:8081/users
→ You should now see:"Product Service is down. Please try again later."
Step 6: Enable Actuator for Circuit Breaker Monitoring (Optional)
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
