This guide walks you through the process of applying circuit breakers to potentially-failing method calls using Spring Cloud Circuit Breaker.
You’ll build a microservice application that uses the Circuit Breaker pattern to gracefully degrade functionality when a method call fails. Use of the Circuit Breaker pattern can allow a microservice to continue operating when a related service fails, preventing the failure from cascading and giving the failing service time to recover.
bookstore/build.gradle
link:https://raw.githubusercontent.com/spring-guides/gs-spring-cloud-circuitbreaker/main/initial/bookstore/build.gradle[role=include]
reading/build.gradle
link:https://raw.githubusercontent.com/spring-guides/gs-spring-cloud-circuitbreaker/main/initial/reading/build.gradle[role=include]
bookstore/pom.xml
link:https://raw.githubusercontent.com/spring-guides/gs-spring-cloud-circuitbreaker/main/initial/bookstore/pom.xml[role=include]
reading/pom.xml
link:https://raw.githubusercontent.com/spring-guides/gs-spring-cloud-circuitbreaker/main/initial/reading/pom.xml[role=include]
The Bookstore service will have a single endpoint. It will be accessible at /recommended
, and will (for simplicity) return a Mono
of String
recommended reading list.
Edit our main class, in BookstoreApplication.java
. It should look like this:
bookstore/src/main/java/hello/BookstoreApplication.java
link:complete/bookstore/src/main/java/hello/BookstoreApplication.java[role=include]
The @RestController
annotation marks BookstoreApplication
as a controller class, like @Controller
does, and also ensures that @RequestMapping
methods in this class will behave as though annotated with @ResponseBody
. That is, the return values of @RequestMapping
methods in this class will be automatically converted appropriately from their original types and will be written directly to the response body.
We’re going to run this application locally alongside a client service application, so in src/main/resources/application.properties
, set server.port
so that the Bookstore service won’t conflict with the client when we get that running.
bookstore/src/main/resources/application.properties
link:complete/bookstore/src/main/resources/application.properties[role=include]
The Reading application will be our front-end (as it were) to the Bookstore application. We’ll be able to view our reading list there at /to-read
, and that reading list will be retrieved from the Bookstore service application.
reading/src/main/java/hello/ReadingApplication.java
package hello;
import reactor.core.publisher.Mono;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.reactive.function.client.WebClient;
@RestController
@SpringBootApplication
public class ReadingApplication {
@RequestMapping("/to-read")
public Mono<String> toRead() {
return WebClient.builder().build()
.get().uri("http://localhost:8090/recommended").retrieve()
.bodyToMono(String.class);
}
public static void main(String[] args) {
SpringApplication.run(ReadingApplication.class, args);
}
}
To get the list from Bookstore, we’re using Spring’s WebClient
class. WebClient
makes an HTTP GET request to the Bookstore service’s URL as we provide it and then returns the result as a Mono
of String
. (For more information on using Spring to consume a RESTful service using WebClient
, see the Building a Reactive RESTful Web Service guide.)
Add the server.port
property to src/main/resources/application.properties
:
reading/src/main/resources/application.properties
link:complete/reading/src/main/resources/application.properties[role=include]
We now can access, in a browser, the /to-read
endpoint on our Reading application, and see our reading list. Yet since we rely on the Bookstore application, if anything happens to it, or if Reading is simply unable to access Bookstore, we’ll have no list and our users will get a nasty HTTP 500
error message.
Spring Cloud’s Circuit Breaker library provides an implementation of the Circuit Breaker pattern: when we wrap a method call in a circuit breaker, Spring Cloud Circuit Breaker watches for failing calls to that method, and if failures build up to a threshold, Spring Cloud Circuit Breaker opens the circuit so that subsequent calls automatically fail. While the circuit is open, Spring Cloud Circuit Breaker redirects calls to the method, and they’re passed on to our specified fallback method.
Spring Cloud Circuit Breaker supports many different circuit breaker implementations including,
Resilience4J, Hystrix, Sentinal, and Spring Retry. In this guide we will use the Resilience4J
implementation. To use this implementation we just need to add spring-cloud-starter-circuitbreaker-reactor-resilience4j
to our application’s classpath.
reading/pom.xml
link:complete/reading/pom.xml[role=include]
reading/build.gradle
link:complete/reading/build.gradle[role=include]
Spring Cloud Circuit Breaker provides an interface called ReactiveCircuitBreakerFactory
which
we can use to create new circuit breakers for our application. An implementation of this interface
will be auto-configured based on the starter that is on your application’s classpath. Lets create
a new service that uses this interface to make API calls to the Bookstore application
reading/src/main/java/hello/BookService.java
link:complete/reading/src/main/java/hello/BookService.java[role=include]
The ReactiveCircuitBreakerFactory
has a single method called create
we can use to create new circuit
breakers. Once we have our circuit breaker all we have
to do is call run
. Run takes a Mono
or Flux
and an optional
Function
. The optional Function
parameter acts as our fallback if anything goes wrong. In our
sample here the fallback will just return a Mono
containing the String
Cloud Native Java (O’Reilly)
.
With our new service in place, we can update the code in ReadingApplication
to use this new service.
reading/src/main/java/hello/ReadingApplication.java
link:complete/reading/src/main/java/hello/ReadingApplication.java[role=include]
Run both the Bookstore service and the Reading service, and then open a browser to the Reading service, at localhost:8080/to-read
. You should see the complete recommended reading list:
Spring in Action (Manning), Cloud Native Java (O'Reilly), Learning Spring Boot (Packt)
Now shut down the Bookstore application. Our list source is gone, but thanks to Hystrix and Spring Cloud Netflix, we have a reliable abbreviated list to stand in the gap; you should see:
Cloud Native Java (O'Reilly)
Congratulations! You’ve just developed a Spring application that uses the Circuit Breaker pattern to protect against cascading failures and to provide fallback behavior for potentially failing calls.