如何在 Spring Boot 2 (w/WebFlux) 中为 HTTP 和 HTTPS 配置两个端口? [英] How to configure in Spring Boot 2 (w/ WebFlux) two ports for HTTP and HTTPS?

查看:71
本文介绍了如何在 Spring Boot 2 (w/WebFlux) 中为 HTTP 和 HTTPS 配置两个端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can anybody tell me how 2 ports (for HTTP and HTTPS) can be configured when using Spring Boot 2 and WebFlux? Any hint is appreciated!

推荐答案

This isnSpring Boot 2 尚不直接支持.

但是,您可以通过多种方式使其工作.

But, you may be able to get it to work in a couple of ways.

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

By default, Spring Boot WebFlux uses Netty. If you are already configured for ssl, then Spring Boot will start up and open port 8443 (or whatever you have configured).

然后,要添加 8080,您可以:

Then, to add 8080, you can do:

@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 配置中,但我已经用一个玩具应用程序自己测试过.不过,不确定它的解决方案有多强大.

Which is a bit clunky since your https configuration is in one spot (application.yml) and your http configuration is in Java config, but I have tested this myself with a toy application. Not sure how robust of a solution it is, though.

另一个可行的选择是尝试与其他建议等效的方法,但使用该类的反应版本,即 TomcatReactiveWebServerFactory.我不知道有什么方法可以为它提供多个连接器,但您可以覆盖它的 getWebServer 方法:

Another option that may work is to try the equivalent of other suggestions, but use the reactive version of the class, which is TomcatReactiveWebServerFactory. I'm not aware of any way to provide more than one connector for it, but you could possibly override its getWebServer method:

@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
        }
    }
}

另外,有点乱,我自己也没有尝试过这种方法.

Also, a bit messy, and I have not tried that approach myself.

当然,请跟踪,以便您知道何时Spring Boot 2 提供官方支持.

Of course, keep track of the ticket so you know when Spring Boot 2 provides official support.

这篇关于如何在 Spring Boot 2 (w/WebFlux) 中为 HTTP 和 HTTPS 配置两个端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆