【问题标题】:Spring Boot Multiple Ports?Spring Boot 多个端口?
【发布时间】:2019-01-23 08:59:57
【问题描述】:

如何让 Spring Boot Web 应用程序在多个端口上运行? 例如 8080 和 80
我怎样才能做到这一点?
application.properties

server.port=8080, 80

【问题讨论】:

  • 一个(spring boot)应用程序只监听一个端口。如果您想让 Spring Boot 应用程序监听 2 个端口,请使用 server.port=8080 启动 2 个端口,另一个在 server.port=80 上启动,即单独的 application.properties 文件。
  • another thread讨论

标签: spring spring-boot port application.properties


【解决方案1】:

您可以添加侦听器,而不是运行多个应用程序。例如,如果您使用 undertow :

@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

【讨论】:

猜你喜欢
  • 2018-05-13
  • 1970-01-01
  • 2018-03-18
  • 1970-01-01
  • 2023-03-25
  • 2016-07-21
  • 2016-11-08
  • 1970-01-01
  • 2015-09-17
相关资源
最近更新 更多