-
Language:
English
-
Language:
English
Red Hat Training
A Red Hat training course is available for Red Hat Fuse
Chapter 1. Getting Started with Spring Boot
1.1. Overview of the circuit breaker booster
The Netflix/Hystrix circuit breaker component enables distributed applications to cope with 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 JBoss Fuse circuit breaker booster consists of two related services:
- A name service, which returns a name to greet, and
-
A greetings service, which invokes the name service to get a name and then returns the string,
Hello, NAME
.
In this demonstration, the Hystrix circuit breaker is inserted between the greetings service and the name service. If the name service becomes unavailable, the greetings service can fall back to an alternative behavior and respond to the client immediately, instead of blocking while it waits for the name service to restart.
1.2. Prerequisites
To build and run the booster demonstration, install the following prerequisites:
- A supported version of the Java Developer Kit (JDK). See the Supported Configurations page for details.
- Apache Maven 3.3.x or later. See the Maven Download page.
1.3. Generate the booster project
To generate the circuit breaker booster project, perform the following steps:
- Navigate to https://developers.redhat.com/launch.
- Click LAUNCH YOUR PROJECT.
- The launcher wizard prompts you to log in to your Red Hat account. Click the Log in or register button to log in.
- On the Deployment type page, click I will build and run locally.
- On the Mission page, select the Circuit Breaker mission and click Next.
- On the Runtime page, select the Fuse runtime and click Next.
- On the Project Info page, accept the default settings for the Runtime Version, Group Id, Artifact Id, and Version.
- Click Next.
- On the Review Summary page, click Download as ZIP File to download the generated project.
- After downloading the ZIP file, use an archive utility to extract the generated project to a convenient location on your local filesystem.
1.4. Build and run the booster
To build and run the booster project, perform the following steps:
Open a shell prompt and build the project from the command line, using Maven:
cd booster-circuit-breaker-spring-boot mvn clean package
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 some output like the following:
... 2017-12-08 15:44:24.223 INFO 22758 --- [ main] o.a.camel.spring.SpringCamelContext : Total 1 routes, of which 1 are started 2017-12-08 15:44:24.227 INFO 22758 --- [ main] o.a.camel.spring.SpringCamelContext : Apache Camel 2.20.0 (CamelContext: camel-1) started in 0.776 seconds 2017-12-08 15:44:24.234 INFO 22758 --- [ main] org.jboss.fuse.boosters.cb.Application : Started Application in 4.137 seconds (JVM running for 4.744)
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 some output like the following:
... 2017-12-08 15:46:58.521 INFO 22887 --- [ main] o.a.c.c.s.CamelHttpTransportServlet : Initialized CamelHttpTransportServlet[name=CamelServlet, contextPath=] 2017-12-08 15:46:58.524 INFO 22887 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2017-12-08 15:46:58.536 INFO 22887 --- [ main] org.jboss.fuse.boosters.cb.Application : Started Application in 6.263 seconds (JVM running for 6.819)
The greetings service exposes a REST endpoint at the URL,
http://localhost:8080/camel/greetings
. You can invoke the REST endpoint either from a Web browser or from a shell prompt, using thecurl
command, as follows:$ curl http://localhost:8080/camel/greetings {"greetings":"Hello, Jacopo"}
- To demonstrate the circuit breaker functionality provided by Camel Hystrix, kill the backend name service by typing Ctrl-C in the window of the shell prompt 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 using the
curl
command, as follows:$ curl http://localhost:8080/camel/greetings {"greetings":"Hello, default fallback"}
The log in the window where the greetings service is running shows the following sequence of messages:
2017-12-08 16:38:30.483 INFO 23579 --- [-CamelHystrix-2] route2 : Try to call name Service 2017-12-08 16:38:30.488 INFO 23579 --- [-CamelHystrix-2] o.a.c.httpclient.HttpMethodDirector : I/O exception (java.net.ConnectException) caught when processing request: Connection refused (Connection refused) 2017-12-08 16:38:30.488 INFO 23579 --- [-CamelHystrix-2] o.a.c.httpclient.HttpMethodDirector : Retrying request 2017-12-08 16:38:30.489 INFO 23579 --- [-CamelHystrix-2] o.a.c.httpclient.HttpMethodDirector : I/O exception (java.net.ConnectException) caught when processing request: Connection refused (Connection refused) 2017-12-08 16:38:30.489 INFO 23579 --- [-CamelHystrix-2] o.a.c.httpclient.HttpMethodDirector : Retrying request 2017-12-08 16:38:30.489 INFO 23579 --- [-CamelHystrix-2] o.a.c.httpclient.HttpMethodDirector : I/O exception (java.net.ConnectException) caught when processing request: Connection refused (Connection refused) 2017-12-08 16:38:30.489 INFO 23579 --- [-CamelHystrix-2] o.a.c.httpclient.HttpMethodDirector : Retrying request 2017-12-08 16:38:30.495 INFO 23579 --- [-CamelHystrix-2] route2 : We are falling back!!!!
-
For more information about this example, visit the Circuit Breaker Mission page at http://localhost:8080/ (while the
greetings-service
is running). This page provides a link to the Hystrix dashboard, which monitors the state of the circuit breaker.