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


当前回答

这招对我很管用:

增加了一个自定义容器类:

@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
       configurableEmbeddedServletContainer.setPort(8888);
    }

}

但这仍然没有使用端口8888。

然后我设置“scanBasePackages”属性就像这样在“@SpringBootApplication”类上我的主要方法:(scanBasePackages ={"自定义容器包"})

@SpringBootApplication(scanBasePackages = {"com.javabrains.util"})
public class CourseApiApp {

    public static void main (String args []) {
        SpringApplication.run(CourseApiApp.class, args);
    }
}

它开始拾取自定义容器中的端口集。

其他回答

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

使用application.properties

添加server.port = 9898

在运行时配置

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

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

根据所使用的应用程序配置文件,有三种方法可以做到这一点

a)如果您正在使用应用程序。属性文件集

server.port = 8090

b)如果您正在使用应用程序。yml文件设置YAML格式的服务器端口属性,如下所示

server:
     port: 8090

c)也可以在main方法中将属性设置为System属性

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

如果你想在本地运行它,使用这个-

mvn spring-boot:run - dserver . jvmarguments ='-Dserver.port=8085'

从Spring Boot 2.0开始,下面的命令是有效的(线索在这里):

mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8085

包括服务器端口在内的许多参数可以通过多种方式进行更改。然而,其优先顺序如下所示:

优先级分配给自定义代码,如下所示:

@Component public class CustomConfiguration implements WebServerFactoryCustomizer { @Override public void customize(ConfigurableServletWebServerFactory factory) { factory.setPort(9090); } }

在这里,我们将服务器端口设置为9090,这是硬编码在代码中。为了避免硬编码,我们可以在bean类中使用@Value注释从环境中分配一个值,并在这里使用它。

第二优先级分配给命令行参数,如下所示: java -jar target/spring-boot-0.0.1-SNAPSHOT.jar——server.port=8092

这里我们告诉服务器从8092开始监听。注意,如果我们同时使用上述两种方法,它将忽略命令行参数,因为自定义代码被赋予了优先级。

Third precedence is assigned to OS environment variable. If none of the above two approaches is taken up, Spring will take server port from environment property. In case of deployment on Kubernetes, a property set under env section in Deployment yaml will be used. Fourth precedence is assigned to profile specific application.properties file. Fifth precedence is assigned to value assigned in application.properties file (which by default Spring Boot tries to find src/main/resources/config, if not found, then tries to find under src/main/resources).

第一种方法和第三种方法的结合是最容易管理和有用的方法。您可以使用环境属性并使用该自定义代码。

示例代码:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class EnvironmentCustomizer {
    @Value("${server.port}")
    private int serverPort;
    public void setServerPort(int serverPort) {
        this.serverPort = serverPort;
    }
    public int getServerPort() {
        return serverPort;
    }
}
@Configuration
public class CustomConfiguration
{
    @Autowired
    EnvironmentCustomizer envCustomizer;
    @Bean
    WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerPortCustomizer() {
    return factory -> factory.setPort(envCustomizer.getServerPort());
    }
}

当你需要一种编程的方式来做它,你可以在启动时设置它:

System.getProperties().put( "server.port", 80 );
SpringApplication.run(App.class, args);

这可能对依赖于环境的端口有帮助。 祝你有愉快的一天