🔍 What is an API Gateway?
An API Gateway is a server that acts as a single entry point into a system, typically for microservices architecture.
It handles requests by:
-
Routing them to the appropriate microservice
-
Applying security, rate limiting, authentication
-
Performing load balancing, logging, caching, etc.
📦 Why Use an API Gateway?
Feature | Purpose |
---|---|
🔁 Request Routing | Directs incoming requests to appropriate backend services |
🔐 Security | Centralized authentication and authorization |
⚖️ Load Balancing | Distributes traffic among service instances |
🔄 Protocol Translation | Translates protocols (HTTP to WebSocket, etc.) |
📉 Rate Limiting | Prevents abuse by limiting requests per client |
📄 Aggregation | Combines responses from multiple services into one |
📊 Monitoring | Tracks usage, logs, and metrics |
🌍 CORS Handling | Cross-origin resource sharing setup |
🧱 Architecture Diagram:
🚀 Common API Gateway Tools
Tool | Language | Notes |
---|---|---|
🔹 Spring Cloud Gateway | Java (Spring Boot) | Official Spring API Gateway |
🔹 Zuul (Netflix) | Java | Older Netflix API Gateway (no longer maintained) |
🔹 NGINX | C | Lightweight, powerful reverse proxy |
🔹 Kong | Lua | Enterprise-grade open source API Gateway |
🔹 Amazon API Gateway | Cloud | Managed gateway on AWS |
🛠️ Example: Spring Cloud Gateway (Java)
1️⃣ Dependency (Maven):
2️⃣ application.yml
config:
3️⃣ Main class:
🔐 API Gateway with Security (JWT)
-
Intercepts incoming requests
-
Validates JWT token
-
Rejects unauthorized users before hitting backend microservices
📊 Advanced Features
-
Rate limiting (
RequestRateLimiter
) -
Circuit breaker (
resilience4j
,Hystrix
) -
Path rewriting
-
Load balancing with service discovery (
Eureka
)
📌 Pros and Cons
✅ Advantages:
-
Centralized control
-
Simplified client code
-
Unified security
❌ Disadvantages:
-
Single point of failure (unless highly available)
-
Can become bottleneck if not scaled properly
-
Adds complexity
No comments:
Post a Comment