Reverse Proxy
The reverse proxy is a server used in communication between the external clients and the internal applications. With the help of reverse proxy the flow of the traffic between the clients and server become smooth and more efficient. Example of reverse proxy nginx. The main use of a reverse proxy server are :
- Load-Balancing
- Web Acceleration
- Security & Anonymity
API Gateway
The API gateway works in a way that whenever a API call is made from the client side, it takes the calls and routes them to microservice which will be dealing with that particular call. The API gateway is also responsible for performing appropriate action on the calls like routing, composition and adding protocols.
Spring Cloud Gateway
The Spring Cloud Gateway is a way to implement the API Gateway. This implementation was done by the Spring Cloud team on the Spring reactive ecosystem. It makes use of Gateway Handler to direct the request made by the client side to the final destination. The three main functional blocks of Spring Cloud Gateway are as follows :
- Route : It is the pathway taken by the request from the initial point to the endpoint
- Predicate : These are the conditions which the request fulfil while parsing through the applications
- Filter : This is basically used if there is a need to make changes on the request or the response
Route Predicate Factories
The Spring Cloud Gateway compares the routes of the incoming request as part of Spring WebFlux HandlerMapping infrastructure. It has many pre-defined route predicate factories. The predicates compares on the basis of different attributes passed on the HTTP request. Below are five route predicate factories :
- After Route Predicate Factory
The after route predicate factory takes one parameter which is a datetime (Java : ZonedDateTime). The predicate compares the request which happen after the value of the datetime passed in the parameter. Example :
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
2. Before Route Predicate Factory
The before route predicate factory takes one parameter which is a datetime (Java : ZonedDateTime). The predicate compares the request which happen before the value of the datetime passed in the parameter. Example :
spring:
cloud:
gateway:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
3. Between Route Predicate Factory
The between route predicate factory takes two parameters which is a datetime1 and datetime2. (Java : ZonedDateTime). The predicate compares the request which happen between the interval of the datetime1 and the datetime2 values passed in the parameter. Note : The value of datetime2 parameter should be after datetime1. Example :
spring:
cloud:
gateway:
routes:
- id: between_route
uri: https://example.org
predicates:
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
4. Cookie Route Predicate Factory
The cookie route predicate factory takes two parameters which is a cookie name and a regexp (regexp : Java regular expression). The predicate compares the cookies with the specified the given name values and whose values are same as the regular expression as passed in the parameters. Example :
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=chocolate, ch.p
5. Header Route Predicate Factory
The header route predicate factory takes two parameters which is a header name and a regexp (regexp : Java regular expression). The predicate compares the header with the specified the given name values and whose values are same as the regular expression as passed in the parameters. Example :
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+
Conclusion
The Spring Cloud Gateway helps in providing an efficient way to simplify and a effective way to provide the path to the API calls. It provides the basic features such as the predicates and filters which are defined according to the routes. The predicates used in the Spring Cloud Gateway are very simple and are easier to implement. The above details provides how owner of an application can use the in-built predicates to get the required inputs. The predicates can be used by combination of more than one predicates to get the more specified output.