zoukankan      html  css  js  c++  java
  • 服务器spring boot版本,平滑升级

    1、在pom文件中加入:

    <!--平滑升级包 开始 -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--平滑升级包 结束 -->

    2、在spring配置文件中:

    #平滑关闭配置开始
    management.endpoint.shutdown.enabled=true
    management.endpoints.web.exposure.include=*
    #平滑关闭配置结束

    3、定制controller

    import org.apache.catalina.connector.Connector;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
    import org.springframework.context.ApplicationListener;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.event.ContextClosedEvent;

    import java.util.concurrent.Executor;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;

    /**
    * 定制的 Connector(tomcat处理器)
    *
    */
    @Configuration
    public class CustomShutdown implements TomcatConnectorCustomizer, ApplicationListener<ContextClosedEvent> {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final int TIMEOUT = 30;

    private volatile Connector connector;

    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
    this.connector.pause();
    Executor executor = this.connector.getProtocolHandler().getExecutor();
    if (executor instanceof ThreadPoolExecutor) {
    try {
    logger.warn("WEB 应用准备关闭");
    ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
    threadPoolExecutor.shutdown();
    if (!threadPoolExecutor.awaitTermination(TIMEOUT, TimeUnit.SECONDS)) {
    logger.warn("WEB 应用等待关闭超过最大时长 " + TIMEOUT + " 秒,将进行强制关闭!");
    threadPoolExecutor.shutdownNow();
    if (!threadPoolExecutor.awaitTermination(TIMEOUT, TimeUnit.SECONDS)) {
    logger.error("WEB 应用关闭失败!");
    }
    }
    } catch (InterruptedException ex) {
    Thread.currentThread().interrupt();
    }
    }
    }

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

    4、在spring启动类中加入

    @Bean
    public CustomShutdown customShutdown() {
    return new CustomShutdown();
    }

    /**
    * 将定制的添加到tomcat
    * @param customShutdown
    * @return
    */
    @Bean
    public ConfigurableServletWebServerFactory webServerFactory(CustomShutdown customShutdown) {
    TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory();
    tomcatServletWebServerFactory.addConnectorCustomizers(customShutdown);
    return tomcatServletWebServerFactory;
    }

    完成

  • 相关阅读:
    ant-design-vue——子组件通过$parent修改父组件的值时无效问题及解决方法
    vue——quill-editor自定义图片上传
    ES6——var、let、const三者的区别
    js——数组/对象常用方法总结
    28.最长回文子序列
    27.马拉车
    26.扫雷一次点击
    JS添加内容之方法里传AJAX参数
    JQ 实现加载其他页面的H5代码 JQ加载H5独立导航栏代码
    CentOS 7不能上网 解决方法
  • 原文地址:https://www.cnblogs.com/xwjBlog/p/11556121.html
Copyright © 2011-2022 走看看