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


当前回答

您可以通过在您的配置(基于java或xml)中覆盖EmbeddedServletContainerFactory bean来指定端口。在这里,您可以为所使用的嵌入式servlet容器指定端口。请参阅Spring Boot - Core“嵌入式Servlet容器支持”段落和示例。希望这能有所帮助。

其他回答

在应用程序属性中添加一行

server.port = 8090

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

使用application.properties

添加server.port = 9898

在运行时配置

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

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

如果您使用gradle作为构建工具,您可以在应用程序中设置服务器端口。Yml文件为:

server:
  port: 8291

如果使用maven,则可以在应用程序中设置端口。属性文件为:

server.port: 8291

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

对于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);
        });
    }
}

将此添加到应用程序中。属性文件

server.port= 8080