How to use the Spring WebClient with Jetty, instead of Netty?

六月ゝ 毕业季﹏ 提交于 2019-12-23 19:37:47

问题


According to the documentation it is possible to use the Spring Reactive WebClient with a different server as 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.

However, I was not able to find a way how to do this. If I simply change the dependency from Netty to Jetty like this:

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'

my application will fail to start:

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

Obviously I need to do something more. But this github issue gives me the impression that WebClient cannot be used without Netty.

Is it possible to replace the Netty implementation of WebClient?


回答1:


Right now, in Spring Framework, WebClient has only one available ClientHttpConnector implementation, which is powered by Reactor Netty. This explains the current situation - using WebClient means you need Reactor Netty as a dependency.

Note that there's an existing issue about supporting Jetty Client as an alternative, see SPR-15092.




回答2:


Add dependency:

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

And then:

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

WebClient webClient = WebClient.builder().clientConnector(connector).build();

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



来源:https://stackoverflow.com/questions/47018963/how-to-use-the-spring-webclient-with-jetty-instead-of-netty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!