[英]Change the port of a Spring boot application without changing the code
I have two applications that I need to run simultaneously, and both are trying to run on port 8080
.我有两个需要同时运行的应用程序,并且都试图在端口
8080
上运行。 I'd like to change one of them to port 9000
.我想将其中一个更改为端口
9000
。 The application I'm trying to change has spring security, so it runs on port 8443
when using https
and port 8080
when using http
.我要更改的应用程序具有弹簧安全性,因此它在使用
https
时在端口8443
上运行,在使用http
时在端口8080
上运行。 I have to move it from port 8080
without changing any .java
files.我必须在不更改任何
.java
文件的情况下将其从端口8080
移出。 Also, I need to run the other application on port 8080
as well, so changing the default tomcat port wouldn't be a good idea.此外,我还需要在端口
8080
上运行其他应用程序,因此更改默认的 tomcat 端口不是一个好主意。
I tried adding to application.properties
the lines server.port=9000
, spring.main.server.port=9000
, then running mvn install
, and then java -jar target/app.jar
.我尝试向
application.properties
添加行server.port=9000
, spring.main.server.port=9000
,然后运行mvn install
,然后运行java -jar target/app.jar
。
I also tried running java -jar target/app.jar
with different flags: -Dserver.port=9000
and --server.port=9000
.我还尝试使用不同的标志运行
java -jar target/app.jar
: -Dserver.port=9000
和--server.port=9000
。
Regardless, I get - Tomcat started on port(s): 8443 (https) 8080 (http)
.无论如何,我得到
- Tomcat started on port(s): 8443 (https) 8080 (http)
。
So, my questions are:所以,我的问题是:
8080
?8080
的端口上运行? Run the following command: 运行以下命令:
mvn spring-boot:run -Drun.jvmArguments='-Dserver.port=8088'
Add the following plugin
to your pom.xml
file 将以下
plugin
添加到您的pom.xml
文件中
<build>
. . .
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
If you want to change spring boot default port without change any code you should pass port number in spring boot run command as如果您想在不更改任何代码的情况下更改 Spring Boot 默认端口,则应在 Spring Boot 运行命令中将端口号传递为
java -jar -Dserver.port=8090 hello-spring-boot.jar
Now your application will use 8090 port.现在您的应用程序将使用 8090 端口。
Reference : How to Change the default port in Spring Boot参考: 如何在 Spring Boot 中更改默认端口
In Other method we can add server.port property in application.properties file在其他方法中,我们可以在 application.properties 文件中添加 server.port 属性
server.port = 8090
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.