Microservices : Spring Cloud API Gateway

Chakresh Tiwari
ShoutLoudz
Published in
3 min readJun 11, 2022
Photo by Prado on Unsplash

In our series of Microservices, this is the second article and previously we have talked about Service registry. Now in this article, we will discuss how What is API Gateway and how to configure API gateways using Java (Spring Boot).

Introduction

A basic understanding is that “Gateways are used for routing the request from a client to server”. But the question is why we need that in the middle when we can directly send the request from a client to the server.

Why is API Gateway required?

So consider an example of a microservice architecture where on one Page we are showing data from different services. To get that data we need to call APIs of all these services, But in that case, the client will make calls to all services, which is making it tightly coupled with server code, because let says we introduced one more microservice then in client also we need to add code for handling that call. So to overcome this problem Gateways got introduced, the client will just make one call and the further gateway will call other microservices.

Use cases of API Gateway:

API gateways are used for many purposes some of them are-

  1. Authentication: Each request can get authenticated at the gateway and then further send to corresponding service.
  2. SSL Termination: Client calls will be HTTPS but further internal calls will be HTTP only.
  3. Load balancer: Gateways can work as a Load balancer also and they will route the request to different servers.
  4. Access quota of APIs we can track at a gateway.
  5. API health monitoring.
  6. API Versioning
  7. Chaos Monkey Testing
  8. A/B Testing
  9. Rate limiting

Drawbacks of API Gateway:

There are a few drawbacks also like

  1. When a client is communicating with the server there is one hop which is adding latency to each request.
  2. It is complicated to set up API gateways.

We can configure multiple gateways also in our architecture for different types of devices(web/android/iOS). We can trim or add some info based on device type.

So this is about API gateway introduction and its use cases. Now let's implement it using Spring Boot.

API Gateway using Spring Boot

So after creating the service registry using the eureka server, we will create one spring boot project for the gateway(dependencies: gateway, reactive web, eureka client) and then one microservice let's say USER microservice(dependencies: web, eureka client)(It will handle onboarding of user)

Now after this start user, API-gateway and eureka server microservice, It will register the USER and API-gateway microservices in the registry.

In Users service just create a test controller and call it using postman.

Using the discovery locator pattern we can connect services using the service name in the URL, but mostly we don't want the service name in the URL

For enabling the discovery locator add the below properties in yml file

spring.cloud.gateway.discovery.locator.enabled=true

For making a request to route through the API gateway we need to add these properties in the gateway yml file.

In port 8083: User-Service

In port 8082: Api gateway

The client will send the request to the API gateway and it will route it to the corresponding microservice, Apart from that using filters we can do other tasks also.

Example

http://localhost:8082/user-microservice/users/test

It will go to the users service controller

http://localhost:8082/users/test: In a similar way this will also go into users service.

This is all about the API gateways.

In the next post, we will discuss load balancers.

Thanks for reading!!

--

--

Chakresh Tiwari
ShoutLoudz

Software Engineer at Cisco(Appdynamics) , Sharing my knowledge and experience related to work. I am here to help learners to prepare for tech interviews.