当前位置:   article > 正文

服务之优雅停机_shutdown immediate graceful

shutdown immediate graceful

1 服务停机

服务优雅停机分运维层面和应用层面,在这里主要讲一下应用层面的优雅停机,这里讲到的实现方式和案例均是在springboot项目下。

2 优雅停机的三种方式

2.1 spring-boot 2.3.1以上版本

springboot支持应用的优雅停机,当应用接收到kill的信号(15 终止号令)时,会停止接收新的请求,并等待活跃的请求完成后,关闭服务,通过yml文件配置:

server:
  shutdown: graceful   //IMMEDIATE立即停机(默认值)、GRACEFUL 
  • 1
  • 2

shutdown配置介绍:

GRACEFUL:should support graceful shutdown, allowing active requests time to complete.
IMMEDIATE:should shut down immediately.

但是需要注意的事,该种方式只对web请求所产生的线程有效,应用内新建立的线程无效(只要活跃的请求完成,即使应用内有线程存活,依然会立即关闭)。

2.2 tomcat方式

优雅停机效果同springboot2.3.1.

@Component
@Slf4j
public class GracefulShutdownTomcat implements TomcatConnectorCustomizer, ApplicationListener<ContextClosedEvent> {

    private volatile Connector connector;
    private final int waitTime = 30;

    @Override
    public void customize(Connector connector) {
        this.connector = connector;
    }

    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        this.connector.pause();
        log.info("active Thread count: {}", Thread.activeCount());
        Executor executor = this.connector.getProtocolHandler().getExecutor();
        if (executor instanceof ThreadPoolExecutor) {
            ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor)executor;
            threadPoolExecutor.shutdown();
            try {
                if (!threadPoolExecutor.awaitTermination(waitTime, TimeUnit.SECONDS)) {
                    log.warn("Tomcat thread pool did't shut down gracefully within" + waitTime + " seconds. Proceeding with forceful shutdown");
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }

    @Bean
    public ConfigurableServletWebServerFactory servletContainer(final GracefulShutdownTomcat gracefulShutdownTomcat) {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addConnectorCustomizers(gracefulShutdownTomcat);
        return tomcat;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

3 jvm

通过jvm的钩子监控kill信号,该种方式对应用内建立的线程池有效(会在设定的最大等待时间达到之前,一直等待线程执行完成,或到达最大时间停机),对web请求产生的线程暂时未找到等待方式,不过web用户请求一般都由运维在k8s和nginx层面进行路由处理。
另外需要注意终止方法调用的最大等待时间,要根据程序设计,提供一个合理的值,避免线程池中存在未执行完成的线程。

@Configuration
public class ShutdownHook {

    public ShutdownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            ThreadPoolFactory.shutdown();
            try {
                ThreadPoolFactory.awaitTermination(30, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/767542
推荐阅读
相关标签
  

闽ICP备14008679号