How to create API Gateway using Spring Cloud Gateway — Step 6

Richy Great
4 min readJun 5, 2020

--

Photo by Jon Garrison on Unsplash

This article is from vinr Tech team. Try our business solutions for FREE.

This story is part of a series “Initial Commit till Running on Cloud”.

In the beginning of your discussion about awesome product, you thought some of your features can be opened up for usage other than your own product. Or your product is a SaaS and your clients might want an API for everything. It gives their developers a peace of mind when they know they can do everything using tiny rest clients, which their ‘not so technical’ colleagues are doing using UI.

So lets give them what they need. A Rest API for everything, opened up with some Oauth security. We will add the Oauth part later.

To do this we might need an API gateway. Who is Gamora and Why is Gamora? Well API gateway in a microservice architecture is the single point of entry for all the APIs provided by your applications. It simply routes requests to different microservices based on a defined set of rules. These rules involves some kind of url pattern. For example http://api.example.com/user-service/api/v1/users/godadmin will be routed to user microservice and the new path will be internally computed by the API gateway by populating the internal load balancer DNS name and port of user microservice appended with /api/v1/users/godadmin

http://api.example.com/user-service/api/v1/users/godadmin translates to http://prod-private-lb.123456789.aws.com:8081/api/v1/users/godadmin

Also we have to digest the truth that nobody can reach user microservice from outside the VPC. Hence API gateway receives the external request translates it to a locally understandable request and fetches the response to transmit to the external requester. Sounds cool?

Enough talk, let’s code.

Spring Cloud Gateway

Spring Cloud Gateway provides an API gateway on top of Spring ecosystem. It includes a lightweight reactive Netty server and the whole routing is done Non Blocking too. This makes Spring Cloud Gateway simple to code and lightning fast to deploy. All you need is an Application class and an application.yml file with your routes.

The pom.xml this time has a minor change in dependency management section. We have to include spring-cloud-dependencies.

Always check the compatible spring cloud release version for the spring boot you have selected. Easy way is to go for the latest in both from maven central.

@SpringBootApplication
public class AwesomeAppApiGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(AwesomeAppApiGatewayApplication.class, args);
}
}

The application.yml is a little tricky but I will tell you a secret. All you need to know is this RewritePath filter if you are routing /microservice-name/** to /**

internal.lb.uri: http://localhost
spring:
cloud:
gateway:
routes:
- id: user-microservice
uri: ${internal.lb.uri}:8081
predicates:
- Path=/user-service/**
filters:
- RewritePath=/user-service/(?<path>.*), /$\{path}

When we deploy we have to override the internal.lb.uri property in environment variable (in container) pointing it to the DNS name of our internal Load Balancer. You can read the above property like predicates.path is the lookup in an incoming request url. If the incoming url has a path as given in one of the routes’ predicates then spring cloud gateway removes the first argument of the given filters.RewritePath and leaves the rest of the path as it is.

Using the uri property Spring Cloud Gateway will construct the url combining with RewritePath. http://localhost:8081/api/v1/users/godadmin is the final path if the request is http://localhost:8080/user-service/api/v1/users/godadmin

Next Step is to create a Build pipeline using CodeBuild in AWS. This is the first step towards Cloud deployment.

Going good? Please read the next part of this series. Also kindly leave your feedback :)

--

--

Richy Great
Richy Great

Written by Richy Great

Father, Software Developer, Tech founder and a Story teller

No responses yet