Spring Boot 2.0 实现优雅停机
发布于: 2021 年 04 月 20 日
前期踩的坑 (Spring Boot 1.x)
1. 添加 mavne 依赖
<!-- springboot监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
复制代码
2. 启用 shutdown
在配置文件里添加下面的配置
#启用shutdown endpoint的HTTP访问
endpoints.shutdown.enabled=true
#不需要验证
endpoints.shutdown.sensitive=false
复制代码
启动的时候可以看到下面的日志,就说明成功了
3. 优雅停机
发送 POST 请求 http://localhost:8080/shutdown
如果响应码是 404 可以尝试 POST http://localhost:8080/actuator/shutdown
Spring Boot 2.0
如果你使用的 spring boot 版本是 2.x 的就会发现,这些 POST 请求都会出现 404 的结果。
下面是 Spring Boot 2.0 优雅停机的实现方式。
1.修改 Application 启动类
tomcat 容器
@SpringBootApplication
public class ShutdownApplication {
public static void main(String[] args) {
SpringApplication.run(ShutdownApplication.class, args);
}
/**
* 用于接受 shutdown 事件
*/
@Bean
public GracefulShutdown gracefulShutdown() {
return new GracefulShutdown();
}
/**
* 配置tomcat
*
* @return
*/
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addConnectorCustomizers(gracefulShutdown());
return tomcat;
}
/**
* 优雅关闭 Spring Boot。容器必须是 tomcat
*/
private class GracefulShutdown implements TomcatConnectorCustomizer, ApplicationListener<ContextClosedEvent> {
private final Logger log = LoggerFactory.getLogger(GracefulShutdown.class);
private volatile Connector connector;
private final int waitTime = 10;
@Override
public void customize(Connector connector) {
this.connector = connector;
}
@Override
public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
this.connector.pause();
Executor executor = this.connector.getProtocolHandler().getExecutor();
if (executor instanceof ThreadPoolExecutor) {
try {
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
threadPoolExecutor.shutdown();
if (!threadPoolExecutor.awaitTermination(waitTime, TimeUnit.SECONDS)) {
log.warn("Tomcat 进程在" + waitTime + " 秒内无法结束,尝试强制结束");
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}
}
复制代码
Undertow 容器 (没有使用过,不保证可用)
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
/**
* 优雅关闭 Spring Boot
*/
@Component
public class GracefulShutdown implements ApplicationListener<ContextClosedEvent> {
@Autowired
private GracefulShutdownWrapper gracefulShutdownWrapper;
@Autowired
private ServletWebServerApplicationContext context;
@Override
public void onApplicationEvent(ContextClosedEvent contextClosedEvent){
gracefulShutdownWrapper.getGracefulShutdownHandler().shutdown();
try {
UndertowServletWebServer webServer = (UndertowServletWebServer)context.getWebServer();
Field field = webServer.getClass().getDeclaredField("undertow");
field.setAccessible(true);
Undertow undertow = (Undertow) field.get(webServer);
List<Undertow.ListenerInfo> listenerInfo = undertow.getListenerInfo();
Undertow.ListenerInfo listener = listenerInfo.get(0);
ConnectorStatistics connectorStatistics = listener.getConnectorStatistics();
while (connectorStatistics.getActiveConnections() > 0){}
}catch (Exception e){
// Application Shutdown
}
}
}
@Component
public class GracefulShutdownWrapper implements HandlerWrapper{
private GracefulShutdownHandler gracefulShutdownHandler;
@Override
public HttpHandler wrap(HttpHandler handler) {
if(gracefulShutdownHandler == null) {
this.gracefulShutdownHandler = new GracefulShutdownHandler(handler);
}
return gracefulShutdownHandler;
}
public GracefulShutdownHandler getGracefulShutdownHandler() {
return gracefulShutdownHandler;
}
}
@Component
@AllArgsConstructor
public class UndertowExtraConfiguration {
private final GracefulShutdownWrapper gracefulShutdownWrapper;
@Bean
public UndertowServletWebServerFactory servletWebServerFactory() {
UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
factory.addDeploymentInfoCustomizers(deploymentInfo -> deploymentInfo.addOuterHandlerChainWrapper(gracefulShutdownWrapper));
factory.addBuilderCustomizers(builder -> builder.setServerOption(UndertowOptions.ENABLE_STATISTICS, true));
return factory;
}
}
}
复制代码
2. 使用 kill 命令杀死进程
使用下面的命令杀死进程。该命令是向 某个进程发送终止信号。
kill -15 [PID]
复制代码
划线
评论
复制
发布于: 2021 年 04 月 20 日阅读数: 17
版权声明: 本文为 InfoQ 作者【U+2647】的原创文章。
原文链接:【http://xie.infoq.cn/article/350e1c8c31fa7398116835920】。文章转载请联系作者。
U+2647
关注
evolving code monkey 2018.11.05 加入
https://zdran.com/
评论