spring-boot - 如何在 Spring Boot 2 (w/WebFlux) 中配置 HTTP 和 HTTPS 的两个端口?

标签 spring-boot spring-webflux

谁能告诉我在使用 Spring Boot 2 和 WebFlux 时如何配置 2 个端口(用于 HTTP 和 HTTPS)?任何提示表示赞赏!

最佳答案

这个isn't directly supported by Spring Boot 2 yet .

但是,您可以通过多种方式让它发挥作用。

默认情况下,Spring Boot WebFlux 使用 Netty。如果您已经配置for ssl ,然后 Spring Boot 将启动并打开端口 8443(或您配置的任何端口)。

然后,要添加 8080,您可以执行以下操作:

@Autowired
HttpHandler httpHandler;

WebServer http;

@PostConstruct
public void start() {
    ReactiveWebServerFactory factory = new NettyReactiveWebServerFactory(8080);
    this.http = factory.getWebServer(this.httpHandler);
    this.http.start();
}

@PreDestroy
public void stop() {
    this.http.stop();
}

这有点笨拙,因为您的 https 配置位于一个位置 (application.yml),而您的 http 配置位于 Java 配置中,但我自己用一个玩具应用程序对此进行了测试。但不确定它的解决方案有多强大。

另一个可能有效的选项是尝试与其他建议等效的方法,但使用该类的响应式版本,即 TomcatReactiveWebServerFactory。我不知道有什么方法可以为其提供多个连接器,但您可以重写其 getWebServer 方法:

@Bean
TomcatReactiveWebServerFactory twoPorts() {
    return new TomcatReactiveWebServerFactory(8443) {
        @Override
        public WebServer getWebServer(HttpHandler httpHandler) {
           // .. copy lines from parent class
           // .. and add your own Connector, similar to how tutorials describe for servlet-based support
        }
    }
}

另外,有点困惑,我自己还没有尝试过这种方法。

当然,请跟踪the ticket这样您就知道 Spring Boot 2 何时提供官方支持。

关于spring-boot - 如何在 Spring Boot 2 (w/WebFlux) 中配置 HTTP 和 HTTPS 的两个端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48221194/

相关文章:

mongodb - 为什么在 Spring 响应式 Mongo 中订阅有效而阻止无效?

java - 如何在Spring WebClient中设置和处理超时?

spring - 基于 Spring Boot Hibernate JPA 的 DAO 的单元测试

java - 将 Spring Security 添加到 Spring Boot 应用程序导致异常

json - MockServer 和 OkHttp : body type missmatch (json vs string)

java - 在内存 DB 中的 H2 中插入日期时 DateTimeFormat 无效

java - 在Spring webflux Flux响应中将一个响应对象转换为另一个对象(pojo)而不订阅它

java - 在 @KafkaListener 注释方法中使用响应式(Reactive) webflux 代码

spring - 需要类型为 'org.springframework.security.authentication.AuthenticationManager' 的 bean,但无法找到。来自 Spring 安全的消息

spring - Reactor Mono 与 CompletableFuture