网站首页 > 博客文章 正文
猿灯塔 希望对各位有帮助!
另外!文末有令你们心动的白嫖惊喜哦!
想要的朋友转发文章并私聊我哦!
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
public enum Shutdown {
GRACEFUL,
IMMEDIATE;
private Shutdown() {
}
}
如源码所示,默认为:IMMEDIATE,所以优雅关闭是大家根据业务需要手动开启的。
再来看第二个参数配置接收类:
org.springframework.boot.autoconfigure.context.LifecycleProperties
如源码所示,默认缓冲时间为:30 秒。
再看下优雅关闭的源码:
根据 Graceful 可以找到几个相关的类,我们进入 Tomcat 的:
org.springframework.boot.web.embedded.tomcat.GracefulShutdown
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 也会强制关闭应用。
所以需要注意的是,优雅关闭时一定要考虑当时的业务处理量,所设置的缓冲时间是否能处理完正在处理中的业务。
另外,Spring Boot 优雅关闭需要配合 Actuator 的 /shutdown 端点来进行触发,具体参考这篇文章:Spring Boot 优雅停止服务的几种方法。
感谢大家的观看,如果可以的话,
我是否有荣幸能得到你们的点赞与转发呢!
另外!小编这里有免费的JAVA干货资料以及面试资料
有需要的朋友私聊我噢
猜你喜欢
- 2024-10-29 jfinal vue ElementUI OA HRM的前后端分离CRM系统源码免费分享
- 2024-10-29 如何项目性能优化?压力测试,数据分析,代码实战
- 2024-10-29 Spring Boot 3 使用 Java 17 和 Jakarta EE 9,并支持GraalVM 的原生 Java
- 2024-10-29 走进Spring Boot源码学习之路和浅谈入门
- 2024-10-29 7 种提升 Spring Boot 吞吐量神技
- 2024-10-29 拥抱Kubernetes,再见了Spring Cloud
- 2024-10-29 美团动态线程池实践思路,开源了(美团 线程池)
- 2024-10-29 Spring Boot性能优化:提高响应速度的秘密
- 2024-10-29 Java 9 和Spring Boot 2.0纷纷宣布支持的HTTP/2到底是什么?
- 2024-10-29 字节二面:为什么SpringBoot的 jar可以直接运行?
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)