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


当前回答

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

其他回答

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

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

使用属性服务器。例如,端口=8080,就像在其他答案中提到的那样,绝对是一种方法。只是想提一下,你也可以暴露一个环境属性:

SERVER_PORT=8080

因为在最近的版本中,spring boot能够替换“_”中的“。”,并将环境变量的小写改为大写。 这在容器中特别有用,在容器中,你所要做的就是定义环境变量,而不需要添加/编辑应用程序。属性或通过命令行传递系统属性(即-Dserver.port=$PORT)

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

server.port= 8080

Via

application.properties

服务器。端口= 8082(或任何新的端口号)

via

application.yml

server
  port: 8082

在文件应用程序。属性添加以下内容: server.port = 8888 这里需要经常提到的项目