首先来介绍下什么是优雅地停止,简而言之,就是对应用进程发送停止指令之后,能保证正在执行的业务操作不受影响,可以继续完成已有请求的处理,但是停止接受新请求。
在 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。
然后需要在 application.yml 中添加一些配置来启用优雅停止的功能:
Copy
开启优雅停止 Web 容器,默认为 IMMEDIATE:立即停止
server:
shutdown: graceful
最大等待时间
spring:
lifecycle:
timeout-per-shutdown-phase: 30s
其中,平滑关闭内置的 Web 容器(以 Tomcat 为例)的入口代码在 org.springframework.boot.web.embedded.tomcat 的 GracefulShutdown 里,大概逻辑就是先停止外部的所有新请求,然后再处理关闭前收到的请求,有兴趣的可以自己去看下。
内嵌的 Tomcat 容器平滑关闭的配置已经完成了,那么如何优雅关闭 Spring 容器了,就需要 Actuator 来实现 Spring 容器的关闭了。
然后加入 actuator 依赖,依赖如下所示:
Copy
org.springframework.boot
spring-boot-starter-actuator
然后接着再添加一些配置来暴露 actuator 的 shutdown 接口:
Copy
暴露 shutdown 接口
management:
endpoint:
shutdown:
enabled: true
endpoints:
web:
exposure:
include: shutdown
其中通过 Actuator 关闭 Spring 容器的入口代码在 org.springframework.boot.actuate.context 包下 ShutdownEndpoint 类中,主要的就是执行 doClose() 方法关闭并销毁 applicationContext,有兴趣的可以自己去看下。
配置搞定后,然后在 controller 包下创建一个 WorkController 类,并有一个 work 方法,用来模拟复杂业务耗时处理流程,具体代码如下:
Copy
@RestController
public class WorkController {
@GetMapping("/work")
public String work() throws InterruptedException {
// 模拟复杂业务耗时处理流程
Thread.sleep(10 * 1000L);
return "success";
}
}
然后,我们启动项目,先用 Postman 请求 http://localhost:8080/work 处理业务:
然后在这个时候,调用 http://localhost:8080/actuator/shutdown 就可以执行优雅地停止,返回结果如下:
Copy
{
“message”: “Shutting down, bye…”
}
如果在这个时候,发起新的请求 http://localhost:8080/work,会没有反应:
再回头看第一个请求,返回了结果:success。
其中有几条服务日志如下:
Copy
2020-05-20 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-20 23:05:15.287 INFO 102724 — [tomcat-shutdown] o.s.b.w.e.tomcat.GracefulShutdown : Graceful shutdown complete
2020-05-20 23:05:15.295 INFO 102724 — [ Thread-253] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService ‘applicationTaskExecutor’
从日志中也可以看出来,当调用 shutdown 接口的时候,会先等待请求处理完毕后再优雅地停止。
到此为止,Spring Boot 2.3 的优雅关闭就讲解完了,是不是很简单呢?如果是在之前不支持优雅关闭的版本如何去做呢?
Spring Boot 旧版本优雅停止#
在这里介绍 GitHub 上 issue 里 Spring Boot 开发者提供的一种方案:
选取的 Spring Boot 版本为 2.2.6.RELEASE,首先要实现 TomcatConnectorCustomizer 接口,该接口是自定义 Connector 的回调接口:
Copy
@FunctionalInterface
public interface TomcatConnectorCustomizer {
void customize(Connector connector);
}
除了定制 Connector 的行为,还要实现 ApplicationListener 接口,因为要监听 Spring 容器的关闭事件,即当前的 ApplicationContext 执行 close() 方法,这样我们就可以在请求处理完毕后进行 Tomcat 线程池的关闭,具体的实现代码如下:
Copy
@Bean
public GracefulShutdown gracefulShutdown() {
return new GracefulShutdown();
}
private static class GracefulShutdown implements TomcatConnectorCustomizer, ApplicationListener {
private static final Logger log = LoggerFactory.getLogger(GracefulShutdown.class);
private volatile Connector connector;
@Override
public void customize(Connector connector) {
this.connector = connector;
}
@Override
public void onApplicationEvent(ContextClosedEvent event) {
this.connector.pause();
Executor executor = this.connector.getProtocolHandler().getExecutor();
if (executor instanceof ThreadPoolExecutor) {
try {
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
threadPoolExecutor.shutdown();
if (!threadPoolExecutor.awaitTermination(30, TimeUnit.SECONDS)) {
log.warn("Tomcat thread pool did not shut down gracefully within 30 seconds. Proceeding with forceful shutdown");
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}
有了定制的 Connector 回调,还需要在启动过程中添加到内嵌的 Tomcat 容器中,然后等待监听到关闭指令时执行,addConnectorCustomizers 方法可以把定制的 Connector 行为添加到内嵌的 Tomcat 中,具体代码如下:
Copy
@Bean
public ConfigurableServletWebServerFactory tomcatCustomizer() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addConnectorCustomizers(gracefulShutdown());
return factory;
}
到此为止,内置的 Tomcat 容器平滑关闭的操作就完成了,Spring 容器优雅停止上面已经说过了,再次就不再赘述了。
通过测试,同样可以达到上面那样优雅停止的效果。
总结
本文主要讲解了 Spring Boot 2.3 版本和旧版本的优雅停止,避免强制停止导致正在处理的业务逻辑会被中断,进而导致产生业务异常的情形。
另外使用 Actuator 的同时要注意安全问题,比如可以通过引入 security 依赖,打开安全限制并进行身份验证,设置单独的 Actuator 管理端口并配置只对内网开放等。
最好的关系就是互相成就,大家的在看、转发、留言三连就是我创作的最大动力。
https://giphy.com/channel/1kk3tg
https://giphy.com/channel/7jcqzk
https://giphy.com/channel/s01ktp
https://giphy.com/channel/yd77hm
https://giphy.com/channel/y6attc
https://giphy.com/channel/x38xi1
https://giphy.com/channel/g9ue0o
https://giphy.com/channel/6zszl1
https://giphy.com/channel/sbqkdm
https://giphy.com/channel/z0zgpf
https://giphy.com/channel/zizrrf
https://giphy.com/channel/kv99jd
https://giphy.com/channel/lvi2ul
https://giphy.com/channel/tvcdw4
https://giphy.com/channel/xry4i0
https://giphy.com/channel/2zt62j
https://giphy.com/channel/jgowyg
https://giphy.com/channel/98v9hx
https://giphy.com/channel/ef32cw
https://giphy.com/channel/94ai05
https://giphy.com/channel/lrtska
https://giphy.com/channel/4zwphp
https://giphy.com/channel/xorpwx
https://giphy.com/channel/8jhsr0
https://giphy.com/channel/fp3p62
https://giphy.com/channel/fmluol
https://giphy.com/channel/950l6h
https://giphy.com/channel/j73p2y
https://giphy.com/channel/mnulcc
https://giphy.com/channel/m9s1nj
https://giphy.com/channel/t1s5c3
https://giphy.com/channel/372py5
https://giphy.com/channel/29xm7g
https://giphy.com/channel/pl3low
https://giphy.com/channel/i62i8u
https://giphy.com/channel/chppqb
https://giphy.com/channel/thd5tk
https://giphy.com/channel/aruk07
https://giphy.com/channel/7vh1p9
https://giphy.com/channel/e0b41l
https://giphy.com/channel/hcqcyc
https://giphy.com/channel/1irkdu
https://giphy.com/channel/wyarjq
https://giphy.com/channel/0l4mo3
https://giphy.com/channel/16cdsd
https://giphy.com/channel/9nnywf
https://giphy.com/channel/y9r026
https://giphy.com/channel/160v4u
https://giphy.com/channel/udw49v
https://giphy.com/channel/84i4r4
https://giphy.com/channel/4q1j3j
https://giphy.com/channel/rfgyy7
https://giphy.com/channel/5bs4x7
https://giphy.com/channel/si8zs7
https://giphy.com/channel/i9dm26
https://giphy.com/channel/gw7o3q
https://giphy.com/channel/7ri7b4
https://giphy.com/channel/tah3cz
https://giphy.com/channel/tcsjb7
https://giphy.com/channel/qr8yt2
https://giphy.com/channel/ffu4l7
https://giphy.com/channel/3v2f9h
https://giphy.com/channel/34lhng
https://giphy.com/channel/bszsay
https://giphy.com/channel/3pxhqn
https://giphy.com/channel/zzo9h7
https://giphy.com/channel/35ngxh
https://giphy.com/channel/x06oio
https://giphy.com/channel/l95lf5
https://giphy.com/channel/ikaq1r
https://giphy.com/channel/kmu5kk
https://giphy.com/channel/nwqy75
https://giphy.com/channel/l6bbt4
https://giphy.com/channel/n2v1m7
https://giphy.com/channel/oww3pe
https://giphy.com/channel/d61fh0
https://giphy.com/channel/o63n6g
https://giphy.com/channel/8x8xf6
https://giphy.com/channel/92apjx
https://giphy.com/channel/yqj5yt
https://giphy.com/channel/7a7qct
https://giphy.com/channel/nunf4v
https://giphy.com/channel/1i663s
https://giphy.com/channel/gqkti5
https://giphy.com/channel/6o5p24
https://giphy.com/channel/bb8r7s
https://giphy.com/channel/tt5z34
https://giphy.com/channel/k22kxx
https://giphy.com/channel/1fph2v
https://giphy.com/channel/709um6
https://giphy.com/channel/1d6skv
https://giphy.com/channel/009zpx
https://giphy.com/channel/kudo7v
https://giphy.com/channel/r1bas8
https://giphy.com/channel/cm43gu
https://giphy.com/channel/rj18l3
https://giphy.com/channel/dr92bs
https://giphy.com/channel/k5tcqt
https://giphy.com/channel/ra14q0
https://giphy.com/channel/8m610g
https://giphy.com/channel/i9v54i
https://giphy.com/channel/ppdvly
https://giphy.com/channel/60hgrx
https://giphy.com/channel/z1zjah
https://giphy.com/channel/qxs81i
https://giphy.com/channel/p6zsth
https://giphy.com/channel/n9l36k
https://giphy.com/channel/8yxmle
https://giphy.com/channel/ir51fn
https://giphy.com/channel/2dizat
https://giphy.com/channel/qhgigq
https://giphy.com/channel/qqah0g
https://giphy.com/channel/64h6mw
https://giphy.com/channel/093g41
https://giphy.com/channel/ckv54x
https://giphy.com/channel/5te5ed
https://giphy.com/channel/hzb1g7
https://giphy.com/channel/k94mc9
https://giphy.com/channel/w290l1
https://giphy.com/channel/mmkles
https://giphy.com/channel/7ektja
https://giphy.com/channel/b82l2a
https://giphy.com/channel/rg34j6
https://giphy.com/channel/h4q6wh
https://giphy.com/channel/uhkrur
https://giphy.com/channel/40ve0k
https://giphy.com/channel/sbjblj
https://giphy.com/channel/evu55t
https://giphy.com/channel/m9seia
https://giphy.com/channel/2hxnnp
https://giphy.com/channel/i2zv54
https://giphy.com/channel/44bme0
https://giphy.com/channel/h720p5
https://giphy.com/channel/v66ow7
https://giphy.com/channel/68qypy
https://giphy.com/channel/i0rid1
https://giphy.com/channel/6ya4yp
https://giphy.com/channel/08eqyh
https://giphy.com/channel/960zro
https://giphy.com/channel/2bbck1
https://giphy.com/channel/pgtbjy
https://giphy.com/channel/z8hgzq
https://giphy.com/channel/knmv99
https://giphy.com/channel/d8nt1l
https://giphy.com/channel/q18x6q
https://giphy.com/channel/9o6z0h
https://giphy.com/channel/ss8m74
https://giphy.com/channel/41dewl
https://giphy.com/channel/yrfgzj
https://giphy.com/channel/ihiqsb
https://giphy.com/channel/0md6dt
https://giphy.com/channel/8k7b5c
https://giphy.com/channel/1i82h6
https://giphy.com/channel/bt9kyj
https://giphy.com/channel/w6owf5
https://giphy.com/channel/2l52kl
https://giphy.com/channel/6l1t67
https://giphy.com/channel/63ew2y
https://giphy.com/channel/7l9w1x
https://giphy.com/channel/at9bms
https://giphy.com/channel/jz72zb
https://giphy.com/channel/au14c4
https://giphy.com/channel/0g8nhx
https://giphy.com/channel/s8h6yg
https://giphy.com/channel/5vocc1
https://giphy.com/channel/nxw0e2
https://giphy.com/channel/fgvpdh
https://giphy.com/channel/3ipyio
https://giphy.com/channel/jc9w06
https://giphy.com/channel/huop1o
https://giphy.com/channel/fni70e
https://giphy.com/channel/7ww7wl
https://giphy.com/channel/fe033g
https://giphy.com/channel/e3u688
https://giphy.com/channel/0p9a7a
https://giphy.com/channel/u3t8sb
https://giphy.com/channel/7zqq5l
https://giphy.com/channel/d879zr
https://giphy.com/channel/qxso4y
https://giphy.com/channel/gexuv7
https://giphy.com/channel/dl0nn2
https://giphy.com/channel/010rau
https://giphy.com/channel/kn83w6
https://giphy.com/channel/q9n1z9
https://giphy.com/channel/xw9gnh
https://giphy.com/channel/sdde1v
https://giphy.com/channel/t6jka6
https://giphy.com/channel/0idslk
https://giphy.com/channel/ncvo4w
https://giphy.com/channel/8ltlc2
https://giphy.com/channel/pxwxxu
https://giphy.com/channel/5176kl
https://giphy.com/channel/p3x239
https://giphy.com/channel/s2rl83
https://giphy.com/channel/5uarr8
https://giphy.com/channel/frhg2p
https://giphy.com/channel/nc2gl5
https://giphy.com/channel/7vun3p
https://giphy.com/channel/7ws9k3
https://giphy.com/channel/q9a27o
https://giphy.com/channel/17h8p4
https://giphy.com/channel/o08ung
https://giphy.com/channel/s3rsi4
https://giphy.com/channel/3a7031
https://giphy.com/channel/1xhfaw
https://giphy.com/channel/7lmwkt
https://giphy.com/channel/mkskwc
https://giphy.com/channel/idd9q7
https://giphy.com/channel/rksris
https://giphy.com/channel/ssbltc
https://giphy.com/channel/9gg0f8
https://giphy.com/channel/iiy8yc
https://giphy.com/channel/bu9rcd
https://giphy.com/channel/s3b9l4
https://giphy.com/channel/02b2ki
https://giphy.com/channel/92f21e
https://giphy.com/channel/8y22l3
https://giphy.com/channel/0r0zb3
https://giphy.com/channel/n9hpmw
https://giphy.com/channel/ne8563
https://giphy.com/channel/80smft
https://giphy.com/channel/acty01
https://giphy.com/channel/p3uup7
https://giphy.com/channel/ih0cac
https://giphy.com/channel/21zh27
https://giphy.com/channel/seb0wv
https://giphy.com/channel/i93qat
https://giphy.com/channel/xoq41p
https://giphy.com/channel/js7s2h
https://giphy.com/channel/j147s7
https://giphy.com/channel/yxgqy0
https://giphy.com/channel/v33vvg
https://giphy.com/channel/w8y9mg
https://giphy.com/channel/h1530y
https://giphy.com/channel/tl9n41
https://giphy.com/channel/s5qq46
https://giphy.com/channel/s5uveo
https://giphy.com/channel/d15p0g
https://giphy.com/channel/akkrmc
https://giphy.com/channel/aterrc
https://giphy.com/channel/yd4088
https://giphy.com/channel/qx4f2s
https://giphy.com/channel/ks5bev
https://giphy.com/channel/0nsj9j
https://giphy.com/channel/rh4x97
https://giphy.com/channel/emdkve
https://giphy.com/channel/s2ppzy
https://giphy.com/channel/qp5ggg
https://giphy.com/channel/1jqhsy
https://giphy.com/channel/6v83vv