Skip to content

Spring Cloud 全景概览

一、什么是 Spring Cloud

Spring Cloud 是 Spring 生态下的一套微服务治理工具集,它基于 Spring Boot 的自动配置能力,将分布式系统开发中常见的模式(如服务发现、配置管理、负载均衡、熔断降级、链路追踪等)封装成开箱即用的组件,让开发者可以快速构建分布式系统。

Spring Cloud 的本质是将分布式系统的复杂性抽象为组件,让开发者用 Spring Boot 的开发体验来构建微服务

二、组件体系全景图

                          ┌─────────────────────────────────────┐
                          │          Spring Cloud 组件体系         │
                          └─────────────────────────────────────┘

  ┌──────────────────────┐  ┌──────────────────────┐  ┌──────────────────────┐
  │     服务治理层         │  │     服务通信层         │  │     弹性容错层         │
  ├──────────────────────┤  ├──────────────────────┤  ├──────────────────────┤
  │ • Nacos (注册+配置)    │  │ • OpenFeign (声明式)   │  │ • Sentinel (熔断限流)  │
  │ • Gateway (网关)      │  │ • LoadBalancer (均衡) │  │ • Resilience4j (容错) │
  │ • Consul (注册+配置)   │  │ • RestTemplate (模板) │  │ • Hystrix (已停维)    │
  │ • Eureka (注册)       │  │ • WebClient (响应式)  │  │                      │
  └──────────────────────┘  └──────────────────────┘  └──────────────────────┘

  ┌──────────────────────┐  ┌──────────────────────┐  ┌──────────────────────┐
  │     可观测性层         │  │     消息驱动层         │  │     配置管理层         │
  ├──────────────────────┤  ├──────────────────────┤  ├──────────────────────┤
  │ • Sleuth (链路追踪)    │  │ • Stream (消息抽象)   │  │ • Config (配置中心)    │
  │ • Micrometer (指标)   │  │ • Bus (消息总线)      │  │ • Vault (密钥管理)    │
  │ • SkyWalking (APM)    │  │ • Function (函数式)   │  │ • Zookeeper (配置)    │
  └──────────────────────┘  └──────────────────────┘  └──────────────────────┘

三、核心组件速查表

组件层级一句话作用替代方案
Nacos注册+配置服务在哪?配置在哪?Eureka + Config、Consul
Gateway网关统一入口,路由转发Zuul、Kong、APISIX
OpenFeign调用远程调用像本地方法,内置负载均衡RestTemplate、gRPC、Dubbo
LoadBalancer均衡选哪个实例调用(Ribbon → LoadBalancer)Ribbon、Nginx
Sentinel容错挂了怎么办?流量太大怎么办?Hystrix、Resilience4j
Sleuth追踪请求经过了哪些服务?SkyWalking Agent、Jaeger
Stream消息解耦异步通信直接使用 MQ SDK
Bus总线配置变更广播Nacos 自带推送

四、版本演进与选型

Spring Cloud 版本命名

Spring Cloud 使用伦敦地铁站名作为版本号,按字母顺序迭代:

版本代号Spring Boot状态
Hoxton地铁站 H2.2.x / 2.3.x停止维护
2020.0.xIllford2.4.x / 2.5.x停止维护
2021.0.xJubilee2.6.x / 2.7.x维护中
2022.0.xKilburn3.0.x维护中
2023.0.xLeyton3.1.x / 3.2.x最新

从 2020.0 开始,Spring Cloud 改为日历版本号,不再使用伦敦地铁站名作为主版本号。

两大生态体系

Spring Cloud 生态
├── Spring Cloud Netflix (老牌,部分停维)
│   ├── Eureka        → 注册中心
│   ├── Ribbon        → 负载均衡
│   ├── Hystrix       → 熔断降级
│   ├── Zuul          → 网关
│   └── Archaius      → 配置

└── Spring Cloud Alibaba (阿里,活跃维护)
    ├── Nacos         → 注册中心 + 配置中心
    ├── Sentinel      → 熔断降级 + 流量控制
    ├── Seata         → 分布式事务
    ├── RocketMQ      → 消息队列
    └── Dubbo         → RPC 框架

当前推荐选型:Spring Cloud Alibaba + Spring Cloud Gateway + OpenFeign + LoadBalancer + Sleuth

五、一次请求的完整旅程

以一个电商下单请求为例,展示 Spring Cloud 各组件如何协同工作:

用户浏览器


┌──────────────────────────────────────────────────────────────────┐
│ 1. Gateway 网关                                                   │
│    - 路由匹配: /order/** → order-service                          │
│    - 鉴权: 校验 JWT Token                                         │
│    - 限流: 下单接口 QPS 限制                                       │
│    - 透传: 将 userId 放入请求头                                    │
└──────────────────────┬───────────────────────────────────────────┘


┌──────────────────────────────────────────────────────────────────┐
│ 2. LoadBalancer 负载均衡                                          │
│    - 从 Nacos 获取 order-service 的实例列表                         │
│    - 按轮询策略选择实例: 192.168.1.10:8081                         │
└──────────────────────┬───────────────────────────────────────────┘


┌──────────────────────────────────────────────────────────────────┐
│ 3. order-service (订单服务)                                       │
│    - 接收请求,开始处理订单业务                                      │
│    - 通过 OpenFeign 调用 inventory-service 扣减库存                 │
│    - 通过 OpenFeign 调用 payment-service 创建支付单                 │
└──────┬──────────────────────────────┬────────────────────────────┘
       │                              │
       ▼                              ▼
┌──────────────┐            ┌──────────────────┐
│ 4. inventory-│            │ 5. payment-      │
│    service   │            │    service        │
│  (库存服务)  │            │  (支付服务)        │
│              │            │                   │
│ Sentinel     │            │ Sentinel          │
│ 熔断保护     │            │ 熔断保护          │
└──────┬───────┘            └────────┬──────────┘
       │                              │
       ▼                              ▼
┌──────────────────────────────────────────────────────────────────┐
│ 6. 全链路追踪                                                     │
│    TraceId: a1b2c3d4-xxxx                                         │
│    ├── Span1: Gateway (2ms)                                       │
│    ├── Span2: order-service (50ms)                                │
│    │   ├── Span3: Feign→inventory-service (30ms)                  │
│    │   └── Span4: Feign→payment-service (15ms)                    │
│    └── 上报到 SkyWalking,可视化调用链                              │
└──────────────────────────────────────────────────────────────────┘

六、选型建议

场景推荐方案
新项目,快速启动Spring Cloud Alibaba 全家桶(Nacos + Sentinel + Gateway + Feign)
存量项目,已有 Netflix 组件逐步迁移到 Alibaba 或 Spring 官方替代品
超大规模(100+ 服务)考虑 Service Mesh(Istio)+ Spring Cloud 混合
对性能极度敏感gRPC + Nacos + Sentinel,不经过 HTTP 网关
需要跨语言调用gRPC 或 REST API + 独立网关(Kong / APISIX)