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


当前回答

您可以在应用程序中配置端口。属性文件或应用程序。Yaml文件,在src/main/resources中。

server.port=8080

其他回答

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

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

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

您可以在应用程序中配置端口。属性文件或应用程序。Yaml文件,在src/main/resources中。

server.port=8080

你可以在应用程序中设置。/src/main/resources/下的属性

server.port = 8090

通过编程方式,使用spring boot 2.1.5:

import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;

@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Override
    public void customize(ConfigurableServletWebServerFactory server) {
        server.setPort(9000);
    }

}

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

server.port= 8080