java 平滑升级_服务器spring boot版本,平滑升级

1、在pom文件中加入:

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-actuator

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 {

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;

}

完成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值