How to configure API Gateway Routes manually using Spring Boot in Microservices
In this tutorial we are going to learn about configuring the manual routes since we have learnt in our previous tutorial above configuring the automatic routes.
Firstly, we will configure a route that will tell the Spring cloud API gateway to route the HTTP request That is received by a spring cloud API gateway to a destination Microservice that runs behind the Spring cloud API gateway.
So we will specify a configuration property in application.properties under resources for a route as followed i.e. spring.cloud.gateway.routes where we need to specify the id, URI, Predicates and also Filters.
server.port=8082 spring.application.name=api-gateway eureka.client.service-url.defaultZone=http://localhost:8010/eureka #Custom Routes for user Microservice spring.cloud.gateway.routes.id=users-status-check spring.cloud.gateway.routes.uri=lb://user-ms1 spring.cloud.gateway.routes.predicates=Path=/users/status/check spring.cloud.gateway.routes.predicates=Method=GET spring.cloud.gateway.routes.filters=RemoveRequestHeader=Cookie
In the URI we will specify the name with which the Microservice has been registered with the Eureka Discovery Service and it can also be an IP address or a hostname of a Microservice that is running behind the API gateway.
Note: spring.cloud.gateway.routes.uri=lb://user-ms1 in this “lb” stands for load balancer.
We can have more than one predicates here we go for sub indexing as show above and its same as the predicate concept in Java 8 which acts like a if condition and if condition in this predicate matches then the request will be routed to a granted destination.
In the above example I have specified predicate with path i.e. predicates=Path=/users/status/check , if the request URI path matches path specified in predicate then spring cloud API gateway will route this request to the destination URI.
We can use different filters that can be applied while a request is routed via API Gateway. For example we can apply a filter on excluding request cookies or include JWT access token for validation.
Once we are done with the custom route’s configuration. We will firstly start the Eureka Discovery service i.e., Eureka Server and then we will start our spring cloud API gateway followed by a UserMicroservie.
To test the Spring cloud API gateway, open postman and specify the URL as follows i.e. http://localhost:8082/users/status/check ,here the port number specified is 8082 on which the API gateway is running.