Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 69

    API Gateway (Microservices)

    Clients should not know every Orders, inventory, and pricing URL.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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

    1. Create a Gateway Boot app with spring-cloud-starter-gateway.
    2. Define an Orders route: /orders/** → lb://orders-service with path rewrite.
    3. Add a rate-limiter or auth filter at the edge.
    4. Point the web/mobile client at the gateway only; remove direct service URLs.

    Informative example

    Spring Cloud Gateway routes for Orders.

    yaml
    spring:
    cloud:
    gateway:
    discovery:
    locator:
    enabled: true
    lower-case-service-id: true
    routes:
    - id: orders
    uri: lb://orders-service
    predicates:
    - Path=/orders/**
    filters:
    - RewritePath=/orders/(?<seg>.*), /api/v1/orders/$\{seg}
    - name: CircuitBreaker
    args:
    name: ordersCB
    fallbackUri: forward:/fallback/orders
    - id: inventory
    uri: lb://inventory-service
    predicates:
    - 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.

    3 questions
    1BeginnerQuestionWhy use an API gateway in microservices?+

    Answer

    Single entry point for routing and cross-cutting concerns while hiding internal topology from clients.
    2IntermediateQuestionWhat does lb:// mean in a gateway URI?+

    Answer

    Resolve the service id via discovery/load balancer instead of a fixed host.
    3IntermediateQuestionShould the gateway be the only authorization layer?+

    Answer

    No — defense in depth: edge authn plus service-level authorization for Orders operations.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.