SpringBoot应用在EC2上自动关闭

3

我在ec2上部署了我的SpringBoot应用程序(使用t2.medium大小的ubuntu,AWS AMI使用t3.medium大小)。

在我的本地电脑上没有问题,但是在ec2上,几个小时后(期间我不工作),它会自己死掉。

我只想检查关闭时发生了什么,所以我创建了一个以下Java类作为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.
2个回答

4
它自动关机的原因是为了在shell上注销ssh会话。
因此,我只需使用nohup命令将我的应用程序执行为后台进程即可。
nohup java -jar my-application.jar &

如果我使用 Docker 部署它,就不会出现问题。


1
这里有一些关于 nohup 的有用后续阅读资料 链接 - Prabhat Choudhary

0
问题并不是应用程序自行关闭,而是人们试图通过发送伪造请求来寻找您的应用程序的安全漏洞。您可以尝试通过在将请求转发到Spring Boot应用程序之前,通过Web服务器(如Apache Httpd)过滤请求来解决此问题。

谢谢您的回复。这是一个简单的问题。我已经回答了我的问题。 - Youngmin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接