如何配置Spring Boot应用程序监听的TCP/IP端口,以便不使用默认的8080端口。
如何配置Spring Boot应用程序监听的TCP/IP端口,以便不使用默认的8080端口。
您还可以使用SERVER_PORT
环境变量来配置Spring Boot端口。只需设置环境变量并重新启动应用程序:
set SERVER_PORT=9999 // on windows machine
export SERVER_PORT=9999 // on linux
大多数情况下,SpringBoot运行在端口8080上,这是由于内嵌的Tomcat。有时候会抛出错误“端口8080已被占用”。为了避免这种问题,我们可以配置服务器端口。
添加 server.port=9898
用以下参数来运行您的应用程序。
spring-boot:run -Drun.jvmArguments='-Dserver.port=8081'
mvn spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8081'
,参见 https://dev59.com/vmEi5IYBdhLWcg3wptl_#66370832。 - asmaier''
,因此mvn spring-boot:run -Dspring-boot.run.jvmArguments=-Dserver.port=8081
是可行的。 - Manuel Jordan使用 mvn 命令行和 Spring Boot 2:
mvn spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8085'
打开您的Spring Boot应用程序中的application.properties文件。并在属性文件中添加以下属性。
server.port = 1443
这应该能正常工作,您可以根据您的希望设置任何端口号。
Gradle运行方式:
./gradlew bootRun
./gradlew bootRun --args='--server.port=8888'
application.properties
文件中有名为PORT
的变量,运行如下命令:PORT=8888 ./gradlew bootRun
Maven运行方式:
mvnw spring-boot:run
mvnw spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8085'
mvn spring-boot:run -Dspring-boot.run.arguments='--server.port=8085'
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8899 --your.custom.property=custom"
application.properties
文件中有名为PORT
的变量,运行如下命令:SERVER_PORT=9093 mvn spring-boot:run
使用java -jar
方式:
./gradlew clean build
,.jar文件在build/libs/
目录下。mvn clean install
,.jar文件在target
目录下。java -jar myApplication.jar
java -jar myApplication.jar --port=8888
java -jar -Dserver.port=8888 myApplication.jar
application.properties
文件中有名为SERVER_PORT
的变量,运行如下命令:SERVER_PORT=8888 java -jar target/myApplication.jar
server.port="port which you need"
通过
application.properties
server.port = 8082
(或任何新的端口号)
通过
application.yml
server
port: 8082
如果你想在命令环境中运行jar文件作为应用程序,只需在前面输入"SERVER_PORT=***"。完整的执行命令如下:
SERVER_PORT=8080 java -jar ***.jar
SERVER_PORT=8080 nohup java -jar ***.jar &
server.port=8080
。此配置位于org.springframework.boot.autoconfigure.web
下的ServerProperties.class
类中。 - Atif