Springboot Tomcat 单体项目如何实现快速jar包升级

同一个项目jar 启动由于项目端口一致导致启动失败,可以通过以下方式启动

大概思路:
1、 判断端口是否占用
2、占用则先通过其他端口启动
3、等待启动完毕后终止老进程
4、重新创建容器实例并且关联DispatcherServlet
在第三步和第四步之间,速度很快的,这样就达到了无缝更新代码的目的。

java 复制代码
@SpringBootApplication()
@EnableScheduling
public class WebMainApplication {
    public static void main(String[] args) {
        String[] newArgs = args.clone();
        int defaultPort = 8088;
        boolean needChangePort = false;
        if (isPortInUse(defaultPort)) {
            newArgs = new String[args.length + 1];
            System.arraycopy(args, 0, newArgs, 0, args.length);
            newArgs[newArgs.length - 1] = "--server.port=9090";
            needChangePort = true;
        }
        ConfigurableApplicationContext run = SpringApplication.run(WebMainApplication.class, newArgs);
        if (needChangePort) {
            String command = String.format("lsof -i :%d | grep LISTEN | awk '{print $2}' | xargs kill -9", defaultPort);
            try {
                Runtime.getRuntime().exec(new String[]{"sh", "-c", command}).waitFor();
                while (isPortInUse(defaultPort)) {
                }
                ServletWebServerFactory webServerFactory = getWebServerFactory(run);
                ((TomcatServletWebServerFactory) webServerFactory).setPort(defaultPort);
                WebServer webServer = webServerFactory.getWebServer(invokeSelfInitialize(((ServletWebServerApplicationContext) run)));
                webServer.start();

                ((ServletWebServerApplicationContext) run).getWebServer().stop();
            } catch (IOException | InterruptedException ignored) {
            }
        }

    }

    private static ServletContextInitializer invokeSelfInitialize(ServletWebServerApplicationContext context) {
        try {
            Method method = ServletWebServerApplicationContext.class.getDeclaredMethod("getSelfInitializer");
            method.setAccessible(true);
            return (ServletContextInitializer) method.invoke(context);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }

    }

    private static boolean isPortInUse(int port) {
        try (ServerSocket serverSocket = new ServerSocket(port)) {
            return false;
        } catch (IOException e) {
            return true;
        }
    }

    protected static Collection<ServletContextInitializer> getServletContextInitializerBeans(ConfigurableApplicationContext context) {
        return new ServletContextInitializerBeans(context.getBeanFactory());
    }


    private static ServletWebServerFactory getWebServerFactory(ConfigurableApplicationContext context) {
        String[] beanNames = context.getBeanFactory().getBeanNamesForType(ServletWebServerFactory.class);

        return context.getBeanFactory().getBean(beanNames[0], ServletWebServerFactory.class);
    }

}

具体逻辑想了解的可以访问文章原文链接 https://mp.weixin.qq.com/s/jaKN1YpvDycX_P13NAQNPw

相关推荐
瀚高PG实验室4 小时前
开发管理工具打不开No way to find ori gi nal streamhand er for jar protocol
java·数据库·jar·瀚高数据库
亦暖筑序4 小时前
Spring AI Alibaba 1.1.2 实战:5种多Agent编排模式完全指南
java·spring boot·ai编程
憧憬成为java架构高手的小白6 小时前
苍穹外卖--day10(订单状态定时处理、来单提醒和客户催单)
java·spring boot
invicinble6 小时前
springboot出现的原因二---作为web的后端服务一站式整合器
前端·spring boot·后端
zhz52146 小时前
Spring Boot + 腾讯 Kona 实现 TLCP 8443 国密 HTTPS 排障实录(奇安信浏览器已通)
spring boot·后端·https·国密·gmssl·kona
夕除7 小时前
spring boot 14
xml·spring boot·redis
是宇写的啊7 小时前
博客系统-小项目
java·数据库·spring boot·mybatis
韩小兔修媛史8 小时前
SpringBoot面试八股文(持续更新)
spring boot·后端·面试
Yana.nice8 小时前
nginx+tomcat环境下,nginx对后端Tomcat实例的健康检查机制
运维·nginx·tomcat
憧憬成为java架构高手的小白8 小时前
苍穹外卖--day11数据统计-图形报表(管理端)
java·spring boot·echarts