This is a simple mini-microservice example that showcases the Circuit Breaker pattern using Spring Boot & Spring Cloud Netflix Hystrix.
A sample app, resilientApp
, will use Circuit Breaker interacting with failingApp
. resilientApp
has a Hystrix Dashboard running, so the current state of the Circuit Breaker can be observed easily. resilientApp
furthermore contains a few tests to demonstrate Springs support for implementing Integration and API tests.
Note: Please bear in mind that Hystrix is no longer being maintained by Netflix. For the sake of simplicity and ease of obervability through the Hystrix dashboard, I nonetheless decided to use Hystrix for this sample. If you plan on using Circuit Breaker in production, please consider an actively maintained library providing implementations, such as resilience4J or Sentinel.
resilientApp
gives callers recommendations on which places to visit (according to their popularity) and what to wear in these places (according to their outside temperature). For making recommendations, resilientApp
depends on another application, failingApp
, which is likely to fail.
In order to handle failing calls, Hystrix Circuit Breaker is used, using fallback methods to give callers a timely response.
resilientApp
exposes 2 endpoints:
localhost:8080/recommender/<locationId>/visit
For advice on whether or not to visit a given location idlocalhost:8080/recommender/<locationId>/outfit
For outfit recommendations for a given location id
failingApp
exposes 2 endpoints, one for a location's popularity score, and one for a location's most recent temperature reading. Both are consumed by resilientApp
:
localhost:8081/locations/<locationId>/popularity
For popularity scores (fairly stable)localhost:8081/locations/<locationId>/temperature
For temperature readings (unstable)
The application uses Springdoc's OpenAPI UI, so the endpoints can be explored and tested using the Swagger UI. While the popularity score endpoint is fairly stable, the resource providing temperature readings fails faily often thanks to some serious monkey business:
- ChaosMonkey: For 50% of all calls throws a
RuntimeException
, otherwise does nothing. - LatencyMonkey: For 50% of all calls waits for 15 seconds, otherwise does nothing.
- Clone this repository
- Start
failingApp
(cd failingApp && mvn spring-boot:run
) - Start
resilientApp
(cd resilientApp && mvn spring-boot:run
) - Perform calls against
resilientApp
(curl localhost:8080/recommender/123/outfit
) - Access Hystrix Dashboard to observe the status of the circuits.
- To perform a number of concurrent requests, run
./perform_GETs.sh
.