如何将 Spring WebClient 与 Jetty 一起使用,而不是 Netty?

Mar*_*dik 6 jetty netty spring-boot spring-webflux

根据文档,可以将 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'
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

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

rgr*_*ski 7

添加依赖:

org.eclipse.jetty:jetty-reactive-httpclient:1.0.3
Run Code Online (Sandbox Code Playgroud)

进而:

HttpClient httpClient = new HttpClient();
ClientHttpConnector connector = new JettyClientHttpConnector(httpClient);

WebClient webClient = WebClient.builder().clientConnector(connector).build();
Run Code Online (Sandbox Code Playgroud)

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


Bri*_*zel 3

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

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