API Gateway
If you have 10 microservices, you don’t want the frontend to know 10 different URLs. You need a single entry point. Spring Cloud Gateway is the standard, built on top of Spring WebFlux (Non-blocking I/O).1. Why an API Gateway?
- Routing: Single domain (
api.myapp.com) routes to multiple services. - Security: Centralized Authentication/Authorization (OAuth2).
- Rate Limiting: Protect your backend from DDoS.
- Monitoring: Log every request entering the system.
2. Setup
- New Spring Boot Project.
- Dependencies:
spring-cloud-starter-gateway,spring-cloud-starter-netflix-eureka-client.
spring-boot-starter-web (Tomcat). Gateway uses Netty.
3. Configuration (Routing)
You can route requests based on paths.application.yml:
localhost:8080/api/v1/users/1 is forwarded to USER-SERVICE/users/1.
4. Custom Filters
You can write Global Filters to intercept every request (e.g., logging).5. Gateway Authentication
Using Spring Security at the gateway level.6. How it Works (Internal Flow)
Spring Cloud Gateway is built on the Reactor Netty (Non-blocking) server.7. Rate Limiting (Redis)
Spring Cloud Gateway has a built-in RequestRateLimiter usage Redis and the Token Bucket Algorithm. Dependency:spring-boot-starter-data-redis-reactive.
Config: