使用actuator优雅地停止SpringBoot应用

原文地址:开发者导航

优雅如何定义?

简而言之,就是对应用进程发送停止指令之后,能够保证正在执行的业务操作不受影响,可以继续完成已有请求的处理,但是停止处理新来的请求。

在 Spring Boot 2.3及以后版本中增加了新特性:优雅停止,目前 Spring Boot 内置的四个嵌入式 Web 服务器(Jetty、Reactor Netty、Tomcat 和 Undertow)以及反应式和基于Servlet的Web应用程序都支持优雅停止。

Spring Boot 2.3 及以上版本优雅停止

首先创建一个 Spring Boot 的 Web 项目,版本选择 2.3.0.RELEASE,Spring Boot 2.3.0.RELEASE内置的 Tomcat版本时9.0.35。

其中,优雅关闭内置Web容器的入口代码在 

org.springframework.boot.web.embedded.tomcat 的 GracefulShutdown 里,大概逻辑就是先停止外部的所有新请求,然后再处理关闭前收到的请求,有兴趣的可以自己去看下。

内嵌的 Tomcat 容器平滑关闭的配置已经完成了,那么如何优雅关闭 Spring 容器了,就需要 Actuator 来实现 Spring 容器的关闭了。

然后加入 actuator 依赖,依赖如下所示:

1

2

3

4

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

然后接着再添加一些配置来暴露 actuator 的 shutdown 接口:

注意此处的坑,下面的配置中,一个是endpoint,一个是endpoints,多了个s,不一样的。

1

2

3

4

5

6

7

8

management:

  endpoint:

    shutdown:

      enabled: true

  endpoints:

    web:

      exposure:

        include: shutdown

启动项目,此时访问http://localhost/actuator会显示支持的方法。

如果把上面配置中的shutdown换成*,会看到actuator支持的所有方法,如健康检查、统计、监控、审计等,这里不做介绍。

{

"_links": {

"self": {

"href": "http://localhost/actuator",

"templated": false

},

"shutdown": {

"href": "http://localhost/actuator/shutdown",

"templated": false

}

}

}

配置搞定后,然后创建一个DemoController控制器,并有一个 sayHi()方法,线程等待10s,用来模拟业务耗时处理,具体代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

/**

 * 优雅关闭springboot

 *

 * @author 架构师小跟班 jiagou1216.com

 */

@RestController

public class DemoController {

    @GetMapping("/sayHi")

    public String sayHi() throws InterruptedException {

        // 模拟复杂业务耗时处理流程

        Thread.sleep(10 * 1000L);

        return "success";

    }

}

启动项目,使用postman测试 ,会等待10s后显示请求成功,等待中......:

此时调用http://localhost/actuator/shutdown 就可以执行优雅地停止,返回结果如下:

{

    "message": "Shutting down, bye..."

}

如果在这个时候,发起新的请求 http://localhost/sayHi,会没有反应,因为服务已经“停止”了:

再回头看第一个请求,返回了结果(10s后):success。

其中有几条服务日志如下:

2020-05-25 23:05:15.163 INFO 102724 --- [ Thread-253] o.s.b.w.e.tomcat.GracefulShutdown : Commencing graceful shutdown. Waiting for active requests to complete

2020-05-25 23:05:15.287 INFO 102724 --- [tomcat-shutdown] o.s.b.w.e.tomcat.GracefulShutdown : Graceful shutdown complete

2020-05-25 23:05:15.295 INFO 102724 --- [ Thread-253] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'

从日志中可以看出来,当调用 shutdown 接口的时候,会先等待请求处理完毕后再优雅地停止。

注意:

如果使用浏览器调用http://localhost/actuator/shutdown方法,会报405,要用POST请求。

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
使用SpringBoot时,我们可以使用钩子函数来实现优雅停止服务。钩子函数是在程序关闭之前执行的一段代码,可以用来完成一些必要的清理工作。有几种方法可以实现这个功能。 第一种方法是使用SpringBoot提供的Actuator功能。Actuator库提供了一系列的端点(endpoint),其中包括shutdown端点。默认情况下,shutdown端点是禁用的,我们需要在配置文件中打开它。首先,在pom.xml文件中引入Actuator的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> ``` 然后,在配置文件中添加以下配置: ```yaml management: endpoints: web: exposure: include: shutdown ``` 现在,当我们访问"/actuator/shutdown"端点时,SpringBoot应用程序将优雅停止。 另外一种方法是使用Java的Runtime类中的addShutdownHook()方法。通过在应用程序中注册一个钩子函数,我们可以在程序关闭前执行自定义的逻辑。例如,我们可以在钩子函数中停止依赖的服务、输出日志或发送信号给其他应用程序。以下是一个示例代码: ```java Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { // 在这里执行停止服务的逻辑,比如停止依赖的服务、输出日志等 } }); ``` 当程序关闭时,钩子函数中的代码将被执行。 这些方法可以帮助我们实现SpringBoot应用程序的优雅停止。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Springboot 优雅停止服务的几种方法](https://blog.csdn.net/weixin_44421461/article/details/123587860)[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 style="max-width: 100%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

开发者导航

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值