intermediate~2h

Spring Cloud Gateway

Spring Boot Mastery's API Gateway module covered the concept. This module is Spring's own purpose-built implementation of it, and what it adds beyond a plain reverse proxy.

Learning objectives

  • Beginner: Configure a route that forwards requests for one path prefix to a specific downstream service.
  • Intermediate: Add a filter that modifies a request or response as it passes through the gateway.
  • Advanced: Combine routing, a circuit breaker, and rate limiting on the same route.
spring: cloud: gateway: routes: - id: orders-route uri: lb://order-service predicates: - Path=/api/orders/**

lb://order-service tells the Gateway to resolve order-service through service discovery and client-side load balancing — the exact same mechanism from earlier in this category, now sitting behind ONE public-facing entry point instead of each service being separately internet-reachable.

💻 Code example

spring: cloud: gateway: routes: - id: orders-route uri: lb://order-service predicates: - Path=/api/orders/**

A filter runs on every request matching a route, before it reaches (or after it returns from) the downstream service — adding an auth header, stripping a path prefix, or adding a response header are all filters.

filters: - StripPrefix=1 - AddRequestHeader=X-Gateway-Source, cracklab-gateway

This is precisely where the cross-cutting authentication check from the Spring Security category's filter chain conceptually lives at the SYSTEM level — instead of every downstream service independently validating a JWT, the Gateway can validate it once, centrally, before a request ever reaches a backend service.

A production Gateway route rarely stands alone — the same route definition composes a circuit breaker (fail fast to a fallback if the downstream service is unhealthy, covered next module) and a rate limiter (protect a downstream service from being overwhelmed by one aggressive client) directly:

filters: - name: CircuitBreaker args: name: ordersCircuitBreaker fallbackUri: forward:/fallback/orders - name: RequestRateLimiter args: redis-rate-limiter.replenishRate: 10 redis-rate-limiter.burstCapacity: 20

✓ Quick recap

  • Gateway routes forward requests by path pattern to a service resolved through discovery, not a hardcoded address.
  • Filters modify requests/responses in flight — centralizing concerns like auth-header validation instead of duplicating them per service.
  • A single route commonly combines routing, circuit breaking, and rate limiting together, since all three are cross-cutting concerns that belong at the edge, not inside every individual service.

💻 Code example

filters: - name: CircuitBreaker args: name: ordersCircuitBreaker fallbackUri: forward:/fallback/orders - name: RequestRateLimiter args: redis-rate-limiter.replenishRate: 10 redis-rate-limiter.burstCapacity: 20

Want a visual for this concept?

Generate a diagram tailored to “Spring Cloud Gateway” — the AI picks whichever visual (flowchart, comparison, sequence, etc.) best fits.

Sign in to generate a visual →

Practice quiz

Next Step

Continue to Circuit Breaker with Resilience4j← Back to all Spring Cloud chapters