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


当前回答

如果你正在使用spring命令行接口(CLI),使用——将命令与spring命令参数分开,来更改端口:

春天跑你好。Groovy -- --server.port=9000

spring-boot cli

其他回答

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

通过编程方式,使用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环境变量来配置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");

您可以通过更改application.properties来更改服务器配置中的许多其他内容。 比如会话超时,地址和端口等。参考下文

裁判:http://docs.spring.io/spring-boot/docs/1.4.x/reference/html/common-application-properties.html

我使用了其中的几个,如下所示。

server.session.timeout=1
server.port = 3029
server.address= deepesh