Graceful Shutdown

所有四个嵌入式 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 或更高版本。

Graceful shutdown with Tomcat requires Tomcat 9.0.33 or later.

若要启用正常关闭,请配置 configprop:server.shutdown[] 属性,如下例所示:

To enable graceful shutdown, configure the configprop:server.shutdown[] property, as shown in the following example:

server:
  shutdown: "graceful"

若要配置超时时间,请配置 configprop:spring.lifecycle.timeout-per-shutdown-phase[] 属性,如下例所示:

To configure the timeout period, configure the configprop:spring.lifecycle.timeout-per-shutdown-phase[] property, as shown in the following example:

spring:
  lifecycle:
    timeout-per-shutdown-phase: "20s"

如果您的 IDE 没有发送正确的 SIGTERM 信号,则使用正常关闭可能无法正常工作。有关更多详细信息,请参阅您的 IDE 的文档。

Using graceful shutdown with your IDE may not work properly if it does not send a proper SIGTERM signal. See the documentation of your IDE for more details.