$ show spring-cloud/spring-cloud-gateway
Spring Cloud Gateway is a production-grade API gateway framework built on the Spring ecosystem, providing both reactive (WebFlux) and servlet-based (WebMVC) implementations. It routes HTTP/HTTPS/gRPC/WebSocket traffic through a configurable chain of predicates and filters, supporting features such as path rewriting, header manipulation, rate limiting (Redis/Bucket4j), circuit breaking (Resilience4J), retry with backoff, response caching, load balancing, and distributed tracing (Micrometer/OpenTelemetry). The architecture is plugin-based: route predicates and filter factories are registered as Spring beans and composed via a fluent builder DSL or YAML/properties configuration. External dependencies include Project Reactor, Spring Cloud Load Balancer, Redis, and optional integrations with gRPC, OAuth2, and serverless functions. The codebase is organized into multiple Maven modules: the core server modules (spring-cloud-gateway-server-webflux and spring-cloud-gateway-server-webmvc) contain the routing engine, filter/predicate factories, and auto-configuration; proxy exchange modules provide lightweight annotation-based proxying; and integration tests cover HTTP/2, SSL, WebSocket, and gRPC scenarios. Key entry points include FilteringWebHandler (WebFlux) and GatewayRouterFunctions (WebMVC) for request processing, RoutePredicateHandlerMapping for route matching, and GatewayAutoConfiguration for bean wiring. The system is designed for extensibility: custom filters and predicates can be added by implementing GatewayFilter or RoutePredicateFactory and registering them as beans. Data flows follow a standard gateway pattern: an incoming request is matched against route definitions (loaded from properties, Redis, or service discovery), then passed through a chain of global filters and route-specific filters. Filters can modify the request/response, cache bodies, enforce rate limits, or invoke circuit breakers before forwarding to the backend via a configurable HTTP client (Netty-based for WebFlux, RestTemplate/WebClient for WebMVC). Observability is embedded via Micrometer metrics and OpenTelemetry tracing, with custom observation conventions for gateway-specific events.
The LLM identified 4 files worth highlighting for reuse:
Defines an interface and @Shortcut annotation for parsing concise string-based configuration into typed config objects using SpEL with a restricted evaluation context. This pattern allows filter and predicate factories to accept configuration as comma-separated key=value pairs from YAML/properties, reducing boilerplate. The implementation is self-contained and could be extracted to other Spring projects needing compact configuration binding.
Provides a clean interface for request/response header filtering with ordered execution and trusted-proxy validation. Implementations like XForwardedHeadersFilter and ForwardedHeadersFilter compose into a chain via ObjectProvider. The pattern is reusable for any HTTP header sanitization or forwarding logic and is not tightly coupled to the gateway's routing internals.
A generic utility to cache reactive Flux streams in a Map or custom cache, with materialization/dematerialization support. It solves the common problem of caching hot reactive streams without breaking the reactive chain. The implementation is self-contained and can be reused in any reactive application needing Flux caching.
Defines a RateLimiter interface with isAllowed() and getConfig() methods, implemented by RedisRateLimiter (using Lua scripts for atomic token bucket) and Bucket4jRateLimiter. The abstraction is clean, supports key resolvers, and can be extracted to other projects needing pluggable rate limiting with different backends.