李锋镝的博客

  • 首页
  • 时间轴
  • 评论区显眼包🔥
  • 左邻右舍
  • 博友圈
  • 关于我
    • 关于我
    • 另一个网站
    • 我的导航站
    • 网站地图
    • 赞助
  • 留言
  • 🚇开往
Destiny
自是人生长恨水长东
  1. 首页
  2. 原创
  3. 正文

SpringBoot优雅关闭应用

2025年3月2日 245点热度 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
取消回复

愿将腰下剑,直为斩楼兰。

那年今日(04月20日)

  • 1971年:中国著名法学家周鲠生逝世
  • 1901年:著名建筑学家梁思成出生于日本东京,祖籍广东新会
  • 1889年:德国纳粹党元首希特勒出生于奥地利布劳瑙
  • 1808年:法兰西第二帝国皇帝拿破仑出生
  • 429年:中国古代数学家祖冲之出生
  • 更多历史事件
最新 热点 随机
最新 热点 随机
Everything Claude Code 详细使用文档 配置Jackson使用字段而不是getter/setter来序列化和反序列化 这个域名注册整整十年了,十年时间,真快啊 Claude Code全维度实战指南:从入门到精通,解锁AI编程新范式 Apollo配置中心中的protalDB的作用是什么 org.apache.ibatis.plugin.Interceptor类详细介绍及使用
AI时代,个人技术博客的出路在哪里?使用WireGuard在Ubuntu 24.04系统搭建VPN这个域名注册整整十年了,十年时间,真快啊WordPress实现用户评论等级排行榜插件WordPress网站换了个字体,差点儿把样式换崩了做了一个WordPress文章热力图插件
使用WireGuard在Ubuntu 24.04系统搭建VPN 为什么 Apache Doris 是比 Elasticsearch 更好的实时分析替代方案? 妹妹的画【2019.07.03】 jmap命令(jdk1.8) 我要狠狠的反驳“公司禁止使用 Lombok ”的观点! Java之五种遍历Map集合的方式
标签聚合
SpringBoot 多线程 分布式 AI docker 数据库 AI编程 ElasticSearch Redis Spring JVM 设计模式 WordPress IDEA SQL JAVA 架构 日常 MySQL K8s
友情链接
  • Blogs·CN
  • Honesty
  • Mr.Sun的博客
  • 临窗旋墨
  • 哥斯拉
  • 彬红茶日记
  • 志文工作室
  • 懋和道人
  • 拾趣博客导航
  • 搬砖日记
  • 旧时繁华
  • 林羽凡
  • 瓦匠个人小站
  • 皮皮社
  • 知向前端
  • 蜗牛工作室
  • 韩小韩博客
  • 风渡言

COPYRIGHT © 2026 lifengdi.com. ALL RIGHTS RESERVED.

域名年龄

Theme Kratos Made By Dylan

津ICP备2024022503号-3

京公网安备11011502039375号