5. API Gateway Java Spring Boot Microservice

What is an API Gateway?

An API Gateway is a crucial component in a microservice architecture that acts as a reverse proxy, sitting between external clients (web browsers, mobile apps) and backend microservices. It handles client requests, routes them to the correct service, and offers additional functionalities such as authentication, load balancing, rate limiting, and request transformation. Essentially, it serves as a single entry point for all clients.

API-Gateway Architecture
API-Gateway Architecture


Key Functions of an API Gateway:
  • Routing: Directs incoming requests to the correct microservice based on rules.
  • Security: Manages authentication and authorization using tools like OAuth or JWT.
  • Request Aggregation: Combines multiple microservice responses into one client response.
  • Load Balancing: Distributes requests across service instances for better performance.
  • Rate Limiting: Controls request volumes to prevent overloads.
  • Caching: Improves performance by storing frequently accessed responses.
  • Logging and Monitoring: Tracks metrics for system insights and debugging.

Why Use an API Gateway in Microservices?
In a microservice architecture, direct communication between services can lead to complexity. API Gateways simplify this by acting as intermediaries. They streamline client communication by providing a unified API, enhance security through centralized validation, hide internal service details, and reduce client complexity.

Common API Gateway Patterns:
  • Backend for Frontend (BFF): Different API Gateways for different client types (mobile, web) to optimize data handling.
  • Single Gateway: A single point of entry for all microservices, suitable for small to mid-sized applications.
  • Microgateway: Multiple gateways that handle specific domains or services, ideal for large systems to reduce bottlenecks.

How to Implement an API Gateway:
You can implement API Gateways using frameworks like Spring Cloud Gateway for Java-based applications. It integrates well with Spring Boot, offering features like routing, security, and request filters.
  • Spring Cloud Gateway (Java): A Java-based, highly customizable API Gateway framework built on Spring Boot for routing, filtering, and securing microservices.
  • Kong API Gateway: An open-source, scalable API Gateway built on NGINX that supports plugins for security, monitoring, and traffic management.
  • AWS API Gateway (Cloud-based): A fully managed service that allows developers to create, publish, and manage APIs at scale within the AWS ecosystem.
  • Zuul (Netflix OSS): A mature, open-source API Gateway for dynamic routing, load balancing, and security, primarily used in Netflix's microservices architecture.
Here we will see Spring Cloud Gateway(Java)

Development Steps
  1. Create a Spring boot project as Microservice(api-gateway).
  2. Import in IntelliJ Idea as a module of parent folder.
  3. Register API-Gateway as Eureka Client to Eureka Server(service-registry).
  4. Configuring API Gateway Routes(Manual & Auto) and Test from Postman client.
1. Create Spring boot project as Microservice : Create a API-Gateway project from Spring Initializr with below three dependencies.
  • Gateway
  • Eureka Discovery Client
  • Spring Boot Actuator
Create API-Gateway from Spring Initializr

2. Import in parent folder as a module: After creating API-Gateway project, It is require to import in IDE (Intellij Idea).
Import API-Gateway in IDE
Import API-Gateway in IDE

3. Register API-Gateway as Eureka Client to Eureka Server: Below are the steps to register API-Gateway to Eureka Server as an Eureka Client. These steps are common for all Microservices to register in Eureka Server as Eureka Client.

Steps 1: Add the below maven dependency in the POM file. Since we have already added a dependency during create the API-Gateway project, so we don't need to add it again.
  • Eureka Discovery Client
  • Dependency Manager       [Add, if not exist yet]
  • Properties                          [Add, if not exist yet]
Step 2: Add the Eureka server URL in the Application.properties file of API-Gateway(api-gateway). and sert Port and Application name of API-Gateway.
Add Eureka Server URL in API-Gateway's Application.properties file
Add Eureka Server URL in API-Gateway's Application.properties file

Step 4: Run the API-Gateway and verify in Eureka UI (in the browser)
API-Gatewat instance verify in Eureka Server
API-Gatewat instance verify in Eureka Server

4. Route the Employee-Service and Department-Service in API-Gateway (Application.properties file): Basically two ways we can route the microservices- 
  • Manual Route
  • Auto Route
Manual Route: Open the Application.properties file of the API-Gateway service. Add below informations of Employee-Service and Department-Service.
  1. IP                         [http://localhost:8091  Or EMPLOYEE-SERVICE]
  2. Base URI             [lb://EMPLOYEE-SERVICE] This enables load balancing across multiple instances of the Employee-Service.
  3. Predicates            [Path=/emp_service/getById]
Manual Routing in Application.properties file
Manual Routing in Application.properties file

Verify Manual Route from Postman Client: In manual routing no need to add Microservice name(EMPLOYEE-SERVICE, DEPARTMENT-SERVICE) in URL. Just provide the base path and controller path. like below.
http://localhost:9191/emp_service/getById?id=9
Here 9191 port is API-Gateway port that routes with the Employee-Service.
Postman Output Manual Routing
Manual Routing verify in Postman client

Manual routing is recommended. It is easy to recognize the routing info.

Auto Route: In auto route, There is no requirement to manually route each microservice. Spring Cloud API-Gateway will auto-route all Microservices that are registered in the Eureka Server(service-registry). 

Configuration in Application.properties file-
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true

Official URL of properties:  https://cloud.spring.io/spring-cloud-gateway/reference/html/appendix.html 

Auto Routing configuration in Application.properties file
Auto Route Configuration in Application.properties file

Verify Auto Route from Postman Client: In the Auto Route system, the Microservice Name is required in URL.

http://localhost:9090/employee-service/emp_service/getById?id=9
Postman output by Auto Routing in API-Gateway
Auto-Route verify from postman client

Summary
Configured the API Gateway by creating an API-Gateway service, importing it as a module in the parent folder, registering it as a Eureka client, and setting up manual and automatic routes for Employee-Service and Department-Service.


Done 👍

Post a Comment

0 Comments