如何配置Spring Boot应用程序侦听的TCP/IP端口,使其不使用默认端口8080。
当前回答
在应用中提供端口号。属性文件将解决该问题
server.port = 8080
“端口取决于您的选择,您希望将应用程序托管在哪里”
其他回答
您还可以使用SERVER_PORT环境变量来配置Spring Boot端口。只需设置环境变量并重新启动应用程序:
set SERVER_PORT=9999 // on windows machine
export SERVER_PORT=9999 // on linux
请注意,如果你没有在系统范围内设置这些环境变量,你应该在同一个会话上运行引导应用程序。
由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
默认情况下,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
如果你打算在命令环境中以jar文件的形式运行应用程序,只需输入“SERVER_PORT=***”作为前缀。要执行的完整命令如下所示:
SERVER_PORT=8080 java -jar ***.jar
如果你想在Linux下后台运行应用程序,使用'nohup'命令将如下所示:
SERVER_PORT=8080 nohup java -jar ***.jar &
1.1通过属性文件更新。
/ src / main /资源/ application.properties
server.port=8888
通过yaml文件更新。
server:
port: 8888
EmbeddedServletContainerCustomizer
@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(8888);
}
}
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何找到哪个程序正在使用端口80在Windows?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder