如何配置Spring Boot应用程序侦听的TCP/IP端口,使其不使用默认端口8080。
当前回答
由于Spring Boot提供了各种配置外部化机制(通过各种PropertySource实现和/或按顺序连接到Environment对象的处理器),您可以通过以下方法设置jar存档之外的任何属性:
Pass property through command line argument as application argument java -jar <path/to/my/jar> --server.port=7788 From property in SPRING_APPLICATION_JSON (Spring Boot 1.3.0+) Define environment variable in U*IX shell: SPRING_APPLICATION_JSON='{"server.port":7788}' java -jar <path/to/my/jar> By using Java system property: java -Dspring.application.json='{"server.port":7788}' -jar <path/to/my/jar> Pass through command line argument: java -jar <path/to/my/jar> --spring.application.json='{"server.port":7788}' Define JVM system property java -Dserver.port=7788 -jar <path/to/my/jar> Define OS environment variable U*IX Shell SERVER_PORT=7788 java -jar <path/to/my/jar> Windows SET SERVER_PORT=7788 java -jar <path/to/my/jar> Place property in ./config/application.properties configuration file server.port=7788 and run: java -jar <path/to/my/jar> Place property in ./config/application.yaml server: port: 7788 and run: java -jar <path/to/my/jar> Place property in ./application.properties server.port=7788 and run: java -jar <path/to/my/jar> Place property in ./application.yaml server: port: 7788 and run: java -jar <path/to/my/jar>
您可以将上述方法组合在一起,列表中的前一个配置优先于后一个配置。
例如:
SERVER_PORT=2266 java -Dserver.port=5566 -jar <path/to/my/jar> --server.port=7788
服务器将在端口7788上启动并监听。
这非常有用,在PropertySources中提供优先级较低的默认属性(通常打包在存档中或编码在源代码中),然后在运行时环境中覆盖它。这就是Spring Boot的设计理念:
坚持己见,但当需求开始偏离默认值时,要迅速离开。
SERVER_NAME到server.name的转换由放松绑定完成。
其他回答
服务器端口声明有两种类型
1.静态类型
server.port=8080. // your port number
动态类型 server.port = 0。//随机生成端口号。 server.port = ${端口:0}
此外,您还可以以编程方式配置端口。
对于Spring Boot 2.x.x:
@Configuration
public class CustomContainer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
public void customize(ConfigurableServletWebServerFactory factory){
factory.setPort(8042);
}
}
对于旧版本:
@Configuration
public class ServletConfig {
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return (container -> {
container.setPort(8012);
});
}
}
正如Spring文档中解释的那样,有几种方法可以做到这一点:
您可以在命令行中设置端口(例如8888)
-Dserver。Port =8888或——server.port=8888
例如:java -jar -Dserver。端口= 8888 test.jar
或者在application.properties中设置端口
server.port=${port:4588}
或(在申请中)。Yml和yaml语法)
server:
port: ${port:4588}
如果在命令行中设置了-Dport(或-Dserver.port)传递的端口,则将考虑该端口。如果不是,则端口默认为4588。
如果你想在属性文件中强制端口,不管环境变量是什么,你只需要写:
server.port=8888
您可以在应用程序中配置端口。属性文件或应用程序。Yaml文件,在src/main/resources中。
server.port=8080
服务器。对于随机端口,Port = 0
服务器。端口= 8080为自定义8080端口
推荐文章
- for循环和for-each循环在性能上有区别吗?
- 你如何比较两个版本的字符串在Java?
- 为什么在Java和。net中不能修改字符串?
- java.util.Random真的那么随机吗?我怎么能生成52!(阶乘)可能的序列?
- ZoneOffset之间的区别是什么。UTC和ZoneId.of(“UTC”)?
- 类未找到:IntelliJ中的空测试套件
- 将JAR文件添加到Spark作业- Spark -submit
- REST API - dto还是不是?
- Spring MVC中的ApplicationContext和WebApplicationContext有什么区别?
- JavaFX应用程序图标
- Java:强/软/弱/幻影引用的区别
- 在序列化和反序列化期间JSON属性的不同名称
- 获取Android设备名称
- Gradle代理配置
- 如何获得具有已知资源名称的资源id ?