Understanding the API Gateway Design Pattern

 In the world of microservices, the API Gateway design pattern plays a crucial role in managing how clients communicate with your backend services. This article will break down what an API Gateway is, why you need one, and provide real-time examples that are easy to understand.

What is an API Gateway?

Why Use an API Gateway?

How Does an API Gateway Work?

Basic Example

Project Setup

Step 1: Creating the API Gateway

<dependencies>
<!-- Spring Boot Web Dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Spring Cloud Gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

<!-- Spring Boot Actuator for Health Checks -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- Eureka Client (Optional for Service Discovery) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version> <!-- Version of Spring Cloud -->
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Step 2: API Gateway Routing Configuration

server:
port: 8080 # API Gateway runs on port 8080

spring:
cloud:
gateway:
routes:
- id: user-service
uri: http://localhost:8081 # User service URL
predicates:
- Path=/user/**

- id: order-service
uri: http://localhost:8082 # Order service URL
predicates:
- Path=/order/**

- id: product-service
uri: http://localhost:8083 # Product service URL
predicates:
- Path=/product/**

Explanation:

Step 3: Setting up Microservices (Product, Order, User)

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Eureka Client (optional if using service discovery) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
@RestController
@RequestMapping("/user")
public class UserController {

@GetMapping("/info")
public String getUserInfo() {
return "User Info: John Doe";
}
}

Step 4: Running the Microservices

Step 5: Adding Extra Features to the API Gateway

1. Load Balancing

spring:
cloud:
gateway:
routes:
- id: user-service
uri: lb://USER-SERVICE
predicates:
- Path=/user/**

2. Global Filters for Security and Logging

@Bean
public GlobalFilter customGlobalFilter() {

return (exchange, chain) -> {
System.out.println("Global Pre Filter executed");

return chain.filter(exchange).then(Mono.fromRunnable(() -> {
System.out.println("Global Post Filter executed");
}));
};
}

3. Rate Limiting

spring:
cloud:
gateway:
routes:
- id: user-service
uri: http://localhost:8081
predicates:
- Path=/user/**
filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 10
redis-rate-limiter.burstCapacity: 20

Real-Time Scenario

Without an API Gateway:

With an API Gateway:

Handling Different Scenarios with API Gateway

Conclusion

Comments

Post a Comment

Popular posts from this blog

Understanding Service Discovery in Microservices

Understanding Executors and ExecutorService in Java

HashMap Internal Implementation in Java

Understanding “try-with-resources” in Java