5
0

Spring Cloud 进阶

2026-05-11

Spring Cloud 进阶教程

一、主流微服务架构设计模式

1.1 分层架构(标准微服务架构)

┌─────────────────────────────────────────────────────────┐
│                    客户端层                              │
│              Web / App / H5 / 小程序                     │
└────────────────────────┬────────────────────────────────┘
                         │
┌────────────────────────▼────────────────────────────────┐
│                    接入层 (Gateway)                      │
│      Spring Cloud Gateway  /  Kong  /  Nginx            │
│      限流、鉴权、路由、日志、跨域                         │
└────────────────────────┬────────────────────────────────┘
                         │
┌────────────────────────▼────────────────────────────────┐
│                    业务服务层                            │
│  ┌──────────┐  ┌──────────┐  ┌──────────┐              │
│  │ 用户服务  │  │ 订单服务  │  │ 商品服务  │   ...        │
│  └──────────┘  └──────────┘  └──────────┘              │
│       Service Mesh (Sidecar 模式) — 可选                │
└────────────────────────┬────────────────────────────────┘
                         │
┌────────────────────────▼────────────────────────────────┐
│                    基础设施层                            │
│  Nacos/Eureka    Sentinel      Seata        SkyWalking │
│  (注册/配置)     (熔断/限流)   (分布式事务)   (链路追踪)  │
└────────────────────────┬────────────────────────────────┘
                         │
┌────────────────────────▼────────────────────────────────┐
│                    数据存储层                            │
│  MySQL / Redis / Elasticsearch / RocketMQ / MinIO      │
└─────────────────────────────────────────────────────────┘

1.2 DDD 领域驱动架构(复杂业务场景)

┌─────────────── 四层架构 ───────────────┐
│                                         │
│  ┌─────────────────────────────────┐    │
│  │     Interface (接口层)          │    │
│  │     Controller / DTO / VO       │    │
│  └──────────────┬──────────────────┘    │
│                 │                       │
│  ┌──────────────▼──────────────────┐    │
│  │     Application (应用层)         │    │
│  │     ApplicationService / CQRS   │    │
│  └──────────────┬──────────────────┘    │
│                 │                       │
│  ┌──────────────▼──────────────────┐    │
│  │     Domain (领域层)              │    │
│  │     Entity / ValueObject        │    │
│  │     DomainService / Aggregate   │    │
│  └──────────────┬──────────────────┘    │
│                 │                       │
│  ┌──────────────▼──────────────────┐    │
│  │     Infrastructure (基础设施层)  │    │
│  │     Repository / MQ / Cache     │    │
│  └─────────────────────────────────┘    │
└─────────────────────────────────────────┘

1.3 CQRS 读写分离架构(高并发读场景)

          ┌─────────┐
          │  Gateway │
          └────┬─────┘
               │
     ┌─────────┴─────────┐
     │                   │
┌────▼────┐        ┌─────▼─────┐
│  Command │        │  Query    │
│  Service │        │  Service  │
│  (写服务) │        │  (读服务)  │
└────┬─────┘        └─────┬─────┘
     │                    │
┌────▼─────┐        ┌─────▼─────┐
│  MySQL   │  ──▶   │  Redis    │
│ (写入库)  │  同步   │  (查询库)  │
└──────────┘  CDC   └───────────┘

1.4 事件驱动架构(异步解耦场景)

用户下单 ──▶ OrderService ──▶ MQ (RocketMQ/Kafka)
                                  │
                ┌─────────────────┼─────────────────┐
                ▼                 ▼                  ▼
          InventoryService   CouponService    NotifyService
          (扣减库存)          (核销优惠券)      (发送通知)

1.5 多种架构对比

架构模式适用场景复杂度核心组件
分层架构通用业务Gateway + Nacos + Feign
DDD 领域驱动复杂业务逻辑四层划分 + 聚合根
CQRS 读写分离高并发读多写少MySQL + Redis + Canal
事件驱动异步解耦RocketMQ / Kafka
Service Mesh多语言/精细化治理Istio + Envoy Sidecar
单元化架构异地多活/合规隔离极高分片路由 + 数据同步

二、核心工具生态

2.1 技术选型全景图

┌─────────────────────────────────────────────────────────┐
│                     类别 & 推荐方案                       │
├────────────────────┬────────────────────────────────────┤
│  注册中心/配置中心   │  Nacos (首选) / Consul / Apollo    │
│  网关              │  Spring Cloud Gateway / Kong       │
│  远程调用           │  OpenFeign + LoadBalancer          │
│  熔断降级/限流      │  Sentinel (首选) / Resilience4j    │
│  分布式事务         │  Seata / RocketMQ事务消息           │
│  链路追踪           │  SkyWalking / Zipkin + Micrometer   │
│  日志收集           │  ELK (ES + Logstash + Kibana)     │
│  分布式缓存         │  Redis / Caffeine (本地)           │
│  消息队列           │  RocketMQ / Kafka / RabbitMQ       │
│  分布式定时任务      │  XXL-JOB / PowerJob               │
│  对象存储           │  MinIO / OSS / COS                 │
│  CI/CD             │  Jenkins / GitHub Actions          │
│  容器编排           │  Docker + K8s / Docker Compose     │
│  监控告警           │  Prometheus + Grafana              │
│  数据同步           │  Canal (MySQL Binlog → Redis/ES)   │
│  权限认证           │  Spring Security + OAuth2 / Sa-Token│
└────────────────────┴────────────────────────────────────┘

2.2 主流工具对比

功能方案A方案B推荐
注册中心NacosEureka 2.x (停更)Nacos
配置中心Nacos ConfigApolloNacos(一站式)
熔断降级SentinelHystrix (停更)Sentinel
网关Spring Cloud GatewayKongGateway(Java生态)
链路追踪SkyWalkingZipkinSkyWalking(无侵入)
分布式事务SeataRocketMQ事务消息Seata(通用)
消息队列RocketMQKafkaRocketMQ(事务消息)

三、各大云平台 Spring Cloud 方案

3.1 阿里云

阿里云对 Spring Cloud Alibaba 有最完善的集成支持,是 Spring Cloud 微服务在阿里云上的首选方案。

核心产品映射

自建组件阿里云托管服务说明
NacosMSE Nacos (微服务引擎)全托管注册中心/配置中心,免运维
SentinelAHAS (应用高可用服务)可视化熔断/限流/降级管控
SeataGTS (全局事务服务)分布式事务,性能优于开源版
SkyWalkingARMS (应用实时监控)链路追踪+性能监控,免部署
RocketMQRocketMQ 商业版消息队列,兼容开源协议
GatewayAPI 网关企业级网关,支持插件扩展
ELKSLS (日志服务)一站式日志采集/存储/分析
GrafanaARMS Prometheus托管 Prometheus + Grafana
K8sACK (容器服务)托管 K8s,集成 MSE 微服务治理

阿里云 MSE 微服务架构方案

┌──────────────────────────────────────────────────────────┐
│                     阿里云 MSE 微服务全家桶                │
│                                                          │
│  ┌──────────┐   ┌──────────┐   ┌──────────┐             │
│  │ MSE Nacos│   │ Sentinel │   │   Seata  │             │
│  │ 注册/配置 │   │ 熔断/限流 │   │ 分布式事务│             │
│  └─────┬────┘   └────┬─────┘   └────┬─────┘             │
│        │             │              │                   │
│  ┌─────┴─────────────┴──────────────┴─────┐             │
│  │         Spring Cloud 业务服务           │             │
│  │         (部署在 ACK/ECS)                │             │
│  └───────────────┬───────────────────────┘             │
│                  │                                      │
│  ┌───────────────┴───────────────────────┐             │
│  │  ARMS (监控)  │  SLS (日志) │  GTS (事务)│            │
│  └───────────────────────────────────────┘             │
└──────────────────────────────────────────────────────────┘

阿里云集成依赖

<dependencyManagement>
    <dependencies>
        <!-- Spring Cloud Alibaba 保持标准 BOM -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2024.0.1.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- 阿里云 SDK BOM(统一管理云产品版本) -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-spring-boot-dependencies</artifactId>
            <version>1.0.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <!-- OSS 对象存储 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alicloud-oss</artifactId>
    </dependency>
    <!-- SMS 短信 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alicloud-sms</artifactId>
    </dependency>
</dependencies>

阿里云部署配置要点

spring:
  application:
    name: user-service
  cloud:
    nacos:
      discovery:
        # MSE Nacos 内网域名
        server-addr: mse-xxx-nacos-ans.mse.aliyuncs.com:8848
        namespace: prod
      config:
        server-addr: mse-xxx-nacos-ans.mse.aliyuncs.com:8848
        namespace: prod
    alicloud:
      access-key: ${ALIYUN_ACCESS_KEY}       # 环境变量注入
      secret-key: ${ALIYUN_SECRET_KEY}
      oss:
        endpoint: oss-cn-hangzhou.aliyuncs.com

阿里云 K8s(ACK) + MSE 部署

apiVersion: apps/v1
kind: Deployment
metadata:
  name: user-service
  # 注入 MSE 治理 Sidecar(无侵入)
  annotations:
    msePilotAutoEnable: "on"
    msePilotCreateAppName: user-service
spec:
  replicas: 3
  template:
    spec:
      containers:
        - name: user-service
          image: registry.cn-hangzhou.aliyuncs.com/demo/user-service:1.0.0
          env:
            - name: JAVA_TOOL_OPTIONS
              value: "-Dspring.cloud.nacos.discovery.server-addr=mse-xxx.nacos-ans.mse.aliyuncs.com:8848"

3.2 腾讯云

腾讯云面向 Spring Cloud 提供北极星(Polaris)作为原生的服务治理方案,同时兼容 Nacos/Eureka。

核心产品映射

自建组件腾讯云托管服务说明
Nacos/EurekaPolaris (北极星) 推荐腾讯自研注册中心/配置中心,兼容 Spring Cloud
Nacos/EurekaTSF NacosTSF 内置托管 Nacos
SentinelTSF 服务限流/熔断TSF 微服务平台内置
GatewayTSF 网关 / API 网关微服务网关 / 企业级 API 网关
SkyWalkingAPM (应用性能监控)链路追踪 + 性能分析
RocketMQ/KafkaTDMQ (消息队列)兼容 RocketMQ/Pulsar/Kafka 协议
ELKCLS (日志服务)日志采集/存储/检索
GrafanaTMP (托管 Prometheus)云原生监控 + Grafana
K8sTKE (容器服务)托管 K8s 集群

腾讯云 TSF 微服务架构方案

腾讯云提供两套微服务治理方案:

方案适用场景注册中心
TSF + Polaris (推荐)新项目,腾讯云原生Polaris
TSF + NacosSpring Cloud Alibaba 迁移TSF 托管 Nacos
TKE + Polaris 开源版自建 K8s,低成本开源 Polaris
┌──────────────────────────────────────────────────────────┐
│                    腾讯云 TSF 架构                        │
│                                                          │
│  ┌─────────────────────────────────────────────────┐     │
│  │              TSF 微服务平台 (PaaS)               │     │
│  │  ┌──────────┐  ┌──────────┐  ┌──────────┐      │     │
│  │  │ Polaris  │  │  服务限流  │  │  配置管理  │      │     │
│  │  │ 注册/发现 │  │  熔断降级  │  │  动态发布  │      │     │
│  │  └──────────┘  └──────────┘  └──────────┘      │     │
│  └─────────────────────────────────────────────────┘     │
│                          │                               │
│  ┌───────────────────────┴──────────────────────────┐    │
│  │    Spring Cloud 微服务 (部署在 TKE / CVM)         │    │
│  └───────────────────────┬──────────────────────────┘    │
│                          │                               │
│  ┌───────────────────────┴──────────────────────────┐    │
│  │  CLS (日志)  │  TMP (监控)  │  TDMQ (消息)      │    │
│  └──────────────────────────────────────────────────┘    │
└──────────────────────────────────────────────────────────┘

腾讯云集成依赖

<!-- 方式1: TSF 全量 SDK -->
<dependency>
    <groupId>com.tencent.tsf</groupId>
    <artifactId>spring-cloud-tsf-starter</artifactId>
    <version>2.0.0</version>
</dependency>

<!-- 方式2: 仅接入 Polaris 注册中心(轻量) -->
<dependency>
    <groupId>com.tencent.polaris</groupId>
    <artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
    <version>1.10.0</version>
</dependency>

<!-- COS 对象存储 -->
<dependency>
    <groupId>com.qcloud</groupId>
    <artifactId>cos_api</artifactId>
    <version>5.6.x</version>
</dependency>

腾讯云部署配置

spring:
  application:
    name: user-service
  cloud:
    # 方式1: Polaris 注册中心
    polaris:
      address: grpc://10.0.0.1:8091
      namespace: production
      service: user-service
    # 方式2: TSF Nacos
    tencent:
      tsf:
        consul:
          host: tsf-nacos.internal.tencentcloudapi.com
          port: 8848

3.3 百度云

百度云没有推出类似 TSF/MSE 的微服务全家桶,首选方案是在 BCC(云服务器) 或 CCE(容器) 上自建 Spring Cloud 基础设施

核心产品映射

自建组件百度云托管服务说明
Nacos自建 Nacos (BCC/CCE)无托管版,自建部署
Sentinel自建 Sentinel自建 Dashboard
SkyWalking自建 SkyWalking自建 OAP Server
RocketMQ自建 RocketMQ (BCC)无托管版
ELKBLS (日志服务)百度日志服务
GrafanaBCM (云监控)监控告警平台
K8sCCE (容器引擎)托管 K8s
对象存储BOS (对象存储)兼容 S3 协议

百度云自建架构方案

┌──────────────────────────────────────────────────────────┐
│              百度云 BCC/CCE 自建微服务架构                 │
│                                                          │
│  ┌─────────────────────────────────────────────────────┐ │
│  │  CCE (K8s) / BCC (云服务器)                         │ │
│  │                                                     │ │
│  │  ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌──────────┐ │ │
│  │  │  Nacos  │ │Sentinel │ │SkyWalking│ │ RocketMQ │ │ │
│  │  │  集群    │ │Dashboard│ │ OAP+UI  │ │  集群    │ │ │
│  │  └─────────┘ └─────────┘ └─────────┘ └──────────┘ │ │
│  │                                                     │ │
│  │  ┌──────────────────────────────────────┐          │ │
│  │  │     Spring Cloud 业务服务 (Deployment)│          │ │
│  │  └──────────────────────────────────────┘          │ │
│  └─────────────────────────────────────────────────────┘ │
│                         │                                │
│  ┌──────────────────────┴──────────────────────────┐    │
│  │  BOS (存储) │ BLS (日志) │ BCM (监控) │ RDS (DB)│    │
│  └─────────────────────────────────────────────────┘    │
└──────────────────────────────────────────────────────────┘

百度云集成依赖

<!-- BOS 对象存储 -->
<dependency>
    <groupId>com.baidubce</groupId>
    <artifactId>bce-java-sdk</artifactId>
    <version>0.10.x</version>
</dependency>
<!-- 或使用 S3 兼容 SDK (MinIO Client 可通用) -->
<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.5.x</version>
</dependency>

百度云部署配置

spring:
  cloud:
    nacos:
      discovery:
        # 内网 BCC 搭建的 Nacos 集群 IP
        server-addr: 10.0.0.1:8848,10.0.0.2:8848,10.0.0.3:8848

# BOS 配置
baidu:
  bos:
    endpoint: https://bj.bcebos.com
    access-key: ${BOS_AK}
    secret-key: ${BOS_SK}
    bucket: my-bucket

3.4 多云方案对比总览

维度阿里云腾讯云百度云
微服务治理MSE (全托管)TSF (全托管)自建 (CCE/BCC)
注册中心MSE NacosPolaris/TSF Nacos自建 Nacos
配置中心MSE Nacos ConfigTSF 配置管理自建 Nacos Config
网关API 网关API 网关 + TSF 网关自建 Gateway
监控ARMSAPMBCM (基础)
日志SLSCLSBLS
事务GTSSeata (TSF集成)自建 Seata
消息队列RocketMQ 商业版TDMQ自建 RocketMQ
容器服务ACKTKECCE
对象存储OSSCOSBOS
微服务成熟度★★★★★★★★★☆★★☆☆☆
Spring Cloud Alibaba兼容性★★★★★ (原生)★★★★☆ (兼容)★★★☆☆ (自建)

四、云原生架构演进方向

4.1 从 Spring Cloud 到 Service Mesh

阶段1               阶段2               阶段3
Spring Cloud       Spring Cloud +      Service Mesh
                   Sidecar             (Istio)

┌─────────┐       ┌─────────────┐     ┌─────────────┐
│ 业务代码  │       │ 业务代码      │     │ 业务代码 (纯净)│
│ +Ribbon  │  ──▶  │ Sidecar 代理 │ ──▶ │             │
│ +Feign   │       │ (Envoy/Mosn) │     │ Envoy (独立) │
│ +Hystrix │       └─────────────┘     └─────────────┘
└─────────┘
  SDK 耦合           代理分离            完全解耦

4.2 阿里云 ASM (Service Mesh)

在 ACK 上通过 ASM 实现 Istio 托管,无需在代码中引入 Spring Cloud 治理依赖。

4.3 选择合适的架构

项目规模 & 团队能力

小型 (1-5个服务)     ──▶  Spring Cloud 标准方案
中型 (5-20个服务)    ──▶  Spring Cloud + 云平台托管 (MSE/TSF)
大型 (20+服务)       ──▶  云平台托管 + Service Mesh (ASM)
超大型 (多团队)      ──▶  Service Mesh + 单元化架构

五、参考文档