Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict Spring WebClient call at application level

I am using Spring WebFlux and WebClient for my web application.

My application potentially can call a 'N' number of other micro services which is also hosted by us.

Now the problem is that i want to restrict my WebClient to invoke a limited number of simultaneous calls to the existing micro services.

Also, I don't want to do it at individual call level, but at application level.

I have already gone through "How to limit the number of active Spring WebClient calls?" and "How to limit the request/second with WebClient?", to no avail.

like image 211
S Atah Ahmed Khan Avatar asked Dec 16 '18 05:12

S Atah Ahmed Khan


2 Answers

You can create a WebClient instance like this:

ConnectionProvider fixedPool = ConnectionProvider.fixed("fixedPool", maxConnections, acquireTimeout);
HttpClient httpClient = HttpClient.create(fixedPool);
WebClient webClient = WebClient.builder()
     .clientConnector(new ReactorClientHttpConnector(httpClient)).build();
like image 98
Brian Clozel Avatar answered Oct 16 '22 02:10

Brian Clozel


Since reactor-netty 0.9.5, the method described by Brian Clozel is now deprecated, use instead:

ConnectionProvider fixedPool = ConnectionProvider.builder("fixedPool")
        .maxConnections(200)
        .pendingAcquireTimeout(Duration.ofMinutes(3))
        .build();
HttpClient httpClient = HttpClient.create(fixedPool);
WebClient webClient = WebClient.builder()
        .clientConnector(new ReactorClientHttpConnector(httpClient))
        .build();

max connectiond and pending acquire timeout are random values, change them according to your needs.

like image 28
Ortomala Lokni Avatar answered Oct 16 '22 00:10

Ortomala Lokni