搜索

1.3. 构建您的 booster 项目

download PDF

这些说明指导您在 Spring Boot 上使用 Fuse 构建 Circuir Breaker booster。

先决条件

流程

  1. 打开 shell 提示符并使用 Maven 从命令行构建项目:

    cd fuse-circuit-breaker
    mvn clean package

    Maven 构建项目后,它会显示 Build Success 消息。

  2. 打开一个新的 shell 提示符并启动名称服务,如下所示:

    cd name-service
    mvn spring-boot:run -DskipTests -Dspring-boot.run.arguments="--server.port=8081"

    当 Spring Boot 启动时,您应该看到类似如下的输出:

    ...
    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)
  3. 打开一个新的 shell 提示符并启动问候服务,如下所示:

    cd greetings-service
    mvn spring-boot:run -DskipTests

    当 Spring Boot 启动时,您应该看到类似如下的输出:

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

    greetings 服务通过 http://localhost:8080/camel/greetings URL 公开 REST 端点。

  4. 通过在 web 浏览器中打开 URL 或打开另一个 shell 提示符并输入以下 curl 命令来调用 REST 端点:

    curl http://localhost:8080/camel/greetings

    以下是响应:

    {"greetings":"Hello, Jacopo"}
  5. 要演示 Camel Hystrix 提供的断路器功能,请在名称服务的 shell 提示符窗口中键入 Ctrl-C 来终止后端名称服务。

    现在,名称服务不可用,断路器在 中启动,以防止在调用时问候服务挂起。

  6. 在网页浏览器中打开 http://localhost:8080/camel/greetings,或者在另一个 shell 提示符窗口中输入以下 curl 命令来调用问候 REST 端点:

    curl http://localhost:8080/camel/greetings

    以下是响应:

    {"greetings":"Hello, default fallback"}

    在运行 greetings 服务的窗口中,日志会显示以下信息序列:

    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!!!!
  7. 有关此示例的更多信息,请打开 Circuit Breaker - Red Hat Fuse 页面,地址为 http://localhost:8080/ (同时 问候服务正在运行 )。此页面包含一个到监控断路器状态的 Hystrix 仪表板的链接。
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

© 2024 Red Hat, Inc.