如何使用 Jetty 而不是 Netty 的 Spring WebClient?
How to use the Spring WebClient with Jetty, instead of Netty?
根据 documentation 可以使用 Spring Reactive WebClient 与不同的服务器作为 Netty:
WebClient provides a higher level API over HTTP client libraries. By
default it uses Reactor Netty but that is pluggable with a different
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 框架中,WebClient
只有一个可用的 ClientHttpConnector
实现,它由 Reactor Netty 提供支持。这解释了当前的情况——使用 WebClient
意味着你需要 Reactor Netty 作为依赖项。
请注意,支持 Jetty Client 作为替代方案存在一个问题,请参阅 SPR-15092。
添加依赖:
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();
根据 documentation 可以使用 Spring Reactive WebClient 与不同的服务器作为 Netty:
WebClient provides a higher level API over HTTP client libraries. By default it uses Reactor Netty but that is pluggable with a different 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 框架中,WebClient
只有一个可用的 ClientHttpConnector
实现,它由 Reactor Netty 提供支持。这解释了当前的情况——使用 WebClient
意味着你需要 Reactor Netty 作为依赖项。
请注意,支持 Jetty Client 作为替代方案存在一个问题,请参阅 SPR-15092。
添加依赖:
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();