首页 > 其他分享 >动态重启更换端口

动态重启更换端口

时间:2024-08-12 12:42:19浏览次数:8  
标签:return String 重启 端口 static context new newArgs 更换

原文:https://mp.weixin.qq.com/s/Hz0gStUXe40FNiuY7rudhA

一种简单办法是,新代码先用其他端口启动,启动完毕后,更改nginx的转发地址,nginx重启非常快,这样就避免了大量的用户访问失败,最后终止老进程就可以。

新思路:

  1. 判断端口是否占用
  2. 占用则先通过其他端口启动
  3. 等待启动完毕后终止老进程
  4. 重新创建容器实例并且关联DispatcherServlet

 

@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);
    }

}

 

标签:return,String,重启,端口,static,context,new,newArgs,更换
From: https://www.cnblogs.com/wujf/p/18327238

相关文章

  • 使用DEVCON工具来禁用和启用USB端口的基本指南;DEVCON工具的高级使用方法,帮助你进行更
    DEVCON(DeviceConsole)是一个命令行工具,用于管理设备驱动程序和设备的状态。要禁用USB端口,可以使用DEVCON工具执行相关操作。以下是一个初级使用教程的大纲,帮助你了解如何使用DEVCON禁用USB端口:DEVCON禁用USB端口初级使用教程大纲1. 介绍与准备什么是DEVCON?介绍DEVCON工具......
  • 通过cmd查找端口占用,并杀死进程
    windows通过cmd查找端口占用1.查看端口占用情况netstat-ano 2.查看某个端口被占用情况netstat-ano|findstrxxxx//xxxx为查询的端口号 3.查看某个状态的端口netstat-ano|findstrxxxx//xxxx为查询的端口状态 具体端口状态请查找后面描述......
  • 主干网络篇 | YOLOv10 更换主干网络之 VanillaNet |《华为方舟实验室最新成果》
    论文地址:https://arxiv.org/pdf/2305.12972.pdf代码地址:https://github.com/huawei-noah/VanillaNet在基础模型的核心是“多样性即不同”,这一哲学在计算机视觉和自然语言处理方面取得了惊人的成功。然而,优化和Transformer模型固有的复杂性带来了挑战,需要转向简洁性的范式......
  • 源是什么、怎么换源、Centos7停止维护后如何更换源
    目录源的介绍与更换1.什么是软件源?2.为什么要更换软件源?2.1提高下载速度:2.2获得最新的软件版本:2.3解决源不可用或不稳定的问题:2.4获取特定的软件包:2.5系统兼容性和需求:3.如何更换软件源:Ubuntu和CentOS的步骤为例3.1Ubuntu3.1.1备份当前的源列表3.1.2编辑源列表文件......
  • 自动化运维-《日常运维手册接入手册》之 服务器端口表
    前言:脚本编写不是一蹴而就,需要根据当前场景和实际需求不断测试和调整,最重要的是勇于踏出敢于尝试的这一步。任务需求:在ansible-vault加密环境下,使用root权限批量将项目上所有服务器的tcp端口过滤掉22,25,53,443,631,9100,10050端口后显示出来;判断服务器上如果有jps命令的就执行......
  • 解决uwsgi -ini uwsgi.ini 重启服务无效果
    更新了前端代码,重启服务无效果(只针对个别服务器)具体log日志yourprocessesnumberlimitis95696yourmemorypagesizeis4096bytes***WARNING:youhaveenabledharakiriwithoutpostbuffering.Slowuploadcouldberejectedonpost-unbufferedwebservers***de......
  • 宝塔配置域名反向代理到ip+端口的接口地址
    宝塔配置域名反向代理到ip+端口的接口地址添加站点配置域名配置反向代理目标URL写服务器的本地ip地址127.0.0.1代理到9000端口上发关域名也写127.0.0.1或者$host申请https证书访问接口https://abc.baidu.com/xxx/xxx......
  • 解决端口号被占用问题
    第一种:最简单有效的方法,重启一下电脑,占用此端口的程序就会释放端口。第二种:使用命令找到占用端口的程序,把它关闭。1、打开运行窗口输入:CMD,进入命令窗口。2、输入:netstat–ano就会列出所有使用端口的信息,A列就是端口数值,B列为使用当前端口对应的程序PID。指定端口号......
  • 解决端口号占用问题:Spring Boot报错,Web server failed to start. Port 8080 was alrea
    报错信息:Webserverfailedtostart.Port8080wasalreadyinuse.报错原因:端口被占用解决方法:解决方法一:修改端口修改配置文件,加上参数:server.port=8014或者在application.yml文件中添加server:port:8014在访问时,替换对应的端口号即可解决方法二:关闭占用端口的......
  • yolov5更换主干网络shufflent
    目录1.网络结构解析1.1创建yolov5s_shufflent_v2_X0_5.yaml文件2.对common.py末尾进行添加 3.修改yolo.py1.网络结构解析1.可以先看看shufflenet_v2的网络结构importtorchfromtorchimportnnfromtorchvisionimportmodelsfromtorchinfoimportsummaryc......