【问题标题】:How to use the Spring WebClient with Jetty, instead of Netty?如何将 Spring WebClient 与 Jetty 一起使用,而不是 Netty?
【发布时间】:2018-04-11 15:38:39
【问题描述】:

根据documentation,可以将 Spring Reactive WebClient 与 Netty 不同的服务器一起使用:

WebClient 通过 HTTP 客户端库提供更高级别的 API。经过 默认它使用 Reactor Netty,但可以使用不同的插件 ClientHttpConnector。

但是,我无法找到一种方法来做到这一点。如果我只是像这样将依赖项从 Netty 更改为 Jetty:

compile('org.springframework.boot:spring-boot-starter-webflux') {
       exclude group: 'org.springframework.boot', module: 'spring-boot-starter-reactor-netty'
}
compile group: 'org.springframework.boot', name: 'spring-boot-starter-jetty', version: '2.0.0.M5'

我的应用程序将无法启动:

2017-10-30 15:40:43.328 ERROR 20298 --- [  restartedMain] o.s.boot.SpringApplication               : Application startup failed

java.lang.NoClassDefFoundError: reactor/ipc/netty/http/client/HttpClient

显然我需要做更多的事情。但是这个github issue给我的印象是没有Netty就不能使用WebClient。

是否可以替代WebClient的Netty实现?

【问题讨论】:

    标签: spring-boot jetty netty spring-webflux


    【解决方案1】:

    添加依赖:

    org.eclipse.jetty:jetty-reactive-httpclient:1.0.3
    

    然后:

    HttpClient httpClient = new HttpClient();
    ClientHttpConnector connector = new JettyClientHttpConnector(httpClient);
    
    WebClient webClient = WebClient.builder().clientConnector(connector).build();
    

    来源:https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-client-builder-jetty

    【讨论】:

      【解决方案2】:

      目前,在 Spring Framework 中,WebClient 只有一个可用的 ClientHttpConnector 实现,它由 Reactor Netty 提供支持。这解释了当前的情况 - 使用 WebClient 意味着您需要 Reactor Netty 作为依赖项。

      请注意,支持 Jetty Client 作为替代方案存在一个问题,请参阅SPR-15092

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-08
        • 1970-01-01
        • 2015-05-08
        • 1970-01-01
        • 2012-01-28
        • 2019-11-09
        • 2012-04-22
        相关资源
        最近更新 更多