SpringBoot 如何优雅的停机

这里写目录标题

  • [1 介绍](#1 介绍)
  • [2 使用](#2 使用)
    • [2.1 开启 hook](#2.1 开启 hook)
    • [2.2 禁用 hook](#2.2 禁用 hook)
  • [3 手动指定 hook](#3 手动指定 hook)

1 介绍

SpringBoot 如果需要使用hook则需要开启spring.main.register-shutdown-hook=true(默认为true)

如果使用kill -9则不会出发JVM的hook,kill可以正常触发hook

yml 复制代码
server:
  port: 8080
  shutdown: IMMEDIATE #GRACEFUL/IMMEDIATE (默认IMMEDIATE)

spring:
  main:
    register-shutdown-hook: true #默认ture

2 使用

java 复制代码
package com.example.java.test;

import jakarta.annotation.PreDestroy;
import org.springframework.stereotype.Component;

/**
 * @author hyacinth
 * @date 2023/10/13 23:07
 * @desc: 测试
 * @title: Test
 * @package com.example.java.test
 */
@Component
public class Test {

    @PreDestroy
    public void destroy() {
        System.out.println("销毁");
    }

    public Test() {
        System.out.println("创建");
    }

}

2.1 开启 hook

如果spring.main.register-shutdown-hook=true

java 复制代码
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.1.4)

2023-10-13T23:11:26.801+08:00  INFO 18244 --- [  restartedMain] com.example.java.JavaApplication         : Starting JavaApplication using Java 21 with PID 18244 (D:\SourceCode\Intellij\java\target\classes started by xuhya in D:\SourceCode\Intellij\java)
2023-10-13T23:11:26.805+08:00  INFO 18244 --- [  restartedMain] com.example.java.JavaApplication         : No active profile set, falling back to 1 default profile: "default"
2023-10-13T23:11:26.916+08:00  INFO 18244 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-10-13T23:11:26.916+08:00  INFO 18244 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2023-10-13T23:11:28.594+08:00  INFO 18244 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-10-13T23:11:28.622+08:00  INFO 18244 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-10-13T23:11:28.623+08:00  INFO 18244 --- [  restartedMain] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.13]
2023-10-13T23:11:28.696+08:00  INFO 18244 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-10-13T23:11:28.697+08:00  INFO 18244 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1779 ms
创建
2023-10-13T23:11:29.308+08:00  INFO 18244 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2023-10-13T23:11:29.386+08:00  INFO 18244 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-10-13T23:11:29.403+08:00  INFO 18244 --- [  restartedMain] com.example.java.JavaApplication         : Started JavaApplication in 3.274 seconds (process running for 3.915)
销毁

Process finished with exit code 130

2.2 禁用 hook

如果spring.main.register-shutdown-hook=false

如果spring.main.register-shutdown-hook=true

```java
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.1.4)

2023-10-13T23:11:26.801+08:00  INFO 18244 --- [  restartedMain] com.example.java.JavaApplication         : Starting JavaApplication using Java 21 with PID 18244 (D:\SourceCode\Intellij\java\target\classes started by xuhya in D:\SourceCode\Intellij\java)
2023-10-13T23:11:26.805+08:00  INFO 18244 --- [  restartedMain] com.example.java.JavaApplication         : No active profile set, falling back to 1 default profile: "default"
2023-10-13T23:11:26.916+08:00  INFO 18244 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-10-13T23:11:26.916+08:00  INFO 18244 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2023-10-13T23:11:28.594+08:00  INFO 18244 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-10-13T23:11:28.622+08:00  INFO 18244 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-10-13T23:11:28.623+08:00  INFO 18244 --- [  restartedMain] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.13]
2023-10-13T23:11:28.696+08:00  INFO 18244 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-10-13T23:11:28.697+08:00  INFO 18244 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1779 ms
创建
2023-10-13T23:11:29.308+08:00  INFO 18244 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2023-10-13T23:11:29.386+08:00  INFO 18244 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-10-13T23:11:29.403+08:00  INFO 18244 --- [  restartedMain] com.example.java.JavaApplication         : Started JavaApplication in 3.274 seconds (process running for 3.915)

Process finished with exit code 130

3 手动指定 hook

java 复制代码
package com.example.java;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class JavaApplication {

    public static void main(String[] args) {
        SpringApplication.run(JavaApplication.class, args);

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.out.println("服务已停止");
        }));
    }

}
java 复制代码
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.1.4)

2023-10-13T23:15:08.667+08:00  INFO 16108 --- [  restartedMain] com.example.java.JavaApplication         : Starting JavaApplication using Java 21 with PID 16108 (D:\SourceCode\Intellij\java\target\classes started by xuhya in D:\SourceCode\Intellij\java)
2023-10-13T23:15:08.671+08:00  INFO 16108 --- [  restartedMain] com.example.java.JavaApplication         : No active profile set, falling back to 1 default profile: "default"
2023-10-13T23:15:08.752+08:00  INFO 16108 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-10-13T23:15:08.752+08:00  INFO 16108 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2023-10-13T23:15:10.314+08:00  INFO 16108 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-10-13T23:15:10.335+08:00  INFO 16108 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-10-13T23:15:10.335+08:00  INFO 16108 --- [  restartedMain] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.13]
2023-10-13T23:15:10.402+08:00  INFO 16108 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-10-13T23:15:10.403+08:00  INFO 16108 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1649 ms
创建
2023-10-13T23:15:10.914+08:00  INFO 16108 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2023-10-13T23:15:10.996+08:00  INFO 16108 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-10-13T23:15:11.015+08:00  INFO 16108 --- [  restartedMain] com.example.java.JavaApplication         : Started JavaApplication in 3.038 seconds (process running for 3.626)
服务已停止
相关推荐
飞的肖几秒前
前端使用 Element Plus架构vue3.0实现图片拖拉拽,后等比压缩,上传到Spring Boot后端
前端·spring boot·架构
Q_19284999063 分钟前
基于Spring Boot的摄影器材租赁回收系统
java·spring boot·后端
Code_流苏5 分钟前
VSCode搭建Java开发环境 2024保姆级安装教程(Java环境搭建+VSCode安装+运行测试+背景图设置)
java·ide·vscode·搭建·java开发环境
良许Linux7 分钟前
0.96寸OLED显示屏详解
linux·服务器·后端·互联网
求知若饥19 分钟前
NestJS 项目实战-权限管理系统开发(六)
后端·node.js·nestjs
禁默1 小时前
深入浅出:AWT的基本组件及其应用
java·开发语言·界面编程
Cachel wood1 小时前
python round四舍五入和decimal库精确四舍五入
java·linux·前端·数据库·vue.js·python·前端框架
Code哈哈笑1 小时前
【Java 学习】深度剖析Java多态:从向上转型到向下转型,解锁动态绑定的奥秘,让代码更优雅灵活
java·开发语言·学习
gb42152871 小时前
springboot中Jackson库和jsonpath库的区别和联系。
java·spring boot·后端
程序猿进阶1 小时前
深入解析 Spring WebFlux:原理与应用
java·开发语言·后端·spring·面试·架构·springboot