3

I have read in the spring documentation https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-concurrency-model that when using reactor netty for client and server the event loop resources are shared, thats mean that when i create more than one instance of webclient the event loop resources also are shared? If i want to communicate with different APIs from my code should i create multiple Webclient instances or should i share the same instance?

joliva
  • 330
  • 3
  • 10

1 Answers1

3

Since WebClient instances are immutable you have no other option than to create for different API's different WebClient's using its built-in builders.

You can copy existing WebClient configurations using mutate() method or create a bare new instance.

See also https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/web-reactive.html#webflux-client and Right way to use Spring WebClient in multi-thread environment

Selim Ok
  • 1,141
  • 1
  • 7
  • 21
  • Thanks Selim, just a quick question, if a create different webclients for different "host" this will automatically share the underlaying netty resources? i mean, the event loop and threads, or this will create a new one for this webclient? – joliva Nov 08 '18 at 12:57
  • According to documentation if you use WebClient.Builder to create WebClient instances spring boot uses the same ClientHttpConnector for each one. https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-webclient.html "Spring Boot creates and pre-configures a WebClient.Builder for you; it is strongly advised to inject it in your components and use it to create WebClient instances. Spring Boot is configuring that builder to share HTTP resources," – Selim Ok Nov 08 '18 at 13:08
  • Can't you just not set the baseurl and that way use one webclient? – obesechicken13 Nov 07 '22 at 15:17