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:
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).
An API Gateway = the single entry point of a microservices system — clients call one endpoint and the gateway routes to the right backing service. Its main value: pulling shared concerns into one place — authentication, rate limiting, CORS, logging, retries — instead of repeating them in every service.
Spring Cloud Gateway is built on WebFlux (non-blocking, high concurrency), replacing Zuul 1 (blocking, in maintenance mode). Three concepts:
spring.cloud.gateway.routes:
- id: orders
uri: lb://order-service # lb:// = load-balanced via service discovery
predicates: [ Path=/api/orders/** ]
filters: [ StripPrefix=1 ]- Route — the destination (backing service).
- Predicate — request match conditions: path, method, header, query...
- Filter — request/response transformation: add/remove headers,
StripPrefix, rate limiting (Redis), circuit breaker integration.
The microservices trio often asked together: Gateway (the front door) + Service Discovery (finding services — separate item) + Circuit Breaker (preventing cascade failure — separate item).