如何配置Spring Boot应用程序侦听的TCP/IP端口,使其不使用默认端口8080。
当前回答
您可以通过更改application.properties来更改服务器配置中的许多其他内容。 比如会话超时,地址和端口等。参考下文
裁判:http://docs.spring.io/spring-boot/docs/1.4.x/reference/html/common-application-properties.html
我使用了其中的几个,如下所示。
server.session.timeout=1
server.port = 3029
server.address= deepesh
其他回答
在应用中提供端口号。属性文件将解决该问题
server.port = 8080
“端口取决于您的选择,您希望将应用程序托管在哪里”
您可以在应用程序中配置端口。属性文件或应用程序。Yaml文件,在src/main/resources中。
server.port=8080
使用mvn shell命令行,spring-boot 2:
mvn spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8085'
包括服务器端口在内的许多参数可以通过多种方式进行更改。然而,其优先顺序如下所示:
优先级分配给自定义代码,如下所示:
@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());
}
}
在应用程序属性中添加一行
server.port = 8090
推荐文章
- 在流中使用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