问题
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