SpringBoot如何正常(优雅)关闭

一.什么是正常(优雅)关闭:
    1.控制台可以输出:
        o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
    2.可以触发到 ApplicationListener 中的 ContextClosedEvent 事件。


二.怎么实现正常(优雅)关闭:
    
    1.开启 SpringBoot 的 actuator 功能:
        ①`pom.xml`添加依赖:
            <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>

        ②配置文件`application.properties`:
            #开启所有的端点
            management.endpoints.web.exposure.include=*
            #启用shutdown
            management.endpoint.shutdown.enabled=true

        ③执行关闭请求(POST):
            http://127.0.0.1:8080/actuator/shutdown

    2.选用 `spring-boot:start` Maven 命令方式启动:

        ①`pom.xml`添加SpringBoot打包插件配置项`<fork>true</fork>`:
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <configuration>
                            <fork>true</fork>
                        </configuration>
                    </plugin>
                </plugins>
            </build>

        ②启动的 Maven 命令:spring-boot:start
        ③关闭的 Maven 命令:spring-boot:stop
    
    3.程序中调用 ConfigurableApplicationContext.close() 方法;
    4.打包后运行:控制台 `Ctrl + C`, Linux:`kill pid`,都能执行正常(优雅)的关闭。
    5.使用STS(SpringToolSuite)开发工具运行 SpringBoot 项目,运行时选择 Run As`Spring Boot App`。


三、备注:
    经测试,方式1、方式2、方式3 仅能关闭 SpringBoot Web线程,如果入口主方法中有其他线程存在,其他线程将不能正常关闭(如主方法中的Timer线程)。

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot提供了多种方式来实现优雅停机。其中一种方式是使用SpringBoot的actuator功能。通过引入actuator的maven依赖,我们可以开启actuatorshutdown功能。具体操作是在pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> ``` 开启actuatorshutdown功能后,可以通过发送POST请求到`/actuator/shutdown`来触发优雅停机操作。这样可以确保在应用程序完成当前请求后才停止。 另外一种方式是使用第三方的starter库,如`hiatus-spring-boot`、`grace-shutdown`、`graceful-shutdown-spring-boot`等。这些库提供了更多的功能和配置选项,使得优雅停机操作更加灵活和方便。可以根据需求选择合适的库来实现优雅停机。 需要注意的是,如果使用内置的Tomcat服务器,通常需要编写代码来支持优雅停机。在这种情况下,可以参考一些在线文章和官方文档来了解如何实现优雅停机操作。 以上引用内容提供了关于SpringBoot优雅停机的不同方法和工具的信息,可以根据具体需求选择合适的方式来实现优雅停机。123 #### 引用[.reference_title] - *1* *3* [SpringBoot系列: 如何优雅停止服务](https://blog.csdn.net/weixin_34197488/article/details/86032084)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] - *2* [Springboot 优雅停止服务的几种方法](https://blog.csdn.net/weixin_44460333/article/details/104035539)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值