1.3. 构建您的 booster 项目
这些说明指导您在 Spring Boot 上使用 Fuse 构建 Circuir Breaker booster。
先决条件
- 您必须已通过 Red Hat Developer Portal 生成并下载您的 booster 项目。
- 您必须有受支持的 Java Developer Kit (JDK)版本。详情请查看 支持的配置页面。
- 您必须具有 Apache Maven 3.3.x 或更高版本。
流程
打开 shell 提示符并使用 Maven 从命令行构建项目:
cd fuse-circuit-breaker
mvn clean package
Maven 构建项目后,它会显示 Build Success 消息。
打开一个新的 shell 提示符并启动名称服务,如下所示:
cd name-service
mvn spring-boot:run -DskipTests -Dserver.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)
打开一个新的 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 端点。通过在 web 浏览器中打开 URL 或打开另一个 shell 提示符并输入以下
curl
命令来调用 REST 端点:curl http://localhost:8080/camel/greetings
以下是响应:
{"greetings":"Hello, Jacopo"}
要演示 Camel Hystrix 提供的断路器功能,请在名称服务的 shell 提示符窗口中键入 Ctrl-C 来终止后端名称服务。
现在,名称服务不可用,断路器在 中启动,以防止在调用时问候服务挂起。
在网页浏览器中打开
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!!!!
-
有关此示例的更多信息,请打开 Circuit Breaker - Red Hat Fuse 页面,地址为 http://localhost:8080/ (同时
问候服务正在运行
)。此页面包含一个到监控断路器状态的 Hystrix 仪表板的链接。