用 Spring Cloud Alibaba 搭一套可跑的微服务:2026稳定版组合与落地步骤
面向要在本地把注册发现、配置中心、网关、限流和分布式事务串起来的后端同学。本稿按掘金公开教程《Spring Cloud 微服务完整教程(2026版)》整理可复现步骤,技术栈以 Spring Cloud Alibaba 为主。[1]
准备工作
推荐版本组合(教程标注为2026生产稳定向):
组件 | 版本 |
|---|---|
JDK | 17 / 21 LTS |
Spring Boot | 3.2.5 |
Spring Cloud | 2023.0.2 (Leyton) |
Spring Cloud Alibaba | 2023.0.1.2 |
Nacos | 2.3.0 |
Sentinel | 1.8.7 |
Seata | 2.0.0 |
Gateway | 4.1.0 |
对应关系:Spring Cloud Alibaba 2023.0.1.x → Spring Cloud 2023.0.x → Spring Boot 3.2.x。[1]
先建空 Maven 父工程 springcloud-demo(packaging=pom),在 dependencyManagement 中 import 上述 BOM,子模块不再各自写版本号。
操作步骤
1、启动 Nacos
下载 Nacos 2.3.0,单机模式启动:sh bin/startup.sh -m standalone(Windows 用 startup.cmd)。浏览器打开 http://localhost:8848/nacos,默认账号密码均为 nacos。确认控制台能登录,再继续建服务。[1]
2、搭建服务提供者并注册
新建子模块 provider-service,引入 spring-boot-starter-web 与 spring-cloud-starter-alibaba-nacos-discovery。application.yml 示例:
server:
port: 8001
spring:
application:
name: provider-service
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
namespace: public
group: DEFAULT_GROUP启动类加 @SpringBootApplication(@EnableDiscoveryClient 在较新版本可省略)。提供 /provider/hello 接口后启动,在 Nacos「服务列表」应能看到 provider-service。[1]
3、搭建消费者:RestTemplate 或 OpenFeign
消费者端口可用 8080,同样接入 Nacos。用 RestTemplate 时加 @LoadBalanced,请求 URL 写服务名:http://provider-service/provider/hello。或改用 OpenFeign:引入 spring-cloud-starter-openfeign,启动类 @EnableFeignClients,接口上 @FeignClient("provider-service")。访问消费者接口能返回提供者文案,即负载调用打通。[1]
4、接入 Nacos 配置中心
引入 spring-cloud-starter-alibaba-nacos-config;Spring Boot 3.x 还需 spring-cloud-starter-bootstrap。在 bootstrap.yml 配置 server-addr、file-extension: yaml。于控制台新建 Data ID=provider-service.yaml,用 @Value + @RefreshScope 读取。改配置并发布后,不重启应能读到新值。[1]
5、加 Gateway 统一入口
新建 gateway-service(如端口9000),依赖 Gateway + Nacos Discovery。路由用 uri: lb://provider-service,Path 断言匹配后转发。确认直连提供者与经网关访问都能通。[1]
6、Sentinel 限流与 Feign 降级(可选进阶)
引入 spring-cloud-starter-alibaba-sentinel,配置 dashboard 地址。控制台对接口设 QPS 限流后,超限应看到 Sentinel 拦截。若用 Feign,打开 feign.sentinel.enabled=true 并配置 fallback 类。[1]
7、Seata AT 分布式事务(可选)
业务库建 undo_log 表,服务引入 Seata starter,注册到 Nacos;发起方方法加 @GlobalTransactional。任意分支异常应触发全局回滚。此步依赖 Seata Server 与多库环境,没有多服务写库场景可先跳过。[1]
学习顺序上,建议先打通「Nacos注册 + 一次远程调用 + Gateway」,再加配置中心与 Sentinel;Seata 放到最后,因为要额外的 Server 与 undo_log。这样每一步都有可观察的成功信号(服务出现在控制台、浏览器打到返回值、超限被拦截),排错成本更低。
注意事项
版本必须按 BOM 对齐,混用 Boot 2.x / Cloud 2021 线容易踩兼容坑。
配置中心在 Boot 3 默认不加载 bootstrap,漏加 starter 会表现为「读不到远程配置」。
网关
StripPrefix与路径断言要一起核对,否则容易 404。教程中的端口、账号为本地演示默认值,上线请改密钥与命名空间隔离。
以下步骤基于掘金2026版公开教程整理,于2026年10月27日对照原文核实;组件小版本请以官方发布说明为准。
来源
[1] 掘金《一份不可多得的〈微服务〉教程:Spring Cloud 微服务完整教程(2026版)》,2026年6月23日。
#微服务 #SpringCloudAlibaba #Nacos #Gateway #Sentinel #Seata
成为第一条评论!
Nobody's responded to this post yet. Add your thoughts and get the conversation going.