如何配置Spring Boot应用程序侦听的TCP/IP端口,使其不使用默认端口8080。
当前回答
服务器端口声明有两种类型
1.静态类型
server.port=8080. // your port number
动态类型 server.port = 0。//随机生成端口号。 server.port = ${端口:0}
其他回答
服务器端口声明有两种类型
1.静态类型
server.port=8080. // your port number
动态类型 server.port = 0。//随机生成端口号。 server.port = ${端口:0}
包括服务器端口在内的许多参数可以通过多种方式进行更改。然而,其优先顺序如下所示:
优先级分配给自定义代码,如下所示:
@Component
public class CustomConfiguration implements WebServerFactoryCustomizer {
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
factory.setPort(9090);
}
}
在这里,我们将服务器端口设置为9090,这是硬编码在代码中。为了避免硬编码,我们可以在bean类中使用@Value注释从环境中分配一个值,并在这里使用它。
第二优先级分配给命令行参数,如下所示: java -jar target/spring-boot-0.0.1-SNAPSHOT.jar——server.port=8092
这里我们告诉服务器从8092开始监听。注意,如果我们同时使用上述两种方法,它将忽略命令行参数,因为自定义代码被赋予了优先级。
Third precedence is assigned to OS environment variable. If none of the above two approaches is taken up, Spring will take server port from environment property. In case of deployment on Kubernetes, a property set under env section in Deployment yaml will be used. Fourth precedence is assigned to profile specific application.properties file. Fifth precedence is assigned to value assigned in application.properties file (which by default Spring Boot tries to find src/main/resources/config, if not found, then tries to find under src/main/resources).
第一种方法和第三种方法的结合是最容易管理和有用的方法。您可以使用环境属性并使用该自定义代码。
示例代码:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class EnvironmentCustomizer {
@Value("${server.port}")
private int serverPort;
public void setServerPort(int serverPort) {
this.serverPort = serverPort;
}
public int getServerPort() {
return serverPort;
}
}
@Configuration
public class CustomConfiguration
{
@Autowired
EnvironmentCustomizer envCustomizer;
@Bean
WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerPortCustomizer() {
return factory -> factory.setPort(envCustomizer.getServerPort());
}
}
此外,您还可以以编程方式配置端口。
对于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);
});
}
}
您还可以使用SERVER_PORT环境变量来配置Spring Boot端口。只需设置环境变量并重新启动应用程序:
set SERVER_PORT=9999 // on windows machine
export SERVER_PORT=9999 // on linux
请注意,如果你没有在系统范围内设置这些环境变量,你应该在同一个会话上运行引导应用程序。
由于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的转换由放松绑定完成。
推荐文章
- 如何格式化Joda-Time DateTime仅为mm/dd/yyyy?
- 如何在POM.xml中引用环境变量?
- 如何在android中复制一个文件?
- 将整数转换为字符串,以逗号表示千
- 接口方法的最终参数-有什么意义?
- Java中的@UniqueConstraint注释
- 如何在清洁模式下运行eclipse ?如果我们这样做会发生什么?
- 获取java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory异常
- Java中的正则表达式命名组
- c#和Java的主要区别是什么?
- 什么是NullPointerException,我如何修复它?
- 在Java中使用“final”修饰符
- 无法在Flutter上找到捆绑的Java版本
- 如何在Kotlin解析JSON ?
- 如何在新的材质主题中改变背面箭头的颜色?