首页 > 解决方案 > 是否可以使用不同的端口两次实例化相同的 spring-boot 应用程序?

问题描述

我需要使用嵌入式 tomcat 两次使用不同的端口实例化相同的 spring boot 应用程序,因此这两个实例会相互消耗其他 api。目标是模拟两个独立程序,其逻辑与两个玩家相同。

我尝试了以下方法:

        SpringApplicationBuilder playerOne = new SpringApplicationBuilder(GameApplication.class)
                .properties("server.port:" + "8080", "app.name:" + "Player1", "spring.jmx.enable:false");

        SpringApplicationBuilder playerTwo = new SpringApplicationBuilder(GameApplication.class)
                .properties("server.port:" + "8081", "app.name:" + "Player2", "spring.jmx.enable:false");

        ConfigurableApplicationContext run1 = playerOne.run();
        ConfigurableApplicationContext run2 = playerTwo.run();

我得到了 javax.management.InstanceAlreadyExistsException

怎么可能做到这一点?它不应该启动两个不同的服务器吗?

标签: javaspringspring-boot

解决方案


运行 jar 并将端口作为输入参数..

然后您可以使用两个已知端口启动两个应用程序。

java -jar myServer.jar --server.port=8080

java -jar myServer.jar --server.port=8081

推荐阅读