繁体   English   中英

在不更改代码的情况下更改 Spring Boot 应用程序的端口

[英]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=9000spring.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 端口。

参考: 如何在 Spring Boot 中更改默认端口

在其他方法中,我们可以在 application.properties 文件中添加 server.port 属性

    server.port = 8090

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM