如何将 Spring Boot 应用程序战争部署到 AWS Elastic Beanstalk?

Posted

技术标签:

【中文标题】如何将 Spring Boot 应用程序战争部署到 AWS Elastic Beanstalk?【英文标题】:How to deploy Spring Boot application war to AWS Elastic Beanstalk? 【发布时间】:2015-09-07 01:55:01 【问题描述】:

之前有人问过类似的问题,但我看到的所有答案都是“它只是有效!”。可悲的是,它不适合我。

我的问题是:当我将一个 war 文件部署到 Elastic Beanstalk 时,我从 catalina.out 收到一堆错误,说“无法启动组件”(下面的日志)。

我已经阅读了 Spring Boot 教程,正在使用 Gradle 进行构建,并添加了“war”插件,以便构建可部署的 war 文件。在本地运行时,我可以访问“来自 Spring Boot 的问候!”。除了生成可部署的war文件并尝试更改项目的侦听端口之外,我没有对这个项目进行任何更改。

我搞砸了负载均衡器配置,允许端口 8080 入站和出站,并向 IAM 添加了“FullAccessEC2Instance”安全角色,以为我没有正确设置权限(但这没有'它只是工作的感觉)。

目前正在旋转我的***,猜测现在该做什么。我正在考虑测试部署到 Heroku,看看那里是否也有问题,或者我的 AWS 配置是否有问题。

    2015-06-21 14:52:02.946  INFO 2644 --- [ost-startStop-1] o.s.boot.SpringApplication               : Started application in 11.831 seconds (JVM running for 21.292)
21-Jun-2015 14:52:02.970 SEVERE [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start: 
 org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1069)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1719)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer.isEnabled(WebSocketServerContainerInitializer.java:122)
    at org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer.onStartup(WebSocketServerContainerInitializer.java:137)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5151)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 10 more

21-Jun-2015 14:52:02.972 SEVERE [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Error deploying web application directory /var/lib/tomcat8/webapps/ROOT
 java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:728)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1069)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1719)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

21-Jun-2015 14:52:02.980 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /var/lib/tomcat8/webapps/ROOT has finished in 18,574 ms
21-Jun-2015 14:52:02.985 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
21-Jun-2015 14:52:03.015 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
21-Jun-2015 14:52:03.026 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 18751 ms

谢谢大家!

编辑:

这是加载实例的完整 catalina.out:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.2.4.RELEASE)

2015-06-21 15:31:43.017  INFO 2034 --- [ost-startStop-1] o.s.boot.SpringApplication               : Starting application on ip-172-31-6-84 with PID 2034 (/var/lib/tomcat8/webapps/ROOT/WEB-INF/lib/spring-boot-1.2.4.RELEASE.jar started by tomcat in /usr/share/tomcat8)
2015-06-21 15:31:43.096  INFO 2034 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2427d57f: startup date [Sun Jun 21 15:31:43 UTC 2015]; root of context hierarchy
2015-06-21 15:31:45.158  INFO 2034 --- [ost-startStop-1] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2015-06-21 15:31:46.622  INFO 2034 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3526 ms
2015-06-21 15:31:47.884  INFO 2034 --- [ost-startStop-1] .b.a.w.JettyWebSocketContainerCustomizer : NonEmbeddedServletContainerFactory detected. Websockets support should be native so this normally is not a problem.
2015-06-21 15:31:50.306  INFO 2034 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2015-06-21 15:31:50.312  INFO 2034 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'errorPageFilter' to: [/*]
2015-06-21 15:31:50.312  INFO 2034 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'metricFilter' to: [/*]
2015-06-21 15:31:50.312  INFO 2034 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2015-06-21 15:31:50.312  INFO 2034 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2015-06-21 15:31:50.313  INFO 2034 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'webRequestLoggingFilter' to: [/*]
2015-06-21 15:31:50.313  INFO 2034 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'applicationContextIdFilter' to: [/*]
2015-06-21 15:31:51.178  INFO 2034 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2427d57f: startup date [Sun Jun 21 15:31:43 UTC 2015]; root of context hierarchy
2015-06-21 15:31:51.388  INFO 2034 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.String hello.HelloController.index()
2015-06-21 15:31:51.391  INFO 2034 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
2015-06-21 15:31:51.391  INFO 2034 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2015-06-21 15:31:51.493  INFO 2034 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-06-21 15:31:51.493  INFO 2034 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-06-21 15:31:51.637  INFO 2034 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-06-21 15:31:53.442  INFO 2034 --- [ost-startStop-1] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "[/health],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(java.security.Principal)
2015-06-21 15:31:53.443  INFO 2034 --- [ost-startStop-1] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "[/trace],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2015-06-21 15:31:53.443  INFO 2034 --- [ost-startStop-1] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "[/beans],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2015-06-21 15:31:53.453  INFO 2034 --- [ost-startStop-1] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "[/env/name:.*],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
2015-06-21 15:31:53.457  INFO 2034 --- [ost-startStop-1] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "[/env],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2015-06-21 15:31:53.457  INFO 2034 --- [ost-startStop-1] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "[/dump],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2015-06-21 15:31:53.458  INFO 2034 --- [ost-startStop-1] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "[/autoconfig],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2015-06-21 15:31:53.458  INFO 2034 --- [ost-startStop-1] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "[/configprops],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2015-06-21 15:31:53.458  INFO 2034 --- [ost-startStop-1] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "[/mappings],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2015-06-21 15:31:53.459  INFO 2034 --- [ost-startStop-1] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "[/info],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2015-06-21 15:31:53.459  INFO 2034 --- [ost-startStop-1] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "[/metrics/name:.*],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
2015-06-21 15:31:53.459  INFO 2034 --- [ost-startStop-1] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "[/metrics],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2015-06-21 15:31:53.516  INFO 2034 --- [ost-startStop-1] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2015-06-21 15:31:53.561  INFO 2034 --- [ost-startStop-1] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2015-06-21 15:31:53.844  INFO 2034 --- [ost-startStop-1] o.s.boot.SpringApplication               : Started application in 12.088 seconds (JVM running for 21.354)
21-Jun-2015 15:31:53.859 SEVERE [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start: 
 org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1069)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1719)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer.isEnabled(WebSocketServerContainerInitializer.java:122)
    at org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer.onStartup(WebSocketServerContainerInitializer.java:137)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5151)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 10 more

21-Jun-2015 15:31:53.866 SEVERE [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Error deploying web application directory /var/lib/tomcat8/webapps/ROOT
 java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:728)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1069)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1719)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

21-Jun-2015 15:31:53.873 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /var/lib/tomcat8/webapps/ROOT has finished in 18,712 ms
21-Jun-2015 15:31:53.878 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
21-Jun-2015 15:31:53.896 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
21-Jun-2015 15:31:53.904 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 18879 ms

【问题讨论】:

【参考方案1】:

原来的解决方案是在 build.grade 的依赖项部分中只包含 Jetty 或 Tomcat。似乎这在文档中是正确的,但我误解了我的意思是删除一个/或,这取决于我想如何部署它。

【讨论】:

以上是关于如何将 Spring Boot 应用程序战争部署到 AWS Elastic Beanstalk?的主要内容,如果未能解决你的问题,请参考以下文章

为 Spring Boot 应用程序生成战争

将 Spring Boot 战争部署到 Tomcat 服务器并收到“无法启动嵌入式 Tomcat org.springframework.context.ApplicationContextExcep

如何使用 Jenkins 部署 Spring Boot Maven 应用程序?

如何使用 Tomcat 部署(和访问)Spring Boot 战争? (使用入门指南)

未能将 Spring Boot 应用程序部署到 WildFly

无法将 Spring Boot 应用程序部署到 Azure