Este contenido no está disponible en el idioma seleccionado.

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

    The launcher wizard prompts you to log in to your Red Hat account.

  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 Launcher page, click the Deploy an Example Application button.
  5. On the Create Example Application page, type the name, fuse-circuit-breaker, in the Create Example Application as field.
  6. Click Select an Example.
  7. In the Example dialog, select the Circuit Breaker option. A Select a Runtime dropdown menu appears.

    1. From the Select a Runtime dropdown, select Fuse.
    2. From the version dropdown menu, select 7.0.1 (Red Hat Fuse) (do not select the 2.21.2 (Community) version).
    3. Click Save.
  8. On the Create Example Application page, click Download.
  9. When you see the Your Application is Ready dialog, click Download.zip. Your browser downloads the generated booster project (packaged as a ZIP file).
  10. Your browser now commences downloading the generated booster project (packaged as a ZIP file).
  11. 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
    Copy to Clipboard Toggle word wrap
  2. Open a new shell prompt and start the name service, as follows:

    cd name-service
    mvn spring-boot:run -DskipTests -Dserver.port=8081
    Copy to Clipboard Toggle word wrap

    As Spring Boot starts up, you should see some output like the following:

    ...
    2019-06-25 10:01:40.763  INFO 13064 --- [           main] o.a.camel.spring.SpringCamelContext      : Route: route1 started and consuming from: servlet:/name?httpMethodRestrict=GET
    2019-06-25 10:01:40.764  INFO 13064 --- [           main] o.a.camel.spring.SpringCamelContext      : Total 1 routes, of which 1 are started
    2019-06-25 10:01:40.765  INFO 13064 --- [           main] o.a.camel.spring.SpringCamelContext      : Apache Camel 2.21.0.fuse-000112-redhat-3 (CamelContext: camel-1) started in 0.436 seconds
    2019-06-25 10:01:40.767  INFO 13064 --- [           main] o.a.c.c.s.CamelHttpTransportServlet      : Initialized CamelHttpTransportServlet[name=CamelServlet, contextPath=]
    2019-06-25 10:01:40.840  INFO 13064 --- [           main] b.c.e.u.UndertowEmbeddedServletContainer : Undertow started on port(s) 8081 (http)
    2019-06-25 10:01:40.846  INFO 13064 --- [           main] com.redhat.fuse.boosters.cb.Application  : Started Application in 6.431 seconds (JVM running for 10.073)
    Copy to Clipboard Toggle word wrap
  3. Open a new shell prompt and start the greetings service, as follows:

    cd greetings-service
    mvn spring-boot:run -DskipTests
    Copy to Clipboard Toggle word wrap

    As Spring Boot starts up, you should see some output like the following:

    ...
    2019-06-25 10:03:25.061  INFO 13171 --- [           main] o.a.c.c.s.CamelHttpTransportServlet      : Initialized CamelHttpTransportServlet[name=CamelServlet, contextPath=]
    2019-06-25 10:03:25.136  INFO 13171 --- [           main] b.c.e.u.UndertowEmbeddedServletContainer : Undertow started on port(s) 8080 (http)
    2019-06-25 10:03:25.143  INFO 13171 --- [           main] com.redhat.fuse.boosters.cb.Application  : Started Application in 6.636 seconds (JVM running for 10.59)
    Copy to Clipboard Toggle word wrap
  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"}
    Copy to Clipboard Toggle word wrap
  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"}
    Copy to Clipboard Toggle word wrap

    The log in the window where the greetings service is running shows the following sequence of messages:

    2019-06-25 10:05:05.710  INFO 13171 --- [-CamelHystrix-2] route2                                   :  Try to call name Service
    2019-06-25 10:05:05.721  INFO 13171 --- [-CamelHystrix-2] o.a.c.httpclient.HttpMethodDirector      : I/O exception (java.net.ConnectException) caught when processing request: Connection refused (Connection refused)
    2019-06-25 10:05:05.721  INFO 13171 --- [-CamelHystrix-2] o.a.c.httpclient.HttpMethodDirector      : Retrying request
    2019-06-25 10:05:05.722  INFO 13171 --- [-CamelHystrix-2] o.a.c.httpclient.HttpMethodDirector      : I/O exception (java.net.ConnectException) caught when processing request: Connection refused (Connection refused)
    2019-06-25 10:05:05.722  INFO 13171 --- [-CamelHystrix-2] o.a.c.httpclient.HttpMethodDirector      : Retrying request
    2019-06-25 10:05:05.722  INFO 13171 --- [-CamelHystrix-2] o.a.c.httpclient.HttpMethodDirector      : I/O exception (java.net.ConnectException) caught when processing request: Connection refused (Connection refused)
    2019-06-25 10:05:05.722  INFO 13171 --- [-CamelHystrix-2] o.a.c.httpclient.HttpMethodDirector      : Retrying request
    2019-06-25 10:05:05.729  INFO 13171 --- [-CamelHystrix-2] route2                                   :  We are falling back!!!!
    Copy to Clipboard Toggle word wrap
  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.
Volver arriba
Red Hat logoGithubredditYoutubeTwitter

Aprender

Pruebe, compre y venda

Comunidades

Acerca de la documentación de Red Hat

Ayudamos a los usuarios de Red Hat a innovar y alcanzar sus objetivos con nuestros productos y servicios con contenido en el que pueden confiar. Explore nuestras recientes actualizaciones.

Hacer que el código abierto sea más inclusivo

Red Hat se compromete a reemplazar el lenguaje problemático en nuestro código, documentación y propiedades web. Para más detalles, consulte el Blog de Red Hat.

Acerca de Red Hat

Ofrecemos soluciones reforzadas que facilitan a las empresas trabajar en plataformas y entornos, desde el centro de datos central hasta el perímetro de la red.

Theme

© 2025 Red Hat