此内容没有您所选择的语言版本。

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 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:

1.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 Deployment type page, click I will build and run locally.
  5. On the Mission page, select the Circuit Breaker mission and click Next.
  6. On the Runtime page, select the Fuse runtime (not the Spring Boot runtime) and click Next.
  7. On the Project Info page, select the 7.0.0 GA version from the Runtime Version dropdown menu. Accept the default settings for the other fields on this page.
  8. Click Next.
  9. On the Review Summary page, click Download as ZIP File to download the generated project.
  10. 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:

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

    cd booster-circuit-breaker-spring-boot
    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:

    ...
    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)
    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:

    ...
    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)
    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:

    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!!!!
    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.
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat