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


当前回答

正如大家所说,您可以在application.properties中指定 服务器。Port = 9000(可以是任何其他值) 如果您在项目中使用弹簧执行器,默认情况下它指向 8080,如果你想改变它,在应用程序中。属性提 管理。Port = 9001(可以是任何其他值)

其他回答

由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

如果端口号可以是随机的,则可以在应用程序中使用随机函数。属性server.port = $ {random.int (4)}

将此添加到应用程序中。属性文件

server.port= 8080

在Spring引导应用程序中,有两种主要方法可以更改嵌入式Tomcat中的端口。

修改application.properties

首先,您可以尝试应用程序。属性文件在/resources文件夹:

server.port = 8090

修改虚拟机选项

第二种方法,如果你想避免修改任何文件和检入你只需要在你的本地文件,你可以使用vm arg:

执行—>编辑配置—>虚拟机选项

-Dserver.port=8090

此外,如果您需要更多信息,可以查看下面的博客文章:在Spring引导应用程序上更改端口

如文档中所说,要么设置服务器。端口作为系统属性使用命令行选项jvm -Dserver。端口=8090或添加应用程序。/src/main/resources/ with中的属性

server.port=8090

随机使用端口:

server.port=0

类似地,添加应用程序。Yml /src/main/resources/

server:
  port: 8090