如何在多个端口上运行Spring Boot Web应用程序?
例如8080和80
如何实现呢?
application.properties
server.port=8080, 80
答案 0 :(得分:3)
您可以添加侦听器,而不是运行多个应用程序。例如,如果您使用Undertown:
@Configuration
public class PortConfig {
@Value("${server.http.port}")
private int httpPort;
@Bean
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
@Override
public void customize(Undertow.Builder builder) {
builder.addHttpListener(httpPort, "0.0.0.0");
}
});
return factory;
}
}
我已经用它来监听HTTP端口和https端口。
对于Tomcat,您会发现相同类型的配置: https://docs.spring.io/spring-boot/docs/1.2.1.RELEASE/api/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.html