java - 是否可以使用不同的端口两次实例化相同的 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
怎么可能做到这一点?它不应该启动两个不同的服务器吗?
解决方案
运行 jar 并将端口作为输入参数..
然后您可以使用两个已知端口启动两个应用程序。
java -jar myServer.jar --server.port=8080
java -jar myServer.jar --server.port=8081
推荐阅读
- python - 为 python 包安装数据包
- c++ - 每个元素的字符串分配
- ios - 如何快速本地化我的多个字符串?
- javascript - 收到错误“TypeError:events.listenForEvents 不是函数”
- c# - .Net Core 3.1 - 找不到方法:Microsoft.VisualBasic.Interaction.Environ(System.String)
- php - Laravel 8如何命名一个组
- mysql - OFFSET x LIMIT y 搞乱了行的顺序
- javascript - 无法读取 HTMLDivElement 处未定义的属性“id”。
- python - pip install dlib 在 Anaconda 环境中不起作用
- python - 如何将我的“打印内容”添加到文本文件中