![](/img/trans.png)
[英]How to change server port in runtime with a spring boot application and spock testing
[英]Change the port of a Spring boot application without changing the code
我有两个需要同时运行的应用程序,并且都试图在端口8080
上运行。 我想将其中一个更改为端口9000
。 我要更改的应用程序具有弹簧安全性,因此它在使用https
时在端口8443
上运行,在使用http
时在端口8080
上运行。 我必须在不更改任何.java
文件的情况下将其从端口8080
移出。 此外,我还需要在端口8080
上运行其他应用程序,因此更改默认的 tomcat 端口不是一个好主意。
我尝试向application.properties
添加行server.port=9000
, spring.main.server.port=9000
,然后运行mvn install
,然后运行java -jar target/app.jar
。
我还尝试使用不同的标志运行java -jar target/app.jar
: -Dserver.port=9000
和--server.port=9000
。
无论如何,我得到- Tomcat started on port(s): 8443 (https) 8080 (http)
。
所以,我的问题是:
8080
的端口上运行?运行以下命令:
mvn spring-boot:run -Drun.jvmArguments='-Dserver.port=8088'
将以下plugin
添加到您的pom.xml
文件中
<build>
. . .
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
如果您想在不更改任何代码的情况下更改 Spring Boot 默认端口,则应在 Spring Boot 运行命令中将端口号传递为
java -jar -Dserver.port=8090 hello-spring-boot.jar
现在您的应用程序将使用 8090 端口。
在其他方法中,我们可以在 application.properties 文件中添加 server.port 属性
server.port = 8090
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.