Skip to content

A simple microservice-style application that uses Circuit Breaker to tackle timeout/availability issues. Also looks into ways to implement the test pyramid using Spring's test support.

Notifications You must be signed in to change notification settings

MrMikeFloyd/resilient-example-spring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Resilience in Spring with the Circuit Breaker pattern

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

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 id
  • localhost:8080/recommender/<locationId>/outfit For outfit recommendations for a given location id

FailingApp

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.

How to run

  1. Clone this repository
  2. Start failingApp (cd failingApp && mvn spring-boot:run)
  3. Start resilientApp (cd resilientApp && mvn spring-boot:run)
  4. Perform calls against resilientApp (curl localhost:8080/recommender/123/outfit)
  5. Access Hystrix Dashboard to observe the status of the circuits.
  6. To perform a number of concurrent requests, run ./perform_GETs.sh.

Further reading

  • See my blog post on the Circuit Breaker pattern here.
  • More on Hystrix here.

About

A simple microservice-style application that uses Circuit Breaker to tackle timeout/availability issues. Also looks into ways to implement the test pyramid using Spring's test support.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published