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


当前回答

希望这篇文章能有所帮助

application.properties=> 

server.port=8090

application.yml=> 

server
  port:8090

其他回答

在我的情况下添加声明

server.port=${port:8081}

覆盖默认的tomcat服务器端口。

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

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

@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());
    }
}

除了在申请时添加端口。属性,通过将属性文件方法与Spring配置文件相结合,您还可以轻松地实现针对不同环境的多个端口。具体来说,我们可以为每个环境创建一个属性文件。

例如,我们将有一个应用程序开发。包含以下内容的属性文件:

server.port=8081

然后您可以添加另一个应用程序-qa。属性文件使用不同的端口:

server.port=8082

只需设置环境变量SERVER_PORT。 (这些例子适用于Linux)

使用java -jar启动: SERVER_PORT=9093 java -jar target/eric-sc-dummy.jar 通过maven spring-boot插件启动: SERVER_PORT=9093 mvn spring-boot:run

小贴士:

如果你在java -jar或mvn命令之前添加了其他子命令,那么你需要在一个单独的命令中添加export来设置env,并通过;将它们分开,以确保它对子进程可用。 例句: 出口SERVER_PORT = 9093;export MAVEN_OPTS="-Xmx256m -Xms64m";运行mvn spring-boot:

这招对我很管用:

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

@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);
    }
}

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