![c09e5871b762c9181ccd703751720875.png](https://img-blog.csdnimg.cn/img_convert/c09e5871b762c9181ccd703751720875.png)
Spring Boot 2.3有个新特性叫:Graceful shutdown(优雅关闭)
下面是官方的说明:
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. When a grace period is configured, upon shutdown, the web server will no longer permit new requests and will wait for up to the grace period for active requests to complete.
优雅关闭支持所有 4 个嵌入式 Web 服务器:Jetty, Reactor Netty, Tomcat, Undertow,以及响应式的和基于 Servlet 的 Web 应用程序。
当配置了一个优雅关闭的缓冲时间,直到应用程序关闭时,Web 服务器都不再允许接收新的请求,缓冲时间是为了等待目前所有进行中的活动请求处理完成。
需要说明的是,Tomcat、Jetty 在网络层会立即停止接收请求,而 Undertow 可以继续接收请求,但会立即返回 503 服务不可用错误。
怎么开启优雅关闭?
下面是 Yaml 文件的配置示例:
# 开启优雅关闭
server:
shutdown: graceful
# 关闭的缓冲时间
spring:
lifecycle:
timeout-per-shutdown-phase: 10s
源码分析
上面介绍了优雅关闭参数的配置方式,下面我们通过源码来看下默认的配置是什么。
先看第一个参数配置接收类:
org.springframework.boot.autoconfigure.web.ServerProperties
![9daeb13e4f57a96ff71f6be0722fab8c.png](https://img-blog.csdnimg.cn/img_convert/9daeb13e4f57a96ff71f6be0722fab8c.png)
public enum Shutdown {
GRACEFUL,
IMMEDIATE;
private Shutdown() {
}
}
如源码所示,默认为:IMMEDIATE,所以优雅关闭是大家根据业务需要手动开启的。
再来看第二个参数配置接收类:
org.springframework.boot.autoconfigure.context.LifecycleProperties
![19767acfb821cfe076f6fe4102e201e3.png](https://img-blog.csdnimg.cn/img_convert/19767acfb821cfe076f6fe4102e201e3.png)
如源码所示,默认缓冲时间为:30 秒。
再看下优雅关闭的源码:
![5a0693abf72b7a97d832a1018fd19e21.png](https://img-blog.csdnimg.cn/img_convert/5a0693abf72b7a97d832a1018fd19e21.png)
根据 Graceful 可以找到几个相关的类,我们进入 Tomcat 的:
org.springframework.boot.web.embedded.tomcat.GracefulShutdown
![d689bf553c7fcb3397c8ca5a2d0399ed.png](https://img-blog.csdnimg.cn/img_convert/d689bf553c7fcb3397c8ca5a2d0399ed.png)
public enum GracefulShutdownResult {
/**
* Requests remained active at the end of the grace period.
*/
REQUESTS_ACTIVE,
/**
* The server was idle with no active requests at the end of the grace period.
*/
IDLE,
/**
* The server was shutdown immediately, ignoring any active requests.
*/
IMMEDIATE;
}
REQUESTS_ACTIVE 说的是在缓冲期结束前连接保持活动状态,也就是虽然官方默认给你 30 秒的缓存时间来处理囤积请求,如果 30 秒之后还没处理完成,最后 Spring Boot 也会强制关闭应用。
所以需要注意的是,优雅关闭时一定要考虑当时的业务处理量,所设置的缓冲时间是否能处理完正在处理中的业务。
更文不易,大家的点赞与支持就是我更文最大的动力!
点击下方领取我送给大家的福利哦!
java初学者到精通到架构师技术www.cnblogs.com