Rate limit web client - spring-boot

I am building a microservice using spring webflux and netty. Internally I use web client to make rest api calls. How can I control rate at which I can call rest api via webclient? I guess backnpressure is only for a single request/reply and does not work across multiple request to my microservice. Amy pointers will be appreciated.Thanks.

Resilience4j has support for non-blocking rate limiting with Reactor.
See: https://resilience4j.readme.io/docs/examples-1#decorate-mono-or-flux-with-a-ratelimiter

Related

Spring Boot Reactive WebService

We want to build a rest API to service high request volumes. I'm planning to build it using Spring Reactive(WebFlux) or using Spring Boot Async. We have multiple different clients who will be invoking our service.
Do I need to worry about different clients who will be consuming this service? Meaning if I build the API using Reactive or Async, will all the clients be able to consume this seemlessly?
Meaning if build a reactive Rest API, will the client using RestTemplate be able to consume or do they need to use WebClient only?
Yes, your (not non blocking) clients will still be able to consume a reactive service.

SpringBoot FeignClient vs WebClient

I want to consume a couple of rest services. Before I have used RestTemplate, but now I want to know What is main diffrences of SpringBoot FeignClient and WebClient?
when they should be used?
To be able to answer “when” one needs to understand the capabilities of each.
Spring WebClient is a non-blocking reactive client to make HTTP requests. Hence if you intend to use Spring Reactive Stream API to stream data asynchronously then this is the way to go. Think event-driven architecture. WebClient is part of the Spring WebFlux library.
[Feign]3 is a declarative REST library that uses annotations based architecture with thread-per-request model. This means that the thread will block until the feign client receives the response. The problem with the blocking code is it must wait until the consuming thread completes, hence think memory and CPU cycles.
So use Spring WebClient when needing non-blocking HTTP requests otherwise Feign due to simple usage model.
(Note: There is no reason as to why one cannot use WebClient for blocking operations but Feign is more mature and it’s annotation based model makes it easier)
The main difference is that WebClient supports Reactive calls.
You can achieve that with 3rd party feign clients like https://github.com/Playtika/feign-reactive but basically for a reactive way you should consider using WebClient with some neat async connector like Jetty. On the other hand, If you want a blocking way with minimal hassle then Feign could be your best choice.
WebClient is a non-blocking reactive.
Feign is blocking.

Can Spring Cloud Gateway work with microservices that are not asynchronous?

I have a few synchronous microservices working on production using Spring Boot 2.X version. Soon, we need to implement a gateway if the number of instances of each microservice is going to be increased. I read that Zuul was in a maintenance phase and was replaced by Spring Cloud Gateway which is by default asynchronous technology. My question is, can I still implement Spring Cloud Gateway with my microservices?
Yes, you can use Spring Cloud Gateway without any doubts.
Basically, asynchronous technology means that your resources/threads on Api Gateway won't be blocked waiting for the response from downstream services and that increases a throughput.
Now, once your blocking services complete their internal logic they respond back to Api Gateway using an originally opened connection. Api Gateway in turn responds back to your client.

Spring boot reactive WebClient calling legacy endpoint

In a Spring Boot (2.2.2.RELEASE) application, I have reactive endpoints (returning Mono or Flux), each of them is using reactive WebClient for calling another service. This "other" service is legacy (non-reactive) one.
Here is my question:
Is there a benefit of using Webflux (reactive WebClient) if my reactive endpoint is calling this non-reactive endpoint which does blocking stuff?
Is my reactive endpoint still reactive?
If we're talking about HTTP endpoints, we can call them with blocking or non-blocking (asynchronous) clients, but not fully reactive.
If your "new" application is reactive, you have to use non-blocking client (WebClient in your case), otherwise you will block NIO-threads and loose all the advantages of the reactive approach. The fact that the “other” application is blocking doesn't matter, you can still get a less resource-intensive "new" application.
They are
1. Not fully.
2. Your request is not full reactive until you change legacy APIs
Explanation:
End-to-End Reactive pattern only help into to the performance side
Currently you’re using reactive client this helps to connect to server in two way communication.
First set of APIs are reactive so web server layer is now reactive but data layer (Legacy APIs ) not reactive

Can I consume a non-reactive REST API service using the Spring 5 WebFlux WebClient?

I have a written a microservice using Spring 5 WebFlux and trying to consume a non-reactive REST API through it. Is it possbile to consume a non-reactive service using a reactive webclient?
Yes, this is possible. From the server's point of view, this is just a regular HTTP client. WebClient does support streaming and backpressure, but this doesn't change things at the HTTP level.
The backpressure is dealt with at the TCP flow-control level, so the HTTP protocol stays the same.

Resources