API Gateway = cửa vào duy nhất của hệ microservices — client chỉ gọi 1 endpoint, gateway route đến đúng service phía sau. Giá trị chính: gom các concern chung về 1 chỗ — authentication, rate limiting, CORS, logging, retry — thay vì lặp lại ở từng service.
Spring Cloud Gateway build trên WebFlux (non-blocking, chịu concurrency cao), thay thế Zuul 1 (blocking, đã vào maintenance mode). 3 khái niệm:
yaml
spring.cloud.gateway.routes:
- id: orders
uri: lb://order-service # lb:// = load-balance qua service discovery
predicates: [ Path=/api/orders/** ]
filters: [ StripPrefix=1 ]- Route — đích đến (service phía sau).
- Predicate — điều kiện match request: path, method, header, query...
- Filter — biến đổi request/response: thêm/xoá header,
StripPrefix, rate limit (Redis), tích hợp circuit breaker.
Bộ ba microservices hay bị hỏi cùng nhau: Gateway (cửa vào) + Service Discovery (tìm service — câu riêng) + Circuit Breaker (chống cascade failure — câu riêng).