# spring-cloud/spring-cloud-gateway architecture

> Architecture diagram of the GitHub repository spring-cloud/spring-cloud-gateway, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/spring-cloud/spring-cloud-gateway
- Repository: https://github.com/spring-cloud/spring-cloud-gateway

## Overview

This project is the Spring Cloud Gateway project – a sophisticated API gateway and reverse proxy built on Java 17, Spring Framework 6, and Spring Boot 3. In reviewing both the complete file tree and the README, we see that the system’s purpose is to provide dynamic routing and powerful filtering for HTTP requests. The codebase is modularized into several Maven modules (e.g., core gateway server modules for both WebFlux and WebMVC, proxy exchange modules, sample applications, integration-tests for various protocols like HTTP2 and gRPC, and even starter modules). The project also supports integration with Spring Cloud DiscoveryClient, which helps dynamically determine backend services.

##### Main Components
- Client/External Requests – A block representing incoming client HTTP requests.
- Gateway Entry Points – Two primary variants:
– A WebFlux-based Gateway Server (as seen in spring-cloud-gateway-server-webflux and related modules)
– A WebMVC-based Gateway Server (evident in spring-cloud-gateway-server-webmvc and its support for MVC routing)
- Routing Engine – A core component within the gateway that uses dynamic routing:
– Route Predicate Factories for matching on HTTP method, path, headers, host, etc.
– Dynamic routing and integration with configuration (possibly pulling information from DiscoveryClient)
- Filter Chain – A distinct block representing the comprehensive set of Gateway Filter Factories (e.g., add/remove headers, modify request body, rewrite path, etc.) that process both incoming requests and outgoing responses.

##### Architectural Patterns and Design Principles
- Reverse Proxy Pattern with Dynamic Routing – The gateway acts as the single entry point for clients.
- Filter Chain (or Pipeline) Architecture – All requests pass through an ordered series of Gateway Filters that can independently modify both requests and responses.
- Modularization via Maven – Although not a runtime component, the file tree’s module separation (e.g., integration-tests, proxyexchange modules, and sample applications) reinforces the design’s extensibility and testability.
- Integration with Spring Cloud and Spring Boot – Dependence on Spring Boot auto-configuration signals the use of dependency injection and configuration-by-convention to wire up components.

##### Relevant Technologies and Frameworks
- Java 17 with Maven as the build system.
- Spring Boot 3, Spring Framework 6.
- Use of Spring Cloud DiscoveryClient (for dynamic service registration/discovery).
- Antora and Asciidoctor for documentation (from the docs directory).

##### Additional Guidance
- Be very detailed: ensure each component is clearly labeled.

Happy diagramming!

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        A["Client Request"]:::presentation
    end

    %% Gateway Layer
    subgraph "Gateway Layer"
        B["Gateway Server (WebFlux)"]:::gateway
        C["Gateway Server (WebMVC)"]:::gateway
        D["Route Matching & Dynamic Routing Engine"]:::engine
        E["Filter Chain (Gateway Filters)"]:::filter
        F["Discovery/Configuration Service"]:::discovery
        G["Proxy Exchange Module (WebFlux)"]:::proxy
        H["Proxy Exchange Module (WebMVC)"]:::proxy
    end

    %% Service Layer
    subgraph "Service Layer"
        I["Downstream / Backend Microservices"]:::services
        J["Sample Application"]:::sample
    end

    %% Data Flow Connections (vertical oriented)
    A -->|"to"| B
    A -->|"to"| C

    B -->|"process"| D
    C -->|"process"| D

    D -->|"applyFilters"| E
    E -->|"forwardReq"| I
    E -->|"sampleRoute"| J

    D -->|"lookup"| F

    B -->|"proxyCall"| G
    C -->|"proxyCall"| H

    %% Optional Response Flow (dashed for clarity)
    I ---|"response"| E
    E ---|"return"| B
    E ---|"return"| C

    %% Styles and Classes
    classDef presentation fill:#bbdefb,stroke:#1e88e5,stroke-width:2px;
    classDef gateway fill:#c8e6c9,stroke:#43a047,stroke-width:2px;
    classDef engine fill:#fff9c4,stroke:#fbc02d,stroke-width:2px;
    classDef filter fill:#ffe0b2,stroke:#fb8c00,stroke-width:2px;
    classDef discovery fill:#d1c4e9,stroke:#7e57c2,stroke-width:2px;
    classDef services fill:#b3e5fc,stroke:#0288d1,stroke-width:2px;
    classDef proxy fill:#f8bbd0,stroke:#d81b60,stroke-width:2px;
    classDef sample fill:#dcedc8,stroke:#7cb342,stroke-width:2px;

    %% Click Events
    click B "https://github.com/spring-cloud/spring-cloud-gateway/tree/main/spring-cloud-gateway-server-webflux"
    click C "https://github.com/spring-cloud/spring-cloud-gateway/tree/main/spring-cloud-gateway-server-webmvc"
    click D "https://github.com/spring-cloud/spring-cloud-gateway/tree/main/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/route"
    click E "https://github.com/spring-cloud/spring-cloud-gateway/tree/main/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter"
    click F "https://github.com/spring-cloud/spring-cloud-gateway/tree/main/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/discovery"
    click G "https://github.com/spring-cloud/spring-cloud-gateway/tree/main/spring-cloud-gateway-proxyexchange-webflux/src/main/java/org/springframework/cloud/gateway/proxyexchange/webflux"
    click H "https://github.com/spring-cloud/spring-cloud-gateway/tree/main/spring-cloud-gateway-proxyexchange-webmvc/src/main/java/org/springframework/cloud/gateway/proxyexchange/webmvc"
    click J "https://github.com/spring-cloud/spring-cloud-gateway/tree/main/spring-cloud-gateway-sample"
```
