Spingboot application in docker container graceful shutdown

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

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值