8
votes

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.

2

2 Answers

5
votes

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();
3
votes

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.