gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:16:01 24 4
gpt4 key购买 nike

谁能告诉我在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com