Spring Boot 优雅停止服务
java1234
共 7589字,需浏览 16分钟
·
2021-05-20 23:04
点击上方蓝色字体,选择“标星公众号”
优质文章,第一时间送达
1、基础知识
2、测试
2.1、新建测试项目
2.1.1、环境信息
2.1.2、新建 SpringBoot 项目
2.2、打包
2.3、部署
2.3.1、环境信息
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'
2.3.3、脚本退出
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();
}
}
#!/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
#!/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、总结
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:
https://blog.csdn.net/achi010/article/details/116770240
粉丝福利:Java从入门到入土学习路线图
👇👇👇
👆长按上方微信二维码 2 秒
感谢点赞支持下哈
评论