Considering graceful shutdown of springboot application which is running in docker and AWS ECS fargate service, there are serveral things need to be noticed.
1. Springboot web application with embeded web servers supports graceful shutdown already, you can enable it with the configuration server.shutdown=graceful in application.properties.
2. For non-web application, Springboot supplies the different ways to do the graceful shutdown, such as implementing DisposableBean interface, @PreDestroy. Or you can disable the Springboot's shutdown hook by calling setRegisterShutdownHook(false), then use Runtime to register your own shutdown hook as following:
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
applicationContext.getBean(your.class).destroy();
applicationContext.close();
}
}));
2. You can run your application in your IDE or in command line, when you click the stop button or press ctrl+c, the shutdown hook will be triggered in local environment.
3. In AWS cloud environment, you can click the stop button for the ECS fargate instance or update the service's desired count number to simulate the shutdown process. ECS will first send SIGTERM to the docker container, after the stop timeout period, it will send SIGKILL to forcefully shutdown the container. In the cloudwatch log, you will see the log of shutdown hook. If you can't see, please check if you run your application in this way, "sh -c java -jar app.jar". Here shell is the PID 1 process, once it recieves the signal, it will stop. It will not pass this signal to your java application. You can optimize this command to "java -jar app.jar" or "sh -c exec java -jar app.jar". Please refer: Topical Guide | Spring Boot Docker and How to close spring boot project gracefully in docker container | Develop Paper

本文介绍如何实现Spring Boot应用的优雅停机,包括配置web应用支持优雅停机的方法及非web应用的不同策略。文中还详细说明了在本地环境及AWS ECS Fargate服务中优雅停机的具体步骤。
1万+

被折叠的 条评论
为什么被折叠?



