李锋镝的博客

  • 首页
  • 时间轴
  • 留言
  • 插件
  • 左邻右舍
  • 关于我
    • 关于我
    • 另一个网站
    • 我的导航站
  • 赞助
Destiny
自是人生长恨水长东
  1. 首页
  2. 原创
  3. 正文

SpringBoot优雅关闭应用

2025年3月2日 244点热度 0人点赞 0条评论

在 Spring Boot 应用中实现优雅关闭可以确保应用在停止时能够妥善处理正在进行的任务、释放资源,避免数据丢失和系统异常。以下是几种实现 Spring Boot 应用优雅关闭的方法:

1. 使用 Spring Boot Actuator

Spring Boot Actuator 提供了一个 /shutdown 端点,可以用于优雅地关闭应用程序。
步骤
添加依赖:在 pom.xml 中添加 Spring Boot Actuator 依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

配置端点:在 application.properties 或 application.yml 中启用 /shutdown 端点:

management.endpoints.web.exposure.include=shutdown
management.endpoint.shutdown.enabled=true
yaml
management:
  endpoints:
    web:
      exposure:
        include: shutdown
  endpoint:
    shutdown:
      enabled: true

触发关闭:通过发送 POST 请求到 /actuator/shutdown 来触发应用的优雅关闭:

curl -X POST http://localhost:8080/actuator/shutdown

2. 实现 DisposableBean 或使用 @PreDestroy 注解

DisposableBean 接口

实现 DisposableBean 接口的 destroy 方法,该方法会在 Spring 容器关闭时自动调用。

import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;

@Component
public class MyDisposableBean implements DisposableBean {
    @Override
    public void destroy() throws Exception {
        // 在这里执行清理操作,如关闭数据库连接、释放资源等
        System.out.println("Performing cleanup operations...");
    }
}

@PreDestroy 注解

使用 @PreDestroy 注解标记一个方法,该方法会在Bean销毁之前执行。

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

@Component
public class MyPreDestroyBean {
    @PreDestroy
    public void cleanup() {
        // 在这里执行清理操作
        System.out.println("Cleaning up resources...");
    }
}

3. 自定义关闭钩子

通过 Runtime.getRuntime().addShutdownHook() 方法添加自定义的关闭钩子,在应用关闭时执行特定的操作。

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class ShutdownHookExample implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            // 在这里执行清理操作
            System.out.println("Shutdown hook is running...");
        }));
    }
}

4. 使用 GracefulShutdown 功能(Spring Boot 2.3+)

Spring Boot 2.3 及以上版本提供了内置的 GracefulShutdown 功能,可以在应用关闭时等待一段时间,让正在进行的请求有足够的时间完成。
配置
在 application.properties 或 application.yml 中配置优雅关闭的超时时间:

server.shutdown=graceful
spring.lifecycle.timeout-per-shutdown-phase=10s
server:
  shutdown: graceful
spring:
  lifecycle:
    timeout-per-shutdown-phase: 10s

上述配置表示在关闭应用时,会等待 10 秒让正在进行的请求完成。

除非注明,否则均为李锋镝的博客原创文章,转载必须以链接形式标明本文链接

本文链接:https://www.lifengdi.com/article/4225

相关文章

  • SpringBoot常用注解
  • CompletableFuture使用详解
  • SpringBoot 中内置的 49 个常用工具类
  • SpringBoot 实现接口防刷的 5 种实现方案
  • SpringBoot 实现 RSA+AES 自动接口解密
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: SpringBoot
最后更新:2025年3月3日

李锋镝

既然选择了远方,便只顾风雨兼程。

打赏 点赞
< 上一篇
下一篇 >

文章评论

1 2 3 4 5 6 7 8 9 11 12 13 14 15 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 46 47 48 49 50 51 52 53 54 55 57 58 60 61 62 63 64 65 66 67 69 72 74 76 77 78 79 80 81 82 85 86 87 90 92 93 94 95 96 97 98 99
取消回复

COPYRIGHT © 2025 lifengdi.com. ALL RIGHTS RESERVED.

Theme Kratos Made By Dylan

津ICP备2024022503号-3