For complete understanding of Resilience4j Circuit Breaker and how we can integrate it inside the Spring Boot application you can checkout our blog.
Blog Link: Resilience4j Circuit Breaker: Ensuring System Stability
A simple app demonstrating how we can implement circuit breaker pattern using Resilience4j in Spring Boot
This is a simple app wherein we are fetching the movie details based on the movie id. The movie details are fetched from external service that is called using the Spring Rest Template. For simplicity, we have created a mock controller which acts as a external service for returning the movie details.
We have created a single controller endpoint which accepts movie id as path parameter and query parameter circuitBreakerType which accepts predefined set of values to mimic the different circuit breaker examples.
a. 1 - Mock controller returns valid movie information
b. 2 - Mock controller returns valid movie information but with a delay
c. 3 - Mock controller returns HTTP status code 404
d. 4 or any other numeric value - Mock controller returns null which leads to MovieNotFound Exception
Different circuit breaker instances are defined inside the application.yml. To mimic different circuit breaker scenarios use:
a. count-based-circuit-breaker: countBasedCircuitBreaker circuit breaker instance will be triggered
b. time-based-circuit-breaker: timeBasedCircuitBreaker circuit breaker instance will be triggered.
c. circuit-breaker-on-exception: circuitBreakerOnException circuit breaker instance will be triggered.
d. circuit-breaker-with-record-failure-predicate: circuitBreakerWithRecordFailurePredicate circuit breaker instance will be triggered.
e. circuit-breaker-with-ignore-exception-predicate: circuitBreakerWithIgnoreExceptionPredicate circuit breaker instance will be triggered.
f. circuit-breaker-for-slow-calls: circuitBreakerForSlowCalls circuit breaker instance will be triggered.
g. circuit-breaker-with-fallback: countBasedCircuitBreaker circuit breaker instance will be triggered and fallback method logic will be executed in this case.
h. custom-circuit-breaker: customCircuitBreaker circuit breaker instance defined in CircuitBreakerConfiguration class will be triggered.
Check the application logs in order to get the better understanding of different circuit breaker scenarios.
curl 'http://localhost:8080/movies/3?circuitBreakerType=count-based-circuit-breaker'
curl 'http://localhost:8080/movies/3?circuitBreakerType=time-based-circuit-breaker'
curl 'http://localhost:8080/movies/3?circuitBreakerType=circuit-breaker-on-exception'
curl 'http://localhost:8080/movies/4?circuitBreakerType=circuit-breaker-with-record-failure-predicate'
curl 'http://localhost:8080/movies/4?circuitBreakerType=circuit-breaker-with-ignore-exception-predicate'
curl 'http://localhost:8080/movies/2?circuitBreakerType=circuit-breaker-for-slow-calls'
curl 'http://localhost:8080/movies/3?circuitBreakerType=circuit-breaker-with-fallback'
curl 'http://localhost:8080/movies/3?circuitBreakerType=custom-circuit-breaker'
Additionally, the Postman collection is available under the resources folder.
[src > main > resources > postman > Spring-Boot-Resilience4j-Circuit-Breaker.postman_collection.json]