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


当前回答

由于Spring Boot提供了各种配置外部化机制(通过各种PropertySource实现和/或按顺序连接到Environment对象的处理器),您可以通过以下方法设置jar存档之外的任何属性:

Pass property through command line argument as application argument java -jar <path/to/my/jar> --server.port=7788 From property in SPRING_APPLICATION_JSON (Spring Boot 1.3.0+) Define environment variable in U*IX shell: SPRING_APPLICATION_JSON='{"server.port":7788}' java -jar <path/to/my/jar> By using Java system property: java -Dspring.application.json='{"server.port":7788}' -jar <path/to/my/jar> Pass through command line argument: java -jar <path/to/my/jar> --spring.application.json='{"server.port":7788}' Define JVM system property java -Dserver.port=7788 -jar <path/to/my/jar> Define OS environment variable U*IX Shell SERVER_PORT=7788 java -jar <path/to/my/jar> Windows SET SERVER_PORT=7788 java -jar <path/to/my/jar> Place property in ./config/application.properties configuration file server.port=7788 and run: java -jar <path/to/my/jar> Place property in ./config/application.yaml server: port: 7788 and run: java -jar <path/to/my/jar> Place property in ./application.properties server.port=7788 and run: java -jar <path/to/my/jar> Place property in ./application.yaml server: port: 7788 and run: java -jar <path/to/my/jar>


您可以将上述方法组合在一起,列表中的前一个配置优先于后一个配置。

例如:

SERVER_PORT=2266 java -Dserver.port=5566 -jar <path/to/my/jar> --server.port=7788

服务器将在端口7788上启动并监听。

这非常有用,在PropertySources中提供优先级较低的默认属性(通常打包在存档中或编码在源代码中),然后在运行时环境中覆盖它。这就是Spring Boot的设计理念:

坚持己见,但当需求开始偏离默认值时,要迅速离开。


SERVER_NAME到server.name的转换由放松绑定完成。

其他回答

如果你打算在命令环境中以jar文件的形式运行应用程序,只需输入“SERVER_PORT=***”作为前缀。要执行的完整命令如下所示:

SERVER_PORT=8080 java -jar ***.jar

如果你想在Linux下后台运行应用程序,使用'nohup'命令将如下所示:

SERVER_PORT=8080 nohup java -jar ***.jar &

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

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

Spring-web模块默认提供了一个运行在端口号8080上的嵌入式tomcat服务器。

您可以修改如下-

A)如果你使用gradle,那么可以在你的应用程序中设置属性。yml:

 server:  
      port: 8042

B)如果你正在使用maven,那么你可以在你的应用程序中设置属性。属性:

server.port: 8042

C)当你在自己的配置文件中有端口,并想在运行时设置它。

通过实现WebServerFactoryCustomizer接口- Spring 2.x

@Component
public class MyTomcatWebServerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        // customize the factory here
        factory.setPort(8042);
    }
}

通过实现EmbeddedServletContainerCustomizer接口- Spring 1.x

@Component
public class CustomizationBean implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        // customize here
        container.setPort(8042);
    }
}

如果您使用gradle作为构建工具,您可以在应用程序中设置服务器端口。Yml文件为:

server:
  port: 8291

如果使用maven,则可以在应用程序中设置端口。属性文件为:

server.port: 8291

由Gradle运行:

在默认端口(8080)运行:./gradlew bootRun 运行在提供的端口(8888):./gradlew bootRun——args='——server.port=8888' 如果应用中有变量。PORT=8888 ./gradlew bootRun . properties文件

由Maven运行:

Run in default port(8080): mvnw spring-boot:run Run in provided port(8888): mvnw spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8085' Run in provided port(8888): mvn spring-boot:run -Dspring-boot.run.arguments='--server.port=8085' Run in provided port(8888) with other custom property: mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8899 --your.custom.property=custom" If we have any variable in the application.properties file named PORT, run this: SERVER_PORT=9093 mvn spring-boot:run

使用java -jar:

Create the .jar file: For Gradle: ./gradlew clean build. We will find the jar file inside: build/libs/ folder. For Maven: mvn clean install. We will find the jar file inside:target folder. Run in default port(8080): java -jar myApplication. jar Run in provided port(8888): java -jar myApplication.jar --port=8888 Run in provided port(8888): java -jar -Dserver.port=8888 myApplication.jar Run in provided port(8888) having variable SERVER_PORT in application.properties file: SERVER_PORT=8888 java -jar target/myApplication.jar