文章目录
1、基础知识
内容概述 : kill 优雅退出, kill -9 暴力退出。
2、测试
2.1、新建测试项目
2.1.1、环境信息
操作系统 : Windows 10
IDE : IntelliJ IDEA 2020.1 (Ultimate Edition)
JDK : 11.0.8
Spring Boot : 2.3.7.RELEASE
2.1.2、新建 SpringBoot 项目
PS : https://start.aliyun.com/
2.2、打包
2.3、部署
上传 demo-0.0.1-SNAPSHOT.jar 包到服务器。
2.3.1、环境信息
操作系统 : CentOS Linux release 7.6.1810 (Core)
JDK : 11.0.8
2.3.2、手动优雅停止服务
启动服务
[root@test test]# nohup java -jar ./demo-0.0.1-SNAPSHOT.jar > ./logs/stdout.log 2>&1 &
[1] 1679
查看服务是否正常启动
[root@test test]# ps -ef|grep demo-0.0.1-SNAPSHOT.jar
root 1679 20331 99 23:36 pts/0 00:00:12 java -jar ./demo-0.0.1-SNAPSHOT.jar
root 1729 20331 0 23:36 pts/0 00:00:00 grep --color=auto demo-0.0.1-SNAPSHOT.jar
手动优雅停止服务
[root@test test]# kill 1679
查看日志
[root@test test]# more logs/stdout.log
nohup: ignoring input
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.7.RELEASE)
2021-05-13 23:36:31.536 INFO 1679 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on 196-Pure-CentOS7 with PID 1679 (/root/test/demo-0.0.1-SNAPSHOT.jar started by root in /root/test)
2021-05-13 23:36:31.541 INFO 1679 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-05-13 23:36:32.807 INFO 1679 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-05-13 23:36:32.825 INFO 1679 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-05-13 23:36:32.825 INFO 1679 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-05-13 23:36:32.917 INFO 1679 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-05-13 23:36:32.918 INFO 1679 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1285 ms
2021-05-13 23:36:33.171 INFO 1679 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-05-13 23:36:33.388 INFO 1679 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-05-13 23:36:33.404 INFO 1679 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.59 seconds (JVM running for 3.289)
2021-05-13 23:40:40.481 INFO 1679 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
最后一行日志可以看出,程序优雅退出了。
如果使用 kill -9 1679 , 则不会有最后一行日志输出。
2.3.3、脚本退出
不想每次都写那么长的命令,所以使用脚本启动&退出。
修改类 DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.ApplicationPidFileWriter;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
// generate a pid in a specified path, while use command to shutdown pid : cat ./app.pid | xargs kill
SpringApplication application = new SpringApplication(DemoApplication.class);
application.addListeners(new ApplicationPidFileWriter("./app.pid"));
application.run();
}
}
同样的方式进行打包、上传。
启动脚本 start.sh
#!/bin/bash
# Program:
# This program for start java services.
# History:
# 2021/05/12 First release
# PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
# export PATH
# start javar services
nohup java -jar ./demo-0.0.1-SNAPSHOT.jar > ./logs/stdout.log 2>&1 &
exit 0
停止脚本 stop.sh
#!/bin/bash
# Program:
# This program for stop java services.
# History:
# 2021/05/12 First release
# PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
# export PATH
# service : java
cat ./app.pid | xargs kill
exit 0
利用启动&停止脚本,同样可以达到优雅停止服务的效果。(推荐使用)
3、总结
事实上,有很多种方法可以优雅的停止服务。本文仅记录了真实的工作中比较常用的一种方法。程序中一般使用内存队列或线程池的时候最好要优雅的停止服务,将内存队列没有处理的数据保存起来或线程池中没处理完的程序处理完。但是因为停机的时候比较快,所以停服务的时候最好不要处理大量的数据操作,这样会影响程序停止。