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


当前回答

您可以通过更改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

其他回答

您可以在应用程序中配置端口。spring引导项目的资源文件夹中的属性文件。

server.port="port which you need"

您可以在下面的方法中添加端口。

执行->配置命令 在application.xml中添加server.port=XXXX

默认情况下,spring启动应用程序从默认端口8080启动嵌入式tomcat服务器。Spring为您提供了以下不同的自定义,您可以从中选择一种。

说明—您可以使用服务器。Port =0 spring引导将找到任何未分配的HTTP随机端口 对我们来说。

1) application.properties

server.port=2020

2) application.yml

server:  
     port : 2020

3)通过编程方式更改服务器端口

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

@Component
public class MyTomcatWebServerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

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

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

@Component
public class CustomizationBean implements EmbeddedServletContainerCustomizer {

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

4)使用命令行选项

 java -jar spring-boot-app.jar -Dserver.port=2020

由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

这个问题是Gradle Spring Port的第一个结果。

如果你使用gradle,你可以这样做,如果你有Spring Boot gradle插件已经应用:

bootRun {
    args += ["--server.port=[PORT]"]
}

想知道更复杂的答案,请看我的回答。