简体   繁体   English

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

[英]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=9000spring.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:所以,我的问题是:

  • How do I get it to run on a port different from 8080 ?如何让它在不同于8080的端口上运行?
  • And, what could be causing the configuration files to not change the port?而且,什么可能导致配置文件不更改端口?

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.

相关问题 如何通过Spring Boot应用程序和Spock测试在运行时更改服务器端口 - How to change server port in runtime with a spring boot application and spock testing 使用 thymeleaf 从前端更改 Spring 的默认端口 - Change default Port of Spring Boot application from frontend with thymeleaf 如何使用Spring Boot应用程序更改嵌入式tomcat连接器端口 - How to change embedded tomcat connector port using spring boot application Spring Boot更改服务器端口 - Spring boot change server port 自定义 Spring 开机报错响应码不改默认正文 - Customize Spring Boot error response code without changing the default body Spring Boot-在不删除代码或不更改POM的情况下禁用嵌入式ElasticSearch - Spring Boot - Disable embedded ElasticSearch without removing code or changing POM 更改页面 spring 引导应用程序 - Changing pages spring boot application 用于 Spring MVC 的 Spring Boot Actuator,无需在另一个端口上启动 Spring - Spring Boot Actuator for Spring MVC without Spring boot on another port 通过Spring Boot和Vaadin传播代码更​​改,而无需在Eclipse中重新启动应用程序 - Propagate code changes by Spring Boot & Vaadin without restart the application in eclipse Spring启动应用程序使用的端口号 - Port number used by spring boot application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM