Skip to content

Spring Cloud

结构

plain
┌─────────────────────────────────────────────────────────────────────┐
│                         Request                                     │
│                    (HTTP/WebSocket/HTTP2)                           │
└─────────────────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────────────────┐
│                    Spring Cloud Gateway                             │
│                 (basic of WebFlux + Netty)                          │
│  ┌───────┐  ┌───────────┐  ┌──────────────┐  ┌────────────────────┐ │
│  │ Route │→ │   Filter  │→ │ LoadBalancer │→ │ protocol coversion │ │
│  │       │  │           │  │              │  │                    │ │
│  └───────┘  └───────────┘  └──────────────┘  └────────────────────┘ │
└─────────────────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────────────────┐
│                       Registry Service                              │
│  service discovery: (Nacos/Consul/Eureka) → find multiple instances │
│  load balance:(Spring Cloud LoadBalancer) → get health instance     │
└─────────────────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────────────────┐
│                      service Instance                               │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐  ┌────────────┐  │
│  │ service port│→ │ Filter Chain│→ │  Dispatcher │→ │ Controller │  │
│  │             │  │             │  │   Servlet   │  │            │  │
│  └─────────────┘  └─────────────┘  └─────────────┘  └────────────┘  │
└─────────────────────────────────────────────────────────────────────┘

流程

plain
Client           Gateway              LoadBalancer         Registry       Micro
|                |                    |                    |              Service
|--HTTP Request->|                    |                    |              |
|                |                    |                    |              |
|                |--1.Route Matching--|                    |              |
|                |                    |                    |              |
|                |--2.Load Balance----|                    |              |
|                |                    |--3.Choose Instance-|              |
|                |                    |                    |              |
|                |                    |<-4.Instance Info---|              |
|                |                    |                    |              |
|                |--5.forward Request------------------------------------>|
|                |                    |                    |              |
|                |<-6.Response--------------------------------------------|
|                |                    |                    |              |
|<-HTTP Response-|                    |                    |              |
  • 1.Route Matching: PathPredicate
  • 2.Load Balance:
    • LoadBalancerClientFilter (负载均衡)
  • 3.Choose Instance:
    • RoundRobin(轮询)
    • Random(随机)
    • Weight(权重)
    • Custom(自定义)
  • 4.Instance Info
  • 5.Forward Request
    • Protocol Coversion
    • NettyRoutingFilter (转发,Netty Request)
    • Header
      • X-Forwarded-For: 原始客户端IP
      • X-Forwarded-Proto: 原始协议 (http/https)
      • X-Forwarded-Port: 原始端口
      • X-Gateway-Request-Id: 全局请求ID (链路追踪)NettyRoutingFilter (核心转发)
      • X-Request-From: Gateway (自定义标识)
  • 6.Response

Gateway

plain
┌─────────────────────────────────────────────────────────────────┐
│                     Gateway 过滤器架构                           │
├─────────────────────────────────────────────────────────────────┤
│  GlobalFilter (全局过滤器) - 所有路由生效                        │
│  ├─ NettyRoutingFilter        # 核心:转发请求到下游服务           │
│  ├─ NettyWriteResponseFilter  # 处理下游响应                     │
│  ├─ LoadBalancerClientFilter  # 负载均衡 (lb://前缀)              │
│  ├─ WebsocketRoutingFilter    # WebSocket 代理                  │
│  ├─ ForwardPathFilter         # 本地转发 (forward://)            │
│  └─ 自定义 GlobalFilter       # 鉴权、限流、日志等                 │
├─────────────────────────────────────────────────────────────────┤
│  GatewayFilter (路由过滤器) - 特定路由配置                        │
│  ├─ StripPrefix               # 去除路径前缀                      │
│  ├─ AddRequestHeader          # 添加请求头                        │
│  ├─ AddResponseHeader         # 添加响应头                        │
│  ├─ CircuitBreaker            # 熔断 (Resilience4j)               │
│  ├─ Retry                     # 重试机制                          │
│  ├─ RequestRateLimiter        # 限流 (RedisRateLimiter)           │
│  └─ ModifyRequestBody         # 修改请求体                        │
└─────────────────────────────────────────────────────────────────┘

Router

plain
┌─────────────────────────┬───────────────────────────┐
│ Predicate 类型           │ 匹配逻辑                   │
├─────────────────────────┼───────────────────────────┤
│ PathRoutePredicate      │ PathPattern 匹配 /api/**  │
│ MethodRoutePredicate    │ HTTP 方法匹配              │
│ HeaderRoutePredicate    │ 请求头正则匹配              │
│ QueryRoutePredicate     │ 查询参数匹配                │
│ WeightRoutePredicate    │ 权重分配 (灰度发布)          │
│ RemoteAddrRoutePredicate│ IP 白名单/黑名单            │
│ CookieRoutePredicate    │ Cookie 值匹配              │
│ Before/After/Between    │ 时间窗口匹配 (定时切换)       │
└─────────────────────────┴───────────────────────────┘