Cross-cutting requirements in Microservices like authentication, authorization, load balancing, rate limiting etc. also required to be implemented. But the question is where it needs to be implemented. Here, no prices for guesses, it should be thorough API Gateway or Edge server only. ‘Spring Cloud Gateway’, a successor of ‘Netflix Zuul’ and pretty good in fulfilling such demands.
The first level benefit of Spring Cloud Gateway is, it is a non-blocking API. So, what does it means actually? Non-blocking APIs processes requests asynchronously. Here, one thread is always available to address requests. All requests are getting processed in background asynchronously though multiple threads.
Zuul 1 which is having compatibility with Spring Cloud is a blocking API while Zuul 2 is a non-blocking approach but Zuul 2 is not having compatibility with Spring Cloud.
Spring Cloud Gateway has three building blocks,
In a most simple way is a mechanism which routes incoming requests to the right destination. The important parameter for such routing are, URI of destination and a condition that needs to be fulfilled. So, technically a route consists of,
- ID, it is a unique identification of route. This can be any text of your choice.
- Destination URI, It is a URI of microservice which we want to forward the request.
- Set of Predicates/filters, here, predicates are condition which needs to be through to access microservice. Filter is kind of layer which allows to modify request or response of microservice.
As mentioned above predicate is set of criteria and follows functional programming i.e. one can consider lambda expressions to implement functionality. Predicates can be a decision maker which service will be called. Predicate will help you to divert to microservices based on URI patterns. One can control route for microservice on the basis of headers, cookies or parameters as well.
Spring Cloud Gateway filters allows to manipulate request and response of microservices.
Filters are mainly of two type
1) Pre Filters, these filters are implemented between client and service which means after request sent by client and before service is called. So, these filters are for incoming request from client.
2) Post Filters, these filters are implemented between service and client which means after response sent to client(from microservice) and before client receives the response. So, these filters are for out going response from microservice.
Filters can be implemented through defining in properties file as well as through code implementation, choice will be as per need of time.
It is enough to introduce Spring Cloud Gateway. We need to have some deep dive to understand capability of this library of Spring Cloud. Please have patience Part-2 of this article is on the way …..