Spring Boot Circuit Breaker is a fault-tolerance mechanism used to prevent cascading failures in a microservices architecture. It helps applications handle failures gracefully by detecting failures and stopping excessive requests to unhealthy services. This guide will walk you through implementing Circuit Breaker using Resilience4j in Spring Boot.
Written by
Şuayb Şimşek
Backend-focused fullstack developer sharing practical notes on Spring Boot, security, microservices, and cloud-native architecture.
🛠️ Step 3: Implement Circuit Breaker in a REST Service
In this section, we clarify Step 3: Implement Circuit Breaker in a REST Service and summarize the key points you will apply in implementation.
Create a Service to Call an External API
The following example gives practical context for Create a Service to Call an External API and can be applied directly.
🛠️ Step 4: Create a REST Controller
▶️ Running the Application
Start the application:
BASH
./mvnw spring-boot:run
or using Gradle:
BASH
gradle bootRun
🧪 Test the Circuit Breaker
Test the Circuit Breaker:
BASH
curl -X GET http://localhost:8080/api/data
🏁 Conclusion
You now have a practical Spring Boot Circuit Breaker implementation with a clear, production-friendly Spring Boot structure. As a next step, adapt configuration and tests to your own domain, then validate behavior under realistic traffic and failure scenarios.
JAVAExternalService.java
package com.example.circuitbreaker.service;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ExternalService {
private final RestTemplate restTemplate = new RestTemplate();
@CircuitBreaker(name = "externalService", fallbackMethod = "fallbackResponse")
public String callExternalAPI() {
return restTemplate.getForObject("http://unreliable-service/api/data", String.class);
}
public String fallbackResponse(Exception e) {
return "Fallback response: Service is unavailable!";
}
}
KOTLINExternalService.kt
package com.example.circuitbreaker.service
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker
import org.springframework.stereotype.Service
import org.springframework.web.client.RestTemplate
@Service
class ExternalService {
private val restTemplate = RestTemplate()
@CircuitBreaker(name = "externalService", fallbackMethod = "fallbackResponse")
fun callExternalAPI(): String {
return restTemplate.getForObject("http://unreliable-service/api/data", String::class.java) ?: ""
}
fun fallbackResponse(e: Exception): String {
return "Fallback response: Service is unavailable!"
}
}
JAVACircuitBreakerController.java
package com.example.circuitbreaker.controller;
import com.example.circuitbreaker.service.ExternalService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class CircuitBreakerController {
private final ExternalService externalService;
public CircuitBreakerController(ExternalService externalService) {
this.externalService = externalService;
}
@GetMapping("/data")
public String fetchData() {
return externalService.callExternalAPI();
}
}
KOTLINCircuitBreakerController.kt
package com.example.circuitbreaker.controller
import com.example.circuitbreaker.service.ExternalService
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("/api")
class CircuitBreakerController(private val externalService: ExternalService) {
@GetMapping("/data")
fun fetchData(): String {
return externalService.callExternalAPI()
}
}