springboot应用程序在ec2上自动关闭 [英] springboot application shutsdown automatically on ec2

查看:279
本文介绍了springboot应用程序在ec2上自动关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ec2上部署了springboot应用程序(ubuntu为t2.medium大小,AWS AMI为t3.medium大小)

I deployed my springboot application on ec2 (ubuntu as t2.medium size, AWS AMI as t3.medium size)

在我的本地PC中,没有问题.但是在ec2上,几个小时后(在我不工作期间)它自己死了

In my local pc, there is no problem.. but on the ec2, after few hours (during I dont work) It died by itself

我只是想检查关机时发生了什么,所以我在java类下面创建了ContextClosedHandler ..但是对我来说似乎没有帮助

I just wanted to check what happened when shutdown so I made a below java class as ContextClosedHandler.. but It seems no help for me

@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.

推荐答案

它自动关闭的原因是注销shell上的ssh会话.
因此,我只是使用 nohup 命令执行了我的应用程序以作为后台执行.

The reason It automatically shutdowns is to logout ssh session on the shell.
so, I just executed my application with nohup command for executing as background.

nohup java -jar my-application.jar &

如果我使用docker部署它,就没有问题.

and if I deploy it using docker, there is no issue.

这篇关于springboot应用程序在ec2上自动关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆