如何配置Spring Boot应用程序侦听的TCP/IP端口,使其不使用默认端口8080。
当前回答
实际上,最简单的方法是设置服务器。端口属性。
如果你使用STS作为IDE,从3.6.7版本开始,你实际上有Spring Properties Editor来打开属性文件。
该编辑器为所有Spring Boot属性提供了自动补全功能。如果你写端口并按CTRL + SPACE,服务器。端口将是第一选择。
其他回答
由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
希望这篇文章能有所帮助
application.properties=> server.port=8090 application.yml=> server port:8090
在application.properties中包含下面的属性
server.port=8080
包括服务器端口在内的许多参数可以通过多种方式进行更改。然而,其优先顺序如下所示:
优先级分配给自定义代码,如下所示:
@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-web模块提供了一个嵌入式tomcat服务器,该服务器在端口号8080下运行。如果需要修改应用程序的端口号,请进入应用程序。属性文件,并使用server配置端口号。端口属性。
server.port= 9876
然后,您的应用程序在端口9876下运行。
推荐文章
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- Spring引导——不是托管类型
- Spring Boot YAML配置的字符串列表
- 使用Enum实现单例(Java)
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何在Java中按两个字段排序?
- 文件之间的差异。路径中的分隔符和斜杠
- 在方法参数中使用NotNull注释
- Spring MVC中处理可选参数的@RequestParam
- Tomcat:如何查找正在运行的Tomcat版本?
- “java”、“javaw”和“javaws”之间有什么区别?
- 将Date对象转换为日历对象