如何配置Spring Boot应用程序侦听的TCP/IP端口,使其不使用默认端口8080。


当前回答

您还可以使用SERVER_PORT环境变量来配置Spring Boot端口。只需设置环境变量并重新启动应用程序:

set SERVER_PORT=9999 // on windows machine
export SERVER_PORT=9999 // on linux

请注意,如果你没有在系统范围内设置这些环境变量,你应该在同一个会话上运行引导应用程序。

其他回答

有三种方法

1设置服务器。应用中的端口属性。属性文件

server.port = 8090

2在应用中设置服务器端口属性。yml文件

server:
     port: 8090

3在“main method”中将属性设置为系统属性

System.setProperty("server.port","8090");

大多数情况下,springboot运行在端口:8080上,因为使用了嵌入式Tomcat。在某些情况下,它可能抛出一个已经在使用的错误端口8080。为了避免这种问题,我们可以配置服务器端口。

使用application.properties

添加server.port = 9898

在运行时配置

使用以下参数运行应用程序。

spring-boot:跑-Drun.jvmArguments = ' -Dserver.port = 8081 '

此外,您还可以以编程方式配置端口。

对于Spring Boot 2.x.x:

@Configuration
public class CustomContainer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
  public void customize(ConfigurableServletWebServerFactory factory){
    factory.setPort(8042);
  }
}

对于旧版本:

@Configuration
public class ServletConfig {
    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return (container -> {
            container.setPort(8012);
        });
    }
}

正如大家所说,您可以在application.properties中指定 服务器。Port = 9000(可以是任何其他值) 如果您在项目中使用弹簧执行器,默认情况下它指向 8080,如果你想改变它,在应用程序中。属性提 管理。Port = 9001(可以是任何其他值)

您还可以使用SERVER_PORT环境变量来配置Spring Boot端口。只需设置环境变量并重新启动应用程序:

set SERVER_PORT=9999 // on windows machine
export SERVER_PORT=9999 // on linux

请注意,如果你没有在系统范围内设置这些环境变量,你应该在同一个会话上运行引导应用程序。