API Gateway (Microservices)
Clients should not know every Orders, inventory, and pricing URL.
Introduction
Clients should not know every Orders, inventory, and pricing URL. An API gateway is the single entry point: routing, TLS termination, authn, rate limits, and often resilience wrappers.
Spring Cloud Gateway is the reactive, Boot 3–friendly choice on the JVM. It routes by path/host to lb://service-id when discovery is present, or to Kubernetes service URLs. Cross-cutting filters live once at the edge instead of in every microservice.
Gateways are not a substitute for service-level authorization — they reduce duplication and hide topology from mobile and web clients.
This lesson places TechLearningPRO's public API in front of the microservices graph.
Purpose of this lesson
Route external traffic through Spring Cloud Gateway to Orders and related services with filters for cross-cutting concerns.
Understanding the topic
One front door; many backends; filters for shared policy.
- Routes: predicates (path, method, header) → URI.
- lb://: load-balanced discovered service ids.
- Filters: rewrite paths, add headers, rate limit, circuit break.
- Discovery locator: optional auto-routes by service name.
- Edge security: JWT validation at gateway + still authorize in Orders.
Step-by-step explanation
- Create a Gateway Boot app with
spring-cloud-starter-gateway. - Define an Orders route:
/orders/**→lb://orders-servicewith path rewrite. - Add a rate-limiter or auth filter at the edge.
- Point the web/mobile client at the gateway only; remove direct service URLs.
Informative example
Spring Cloud Gateway routes for Orders.
spring:cloud:gateway:discovery:locator:enabled: truelower-case-service-id: trueroutes:- id: ordersuri: lb://orders-servicepredicates:- Path=/orders/**filters:- RewritePath=/orders/(?<seg>.*), /api/v1/orders/$\{seg}- name: CircuitBreakerargs:name: ordersCBfallbackUri: forward:/fallback/orders- id: inventoryuri: lb://inventory-servicepredicates:- Path=/inventory/**
Real-world use
TechLearningPRO mobile apps once embedded three base URLs. Renaming inventory broke old clients. After Gateway, clients call https://api.techlearningpro.example only; backend renames became route edits. Edge JWT validation also stopped unauthenticated traffic from touching Orders pods.
Security implications
The gateway is a high-value target.
- Terminate TLS at the edge; use mTLS or private networks east-west.
- Validate JWTs with proper issuer/audience checks — no shared static secrets in repo.
- Restrict admin gateway actuators like any other Boot app.
Best practices
- Keep routes declarative and versioned with the platform repo.
- Centralise rate limits and WAF-like filters at the edge.
- Emit gateway metrics and traces for every route.
Common mistakes
- Putting all business logic in gateway filters — keep it thin.
- Skipping service-side auth because 'the gateway checks JWT'.
- Blocking Netty threads with heavy work inside filters.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhy use an API gateway in microservices?+
Answer
2IntermediateQuestionWhat does lb:// mean in a gateway URI?+
Answer
3IntermediateQuestionShould the gateway be the only authorization layer?+
Answer
Hands-on exercise
Lab. Configure the Orders route above against two local services. Call via gateway path rewrite and confirm the backend sees /api/v1/orders/.... Add a fallback route and trip it with a downed Orders instance.
Summary
The gateway is the front door for Orders microservices. Next: centralise configuration with Spring Cloud Config Server.