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


当前回答

在应用程序属性中添加一行

server.port = 8090

其他回答

使用属性服务器。例如,端口=8080,就像在其他答案中提到的那样,绝对是一种方法。只是想提一下,你也可以暴露一个环境属性:

SERVER_PORT=8080

因为在最近的版本中,spring boot能够替换“_”中的“。”,并将环境变量的小写改为大写。 这在容器中特别有用,在容器中,你所要做的就是定义环境变量,而不需要添加/编辑应用程序。属性或通过命令行传递系统属性(即-Dserver.port=$PORT)

服务器端口声明有两种类型

1.静态类型

   server.port=8080. // your port number

动态类型 server.port = 0。//随机生成端口号。 server.port = ${端口:0}

由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

您可以通过在您的配置(基于java或xml)中覆盖EmbeddedServletContainerFactory bean来指定端口。在这里,您可以为所使用的嵌入式servlet容器指定端口。请参阅Spring Boot - Core“嵌入式Servlet容器支持”段落和示例。希望这能有所帮助。

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

SERVER_PORT=8080 java -jar ***.jar

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

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