打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
SpringBoot优雅退出

背景

我看了一些介绍SpringBoot源码的文章,里面都是从SpringApplication这个外部调用入口开始,重点介绍里面的listener监听器。今天我想想说说SpringBoot的优雅退出机制。

调用入口调用SpringApplication开始启动SpringBoot

@SpringBootApplicationpublic class Application {    public static void main(String[] args) throws Exception {        SpringApplication.run(Application.class, args);    }}

SpringApplication实例初始化时有以下操作

public SpringApplication(Object... sources) {    this.bannerMode = Mode.CONSOLE;    this.logStartupInfo = true;    this.addCommandLineProperties = true;    this.headless = true;    this.registerShutdownHook = true;    this.additionalProfiles = new HashSet();    this.initialize(sources);}

其中有一步this.registerShutdownHook注册jvm退出前的事件。

概念

jvm有shutdwonHook机制,中文习惯叫优雅退出。相当于在linux系统中执行SIGTERM(kill -15 或者 svc -d)时退出前执行的一些操作。

原理

以linux系统为例说明:

进程在响应kill -15 pid命令进行关闭操作时默认发送SIGTERM信号至指定进程/进程组。如果进程没有捕获该信号的逻辑,则SIGTERM的作用是终止进程。而registerShutdownHook=true说明有注册的事件需要捕获该信号,先执行相应的逻辑再进行终止。

/*** Register a shutdown hook with the JVM runtime, closing this context* on JVM shutdown unless it has already been closed at that time.* <p>Delegates to {@code doClose()} for the actual closing procedure.@see Runtime#addShutdownHook@see #close()@see #doClose()*/@Overridepublic void registerShutdownHook() {   if (this.shutdownHook == null) {      // No shutdown hook registered yet.      this.shutdownHook = new Thread() {         @Override         public void run() {            synchronized (startupShutdownMonitor) {               doClose();            }         }      };      Runtime.getRuntime().addShutdownHook(this.shutdownHook);   }}

如上面spring里registerShutdownHook的源码所示,就是注册一个jvm的shutdownHook钩子函数。jvm退出前会执行这个钩子函数。这个关闭操作在强制退出的时候不起作用。强制关闭范围详见下图


一个SpringBoot项目在优雅退出时做的事情可以通过log来感受一下

2019/06/20 20:47:20.405 Thread-29 [INFO] NettyHttpServer (NettyHttpServer.java:171) Close NettyHttpServer bind 0.0.0.0/0.0.0.0:5080
2019/06/20 20:47:20.405 Thread-36 [INFO] AnnotationConfigApplicationContext (AbstractApplicationContext.java:984) Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@a37aefe: startup date [Thu Jun 20 20:45:38 CST 2019]; root of context hierarchy
2019/06/20 20:47:20.406 Thread-32 [INFO] NettyServer (NettyServer.java:130) stopping netty server(XXX:9000), sleep 6 seconds!
2019/06/20 20:47:20.407 Thread-35 [INFO] ScheduleManager (ScheduleManager.java:373) Before taskNode quit,check executing task and callback result.
2019/06/20 20:47:20.407 Thread-35 [INFO] ScheduleManager (ScheduleManager.java:228) Destroy task node resources: zk and threadpool.
2019-06-20 20:47:20,430 Thread-35 WARN Unable to register Log4j shutdown hook because JVM is shutting down. Using SimpleLogger

总结

分析问题先找抓手 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
你们的优雅停机真的优雅吗? | 京东云技术团队
【剖析 | SOFARPC 框架】系列之 SOFARPC 优雅关闭剖析
Spring Boot 2.5 新特性 之 优雅停机
哦,这就是java的优雅停机?(实现及原理)
Java开发框架Spring源码分析
springBoot初体验
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服