spring boot 应用程序在 ec2 上自动关闭
springboot application shutsdown automatically on ec2
我在 ec2 上部署了我的 springboot 应用程序
(ubuntu 为 t2.medium 大小,AWS AMI 为 t3.medium 大小)
在我的本地电脑上,没有问题..
但是在 ec2 上,几个小时后(在我不工作期间)
它自己死了
我只是想检查关机时发生了什么,所以我在下面 java class 作为 ContextClosedHandler.. 但它似乎对我没有帮助
@Component
public class ContextClosedHandler implements ApplicationListener<ContextClosedEvent> {
@Override
public void onApplicationEvent(ContextClosedEvent event) {
LOGGER.info(event.getSource());
LOGGER.info(event.getApplicationContext().getApplicationName());
LOGGER.info(event.getApplicationContext().getDisplayName());
LOGGER.info(event.getApplicationContext().getId());
LOGGER.info(event.getApplicationContext().getParent());
LOGGER.info(event.getApplicationContext().getStartupDate());
}
}
日志如下
[INFO / 20201112 14:19 / http-nio-8080-exec-1 / Caller+0 at org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:175)
Caller+1 at org.apache.juli.logging.DirectJDKLog.info(DirectJDKLog.java:121)
Error parsing HTTP request header
Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Invalid character found in the HTTP protocol [HTTP/1.10x0aHost:]
at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:560)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:260)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
[INFO / 20201112 14:51 / http-nio-8080-exec-2 / Caller+0 at org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:173)
Caller+1 at org.apache.juli.logging.DirectJDKLog.info(DirectJDKLog.java:116)
Initializing Spring DispatcherServlet 'dispatcherServlet'
[INFO / 20201112 14:51 / http-nio-8080-exec-2 / Caller+0 at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:525)
Caller+1 at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:170)
Initializing Servlet 'dispatcherServlet'
[INFO / 20201112 14:51 / http-nio-8080-exec-2 / Caller+0 at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:547)
Caller+1 at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:170)
Completed initialization in 9 ms
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:26)
Caller+1 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:12)
event.getSource(): org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4c203ea1, started on Thu Nov 12 11:56:20 UTC 2020
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:27)
Caller+1 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:12)
event.getApplicationContext().getApplicationName():
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:30)
Caller+1 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:12)
event.getApplicationContext().getDisplayName(): org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4c203ea1
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:32)
Caller+1 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:12)
event.getApplicationContext().getId(): application
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:33)
Caller+1 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:12)
event.getApplicationContext().getParent(): null
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:36)
Caller+1 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:12)
event.getApplicationContext().getStartupDate(): 1605182180002
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at org.springframework.scheduling.concurrent.ExecutorConfigurationSupport.shutdown(ExecutorConfigurationSupport.java:218)
Caller+1 at org.springframework.scheduling.concurrent.ExecutorConfigurationSupport.destroy(ExecutorConfigurationSupport.java:208)
Shutting down ExecutorService 'scheduler'
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at org.springframework.scheduling.concurrent.ExecutorConfigurationSupport.shutdown(ExecutorConfigurationSupport.java:218)
Caller+1 at org.springframework.scheduling.concurrent.ExecutorConfigurationSupport.destroy(ExecutorConfigurationSupport.java:208)
Shutting down ExecutorService 'executor'
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at com.zaxxer.hikari.HikariDataSource.close(HikariDataSource.java:350)
Caller+1 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
HikariPool-1 - Shutdown initiated...
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at com.zaxxer.hikari.HikariDataSource.close(HikariDataSource.java:352)
Caller+1 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
HikariPool-1 - Shutdown completed.
问题不在于应用程序自行关闭,而是有人试图通过发送伪造的请求来查找您应用程序的安全漏洞。您可以尝试通过 Web 服务器(如 Apache Httpd)过滤请求,然后再将它们转发到 Spring 启动应用程序来解决它。
它自动关闭的原因是在 shell.
上注销 ssh 会话
所以,我只是用 nohup 命令执行我的应用程序作为后台执行。
nohup java -jar my-application.jar &
如果我使用 docker 部署它,则没有问题。
我在 ec2 上部署了我的 springboot 应用程序 (ubuntu 为 t2.medium 大小,AWS AMI 为 t3.medium 大小)
在我的本地电脑上,没有问题.. 但是在 ec2 上,几个小时后(在我不工作期间) 它自己死了
我只是想检查关机时发生了什么,所以我在下面 java class 作为 ContextClosedHandler.. 但它似乎对我没有帮助
@Component
public class ContextClosedHandler implements ApplicationListener<ContextClosedEvent> {
@Override
public void onApplicationEvent(ContextClosedEvent event) {
LOGGER.info(event.getSource());
LOGGER.info(event.getApplicationContext().getApplicationName());
LOGGER.info(event.getApplicationContext().getDisplayName());
LOGGER.info(event.getApplicationContext().getId());
LOGGER.info(event.getApplicationContext().getParent());
LOGGER.info(event.getApplicationContext().getStartupDate());
}
}
日志如下
[INFO / 20201112 14:19 / http-nio-8080-exec-1 / Caller+0 at org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:175)
Caller+1 at org.apache.juli.logging.DirectJDKLog.info(DirectJDKLog.java:121)
Error parsing HTTP request header
Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Invalid character found in the HTTP protocol [HTTP/1.10x0aHost:]
at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:560)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:260)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
[INFO / 20201112 14:51 / http-nio-8080-exec-2 / Caller+0 at org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:173)
Caller+1 at org.apache.juli.logging.DirectJDKLog.info(DirectJDKLog.java:116)
Initializing Spring DispatcherServlet 'dispatcherServlet'
[INFO / 20201112 14:51 / http-nio-8080-exec-2 / Caller+0 at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:525)
Caller+1 at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:170)
Initializing Servlet 'dispatcherServlet'
[INFO / 20201112 14:51 / http-nio-8080-exec-2 / Caller+0 at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:547)
Caller+1 at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:170)
Completed initialization in 9 ms
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:26)
Caller+1 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:12)
event.getSource(): org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4c203ea1, started on Thu Nov 12 11:56:20 UTC 2020
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:27)
Caller+1 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:12)
event.getApplicationContext().getApplicationName():
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:30)
Caller+1 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:12)
event.getApplicationContext().getDisplayName(): org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4c203ea1
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:32)
Caller+1 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:12)
event.getApplicationContext().getId(): application
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:33)
Caller+1 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:12)
event.getApplicationContext().getParent(): null
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:36)
Caller+1 at kr.co.motov.ssp.util.ContextClosedHandler.onApplicationEvent(ContextClosedHandler.java:12)
event.getApplicationContext().getStartupDate(): 1605182180002
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at org.springframework.scheduling.concurrent.ExecutorConfigurationSupport.shutdown(ExecutorConfigurationSupport.java:218)
Caller+1 at org.springframework.scheduling.concurrent.ExecutorConfigurationSupport.destroy(ExecutorConfigurationSupport.java:208)
Shutting down ExecutorService 'scheduler'
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at org.springframework.scheduling.concurrent.ExecutorConfigurationSupport.shutdown(ExecutorConfigurationSupport.java:218)
Caller+1 at org.springframework.scheduling.concurrent.ExecutorConfigurationSupport.destroy(ExecutorConfigurationSupport.java:208)
Shutting down ExecutorService 'executor'
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at com.zaxxer.hikari.HikariDataSource.close(HikariDataSource.java:350)
Caller+1 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
HikariPool-1 - Shutdown initiated...
[INFO / 20201112 15:19 / SpringContextShutdownHook / Caller+0 at com.zaxxer.hikari.HikariDataSource.close(HikariDataSource.java:352)
Caller+1 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
HikariPool-1 - Shutdown completed.
问题不在于应用程序自行关闭,而是有人试图通过发送伪造的请求来查找您应用程序的安全漏洞。您可以尝试通过 Web 服务器(如 Apache Httpd)过滤请求,然后再将它们转发到 Spring 启动应用程序来解决它。
它自动关闭的原因是在 shell.
上注销 ssh 会话
所以,我只是用 nohup 命令执行我的应用程序作为后台执行。
nohup java -jar my-application.jar &
如果我使用 docker 部署它,则没有问题。