You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This guide walks you through the process of creating a Spring application.
10
+
:project_id: gs-spring-cloud-circuitbreaker
11
+
This guide walks you through the process of applying circuit breakers to potentially-failing method calls using Spring Cloud Circuit Breaker.
12
12
13
13
== What you'll build
14
14
15
-
You'll build a Spring application.
15
+
You'll build a microservice application that uses the http://martinfowler.com/bliki/CircuitBreaker.html[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.
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.
36
90
37
-
Create a new controller for your Spring application:
91
+
Edit our main class, in `BookstoreApplication.java`. It should look like this:
NOTE: The above example does not specify `GET` vs. `PUT`, `POST`, and so forth, because `@RequestMapping` maps all HTTP operations by default. Use `@RequestMapping(method=GET)` to narrow this mapping.
99
+
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.
46
100
101
+
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.
Although it is possible to package this service as a traditional link:/understanding/WAR[WAR] file for deployment to an external application server, the simpler approach demonstrated below creates a standalone application. You package everything in a single, executable JAR file, driven by a good old Java `main()` method. Along the way, you use Spring's support for embedding the link:/understanding/Tomcat[Tomcat] servlet container as the HTTP runtime, instead of deploying to an external instance.
109
+
== Set up a client microservice application
51
110
111
+
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.
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 https://spring.io/guides/gs/reactive-rest-service/[Building a Reactive RESTful Web Service] guide.)
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.
154
+
155
+
== Apply The Circuit Breaker Pattern
156
+
Spring Cloud's Circuit Breaker library provides an implementation of the Circuit Breaker pattern:
157
+
when we wrap a method call in a circuit breaker, Spring Cloud Circuit Breaker watches for failing
158
+
calls to that method, and if failures build up to a threshold, Spring Cloud Circuit Breaker opens
159
+
the circuit so that subsequent calls automatically fail. While the circuit is open, Spring Cloud
160
+
Circuit Breaker redirects calls to the method, and they’re passed on to our specified fallback
161
+
method.
162
+
163
+
Spring Cloud Circuit Breaker supports many different circuit breaker implementations including,
164
+
Resilience4J, Hystrix, Sentinal, and Spring Retry. In this guide we will use the Resilience4J
165
+
implementation. To use this implementation we just need to add `spring-cloud-starter-circuitbreaker-reactor-resilience4j`
166
+
to our application's classpath.
167
+
168
+
`reading/pom.xml`
169
+
[source,xml]
170
+
----
171
+
include::complete/reading/pom.xml[]
172
+
----
173
+
174
+
`reading/build.gradle`
175
+
[source,groovy]
176
+
----
177
+
include::complete/reading/build.gradle[]
178
+
----
179
+
180
+
Spring Cloud Circuit Breaker provides an interface called `ReactiveCircuitBreakerFactory` which
181
+
we can use to create new circuit breakers for our application. An implementation of this interface
182
+
will be auto-configured based on the starter that is on your application's classpath. Lets create
183
+
a new service that uses this interface to make API calls to the Bookstore application
Logging output is displayed. The service should be up and running within a few seconds.
206
+
== Try it out
67
207
208
+
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:
68
209
69
-
== Test the application
210
+
----
211
+
Spring in Action (Manning), Cloud Native Java (O'Reilly), Learning Spring Boot (Packt)
212
+
----
70
213
71
-
Now that the applicationis running, you can test it.
214
+
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:
72
215
216
+
----
217
+
Cloud Native Java (O'Reilly)
218
+
----
73
219
74
220
== Summary
75
221
76
-
Congratulations! You've just developed a Spring application!
222
+
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.
0 commit comments