Chapter 1. Getting started with Fuse on Spring Boot
To develop Fuse applications on Spring Boot, get started by generating and building a Fuse sample booster project that runs on Spring Boot. The following topics provide details:
1.1. About Fuse on Spring Boot
Spring Boot is an evolution of the well-known Spring container. A distinctive quality of the Spring Boot container is that container functionality is divided up into small chunks, which can be deployed independently. This enables you to deploy a container with a small footprint, specialized for a particular kind of service, and this happens to be exactly what you need to fit the paradigm of a microservices architecture.
Distinctive features of this container technology are:
- Particularly suited to running on a scalable cloud platform (Kubernetes and OpenShift).
- Small footprint (ideal for microservices architecture).
- Optimized for convention over configuration.
- No application server required. You can run a Spring Boot application Jar directly in a JVM.
1.2. Generating your booster project
Fuse booster projects exist to help developers get started with running standalone applications. The instructions provided here guide you through generating one of those booster projects, the Circuit Breaker booster. This exercise demonstrates useful components of the Fuse on Spring Boot.
The Netflix/Hystrix circuit breaker enables distributed applications to handle interruptions to network connectivity and temporary unavailability of backend services. The basic idea of the circuit breaker pattern is that the loss of a dependent service is detected automatically and an alternative behavior can be programmed, in case the backend service is temporarily unavailable.
The Fuse circuit breaker booster consists of two related services:
-
A
name
service, the backend service that returns a name to greet. -
A
greetings
service, the frontend service that invokes thename
service to get a name and then returns the string,Hello, NAME
.
In this booster demonstration, the Hystrix circuit breaker is inserted between the greetings
service and the name
service. If the backend name
service becomes unavailable, the greetings
service can fall back to an alternative behavior and respond to the client immediately, instead of being blocked while it waits for the name
service to restart.
Prerequisites
- You must have access to the Red Hat Developer Platform.
- You must have a supported version of the Java Developer Kit (JDK). See the Supported Configurations page for details.
- You must have Apache Maven 3.3.x or later.
Procedure
- Navigate to https://developers.redhat.com/launch.
Click START.
The launcher wizard prompts you to log in to your Red Hat account.
- Click the Log in or register button and then log in.
- On the Launcher page, click the Deploy an Example Application button.
- On the Create Example Application page, type the name, fuse-circuit-breaker, in the Create Example Application as field.
- Click Select an Example.
In the Example dialog, select the Circuit Breaker option. A Select a Runtime dropdown menu appears.
- From the Select a Runtime dropdown, select Fuse.
-
From the version dropdown menu, select 7.6 (Red Hat Fuse) (do not select the
2.21.2 (Community)
version). - Click Save.
- On the Create Example Application page, click Download.
-
When you see the Your Application is Ready dialog, click
Download.zip
. Your browser downloads the generated booster project (packaged as a ZIP file). - Use an archive utility to extract the generated project to a convenient location on your local file system.
1.3. Building your booster project
These instructions guide you through building the Circuir Breaker booster with Fuse on Spring Boot.
Prerequisites
- You must have generated and downloaded your booster project via the Red Hat Developer Portal.
- You must have a supported version of the Java Developer Kit (JDK). See the Supported Configurations page for details.
- You must have Apache Maven 3.3.x or later.
Procedure
Open a shell prompt and build the project from the command line, using Maven:
cd fuse-circuit-breaker
mvn clean package
After Maven builds the project, it displays a Build Success message.
Open a new shell prompt and start the name service, as follows:
cd name-service
mvn spring-boot:run -DskipTests -Dserver.port=8081
As Spring Boot starts up, you should see output similar to the following:
... 2019-05-06 20:19:59.401 INFO 9553 --- [ main] o.a.camel.spring.SpringCamelContext : Route: route1 started and consuming from: servlet:/name?httpMethodRestrict=GET 2019-05-06 20:19:59.402 INFO 9553 --- [ main] o.a.camel.spring.SpringCamelContext : Total 1 routes, of which 1 are started 2019-05-06 20:19:59.403 INFO 9553 --- [ main] o.a.camel.spring.SpringCamelContext : Apache Camel 2.21.0.fuse-730078-redhat-00001 (CamelContext: camel-1) started in 0.287 seconds 2019-05-06 20:19:59.406 INFO 9553 --- [ main] o.a.c.c.s.CamelHttpTransportServlet : Initialized CamelHttpTransportServlet[name=CamelServlet, contextPath=] 2019-05-06 20:19:59.473 INFO 9553 --- [ main] b.c.e.u.UndertowEmbeddedServletContainer : Undertow started on port(s) 8081 (http) 2019-05-06 20:19:59.479 INFO 9553 --- [ main] com.redhat.fuse.boosters.cb.Application : Started Application in 5.485 seconds (JVM running for 9.841)
Open a new shell prompt and start the greetings service, as follows:
cd greetings-service
mvn spring-boot:run -DskipTests
As Spring Boot starts up, you should see output similar to the following:
... 2019-05-06 20:22:19.051 INFO 9729 --- [ main] o.a.c.c.s.CamelHttpTransportServlet : Initialized CamelHttpTransportServlet[name=CamelServlet, contextPath=] 2019-05-06 20:22:19.115 INFO 9729 --- [ main] b.c.e.u.UndertowEmbeddedServletContainer : Undertow started on port(s) 8080 (http) 2019-05-06 20:22:19.123 INFO 9729 --- [ main] com.redhat.fuse.boosters.cb.Application : Started Application in 7.68 seconds (JVM running for 12.66)
The greetings service exposes a REST endpoint at the
http://localhost:8080/camel/greetings
URL.Invoke the REST endpoint by either opening the URL in a web browser or by opening another shell prompt and typing the following
curl
command:curl http://localhost:8080/camel/greetings
Here is the response:
{"greetings":"Hello, Jacopo"}
To demonstrate the circuit breaker functionality provided by Camel Hystrix, kill the backend name service by typing Ctrl-C in the shell prompt window where the name service is running.
Now that the name service is unavailable, the circuit breaker kicks in to prevent the greetings service from hanging when it is invoked.
Invoke the greetings REST endpoint by either opening
http://localhost:8080/camel/greetings
in a web browser or by typing the followingcurl
command in another shell prompt window:curl http://localhost:8080/camel/greetings
Here is the response:
{"greetings":"Hello, default fallback"}
In the window where the greetings service is running, the log shows the following sequence of messages:
2019-05-06 20:24:16.952 INFO 9729 --- [-CamelHystrix-2] route2 : Try to call name Service 2019-05-06 20:24:16.956 INFO 9729 --- [-CamelHystrix-2] o.a.c.httpclient.HttpMethodDirector : I/O exception (java.net.ConnectException) caught when processing request: Connection refused (Connection refused) 2019-05-06 20:24:16.956 INFO 9729 --- [-CamelHystrix-2] o.a.c.httpclient.HttpMethodDirector : Retrying request 2019-05-06 20:24:16.957 INFO 9729 --- [-CamelHystrix-2] o.a.c.httpclient.HttpMethodDirector : I/O exception (java.net.ConnectException) caught when processing request: Connection refused (Connection refused) 2019-05-06 20:24:16.957 INFO 9729 --- [-CamelHystrix-2] o.a.c.httpclient.HttpMethodDirector : Retrying request 2019-05-06 20:24:16.957 INFO 9729 --- [-CamelHystrix-2] o.a.c.httpclient.HttpMethodDirector : I/O exception (java.net.ConnectException) caught when processing request: Connection refused (Connection refused) 2019-05-06 20:24:16.957 INFO 9729 --- [-CamelHystrix-2] o.a.c.httpclient.HttpMethodDirector : Retrying request 2019-05-06 20:24:16.964 INFO 9729 --- [-CamelHystrix-2] route2 : We are falling back!!!!
-
For more information about this example, open the Circuit Breaker - Red Hat Fuse page at http://localhost:8080/ (while the
greetings-service
is running). This page includes a link to the Hystrix dashboard that monitors the state of the circuit breaker.