如何让 Springboot 的 ShutdownHook 等待,直到它的所有处理请求都完成? [英] How to make the Springboot's ShutdownHook wait, till all of its processing requests are completed?

查看:27
本文介绍了如何让 Springboot 的 ShutdownHook 等待,直到它的所有处理请求都完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 docker 容器内运行我的 Spring Boot Web 应用程序,每次我们安全地停止容器时,它都会突然关闭正在运行的 spring-boot 服务(默认行为).我的要求是在没有任何问题的情况下处理现有请求.我尝试了以下方法,例如在 ContextClosedEvent 中添加睡眠逻辑几分钟,以便现有请求有时间为响应提供服务,但以下方法即使在关闭已启动.有什么具体的方法可以达到我的要求吗?

I am running my Spring boot web application inside the docker container and every time we safely stop the container, it abruptly shut down the running spring-boot service(Default behavior). My requirement is to process the existing requests without any issues. I have tried the below approach something like adding sleep logic for some minutes in ContextClosedEvent, so that the existing requests have time to serve the response, but the following approach doesn't stop the requests incoming even after the shutdown got initiated. is there any specific way to achieve my requirement?

@Slf4j
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.addListeners((ApplicationListener<ContextClosedEvent>) event -> {
            log.info("Shutdown initiated...");
            try {
                Thread.sleep(TimeUnit.MINUTES.toMillis(5));
            } catch (InterruptedException e) {
                log.error("Exception is thrown", e);
            }
            log.info("Graceful Shutdown is processed succesfully");
        });
        application.run(args);
    }

推荐答案

分享我的方法.假设您将 Springboot 应用程序用作 Web 应用程序,并且您希望处理现有请求而不突然终止它们,Spring Boot 提供了开箱即用的解决方案.

Sharing my approach. Let's say you are using the Springboot application as a web app, and you want to process the existing requests without abruptly terminating them, spring boot provides the out-of-box solution.

摘自 Spring 文档

所有四个嵌入式 Web 服务器(Jetty、Reactor Netty、Tomcat 和 Undertow)以及反应式和基于 Servlet 的 Web 应用程序都支持正常关闭.它作为关闭应用程序上下文的一部分发生,并在停止 SmartLifecycle bean 的最早阶段执行.此停止处理使用超时,该超时提供宽限期,在此期间允许完成现有请求但不允许新请求.不允许新请求的确切方式因正在使用的 Web 服务器而异.Jetty、Reactor Netty 和 Tomcat 将停止接受网络层的请求.Undertow 将接受请求,但会立即以服务不可用 (503) 响应进行响应.

Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and Servlet-based web applications. It occurs as part of closing the application context and is performed in the earliest phase of stopping SmartLifecycle beans. This stop processing uses a timeout which provides a grace period during which existing requests will be allowed to complete but no new requests will be permitted. The exact way in which new requests are not permitted varies depending on the web server that is being used. Jetty, Reactor Netty, and Tomcat will stop accepting requests at the network layer. Undertow will accept requests but respond immediately with a service unavailable (503) response.

注意:使用 Tomcat 正常关闭需要 Tomcat 9.0.33 或更高版本.

NOTE: Graceful shutdown with Tomcat requires Tomcat 9.0.33 or later.

代码片段

启用正常关机

server.shutdown=graceful

设置超时时间(这里我设置时间值为1分钟)

To configure the timeout period(here I set the time values as 1minute)

spring.lifecycle.timeout-per-shutdown-phase=1m

控制台输出:

[INFO] [SpringContextShutdownHook] [org.springframework.boot.web.embedded.tomcat.GracefulShutdown:53] Comme
ncing graceful shutdown. Waiting for active requests to complete

...logs of existing running API endpoints

[tomcat-shutdown]   [org.springframework.boot.web.embedded.tomcat.GracefulShutdown:78] Graceful shut
down complete
[SpringContextShutdownHook] [org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor:218] S
hutting down ExecutorService 'applicationTaskExecutor'

从上面的日志中可以看出,spring boot 等待所有正在运行的请求处理并优雅地关闭服务器

From the above logs, you could see that the spring boot waits for all the running requests to process and shut the server gracefully

这篇关于如何让 Springboot 的 ShutdownHook 等待,直到它的所有处理请求都完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆