我的应用程序是使用 Spring Boot 和 Apache Camel 用 Java 编写的。当在我的 Windows 开发环境中运行时,它启动并运行良好。但是,当在我的 Open Shift 测试环境中运行时,应用程序正常启动然后关闭,就像我按下了 CTRL+C 一样。
网上的一些建议是添加或删除某些依赖项。这已经在我的 Maven pom.xml 中完成:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
以下是我的日志中最相关的部分:
2019-04-23T12:34:37.009+0000 INFO [main] [org.apache.camel.spring.SpringCamelContext] [] [] [] [] [] [] [] [] [] [] Route: myservice-localhost started and consuming from: direct://myservice-localhost
2019-04-23T12:34:37.011+0000 INFO [main] [org.apache.camel.spring.SpringCamelContext] [] [] [] [] [] [] [] [] [] [] Route: route1 started and consuming from: direct://myservice-myroute
2019-04-23T12:34:37.016+0000 INFO [main] [org.apache.camel.spring.SpringCamelContext] [] [] [] [] [] [] [] [] [] [] Route: myservice started and consuming from: servlet:/myservice/%7BcompanyID%7D
2019-04-23T12:34:37.021+0000 INFO [main] [org.apache.camel.spring.SpringCamelContext] [] [] [] [] [] [] [] [] [] [] Total 9 routes, of which 9 are started
2019-04-23T12:34:37.041+0000 INFO [main] [org.apache.camel.spring.SpringCamelContext] [] [] [] [] [] [] [] [] [] [] Apache Camel 2.21.0 (CamelContext: OBGW-Camel) started in 4.165 seconds
2019-04-23T12:34:37.289+0000 INFO [main] [org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainer] [] [] [] [] [] [] [] [] [] [] Undertow started on port(s) 8090 (http)
2019-04-23T12:34:37.294+0000 INFO [main] [org.springframework.context.support.DefaultLifecycleProcessor] [] [] [] [] [] [] [] [] [] [] Starting beans in phase 0
2019-04-23T12:34:37.415+0000 INFO [main] [org.apache.camel.component.servlet.CamelHttpTransportServlet] [] [] [] [] [] [] [] [] [] [] Initialized CamelHttpTransportServlet[name=CamelServlet, contextPath=]
2019-04-23T12:34:37.416+0000 INFO [main] [org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainer] [] [] [] [] [] [] [] [] [] [] Undertow started on port(s) 8080 (http)
2019-04-23T12:34:37.422+0000 INFO [main] [com.mycompany.Application] [] [] [] [] [] [] [] [] [] [] Started Application in 21.385 seconds (JVM running for 22.759)
2019-04-23T12:34:49.811+0000 INFO [Thread-2] [org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext] [] [] [] [] [] [] [] [] [] [] Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5387f9e0: startup date [Tue Apr 23 12:34:17 UTC 2019]; root of context hierarchy
2019-04-23T12:34:49.812+0000 INFO [Thread-2] [org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext] [] [] [] [] [] [] [] [] [] [] Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@630390b9: startup date [Tue Apr 23 12:34:31 UTC 2019]; parent: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5387f9e0
2019-04-23T12:34:49.846+0000 INFO [Thread-2] [org.springframework.context.support.DefaultLifecycleProcessor] [] [] [] [] [] [] [] [] [] [] Stopping beans in phase 2147483647
2019-04-23T12:34:49.846+0000 INFO [Thread-2] [org.apache.camel.spring.SpringCamelContext] [] [] [] [] [] [] [] [] [] [] Apache Camel 2.21.0 (CamelContext: OBGW-Camel) is shutting down
2019-04-23T12:34:49.852+0000 INFO [Thread-2] [org.apache.camel.impl.DefaultShutdownStrategy] [] [] [] [] [] [] [] [] [] [] Starting to graceful shutdown 9 routes (timeout 300 seconds)
2019-04-23T12:34:49.872+0000 INFO [Camel (OBGW-Camel) thread #1 - ShutdownTask] [org.apache.camel.impl.DefaultShutdownStrategy] [] [] [] [] [] [] [] [] [] [] Route: myservice shutdown complete, was consuming from: servlet:/myservice/%7BcompanyID%7D
2019-04-23T12:34:49.872+0000 INFO [Camel (OBGW-Camel) thread #1 - ShutdownTask] [org.apache.camel.impl.DefaultShutdownStrategy] [] [] [] [] [] [] [] [] [] [] Route: route1 shutdown complete, was consuming from: direct://myservice-myroute
在 Open Shift 环境中,一切都完全相同,直到语句 Started Application
开始关闭。
最佳答案
通常,对 spring-boot-starter-web
的依赖足以保持进程的启动和运行。
但是,您正在使用 Undertow作为 Camel 的 HTTP 服务器而不是 Spring Boot 的标准 Tomcat。因此你可能需要把
camel.springboot.main-run-controller=true
进入您的应用程序属性(正如评论所建议的那样)。 Camel SpringBoot page上也提到了这一点的文档。
关于java - 使用 Apache Camel 的 Spring Boot 应用程序在启动后立即关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55812172/