Chapter 2. Getting Started with Spring Boot

2.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 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.

2.2. Prerequisites

To build and run the booster demonstration, install the following prerequisites:

2.3. Generate the booster project

To generate the circuit breaker booster project, perform the following steps:

  1. Navigate to https://developers.redhat.com/launch.
  2. Click LAUNCH YOUR PROJECT.
  3. The launcher wizard prompts you to log in to your Red Hat account. Click the Log in or register button to log in.
  4. On the Create Application page, enter the name, fuse-circuit-breaker, and click Continue.
  5. At the Select Target Environment step, select Build and Run Locally and click the double down arrow, double down arrow to advance to the next step.
  6. At the Select Mision and Runtime step, select the mission and runtime, as follows:

    1. In the Select a Runtime column, click Fuse to select the Fuse runtime.
    2. From the dropdown menu in the Fuse runtime cell, select the latest available 7.x.x (Red Hat Fuse) version (do not select the 2.21.2 (Community) version).
    3. In the Select a Mission column, click Circuit Breaker to select the circuit breaker booster project.
    4. Click the double down arrow, double down arrow to advance to the next step.
  7. At the Confirm Application Summary and Setup step, click Set Up Application to generate the source code for the circuit breaker booster.
  8. Your browser now commences downloading the generated booster project (packaged as a ZIP file).
  9. After downloading the ZIP file, use an archive utility to extract the generated project to a convenient location on your local filesystem.

2.4. Build and run the booster

To build and run the booster project, perform the following steps:

  1. Open a shell prompt and build the project from the command line, using Maven:

    cd fuse-circuit-breaker
    mvn clean package
  2. 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)
  3. 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)
  4. 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 the curl command, as follows:

    $ curl http://localhost:8080/camel/greetings
    {"greetings":"Hello, Jacopo"}
  5. 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.
  6. 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!!!!
  7. 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.