# spring-cloud-examples **Repository Path**: code-org/spring-cloud-examples ## Basic Information - **Project Name**: spring-cloud-examples - **Description**: 当前是一个 spring-cloud-examples 实战示例项目 - **Primary Language**: Java - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-04-15 - **Last Updated**: 2026-04-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # spring-cloud-examples ## 介绍 这是一个基于 **Spring Cloud 2021.x** 的微服务实战示例项目,涵盖服务注册、配置管理、网关路由、服务调用、熔断限流、分布式事务、消息队列、链路追踪等完整的微服务生态体系。 **核心技术栈:** - Spring Boot 2.7.18 | Spring Cloud Alibaba 2021.0.5.0 - 服务注册:Nacos 2.2.0 - 服务网关:Spring Cloud Gateway - 消息队列:RocketMQ / Kafka - 分布式事务:Seata - 服务治理:Sentinel(熔断限流) - 链路追踪:SkyWalking / Zipkin - ORM 框架:MyBatis-Plus ## 快速开始 ### 环境要求 | 环境 | 版本要求 | 备注 | |------|---------|------| | **Java 版本** | 1.8 或更高 | 推荐使用 JDK 1.8+ | | **Maven** | 3.6.0+ | 项目构建工具 | | **Nacos** | 2.2.0+ | 服务注册与配置中心 | | **RocketMQ** | 4.9.4+ | 消息队列(可选) | | **MySQL** | 5.7 或 8.0 | 数据库(可选) | ### 本地构建与运行 ```bash # 1. 克隆项目 git clone https://gitee.com/code-org/spring-cloud-examples.git cd spring-cloud-examples # 2. 编译项目 mvn clean install # 3. 启动 Nacos(详见官方文档) # https://nacos.io/zh-cn/docs/deployment.html # 4. 启动服务 # 修改 application.yml 配置 Nacos 地址 # 然后运行你想测试的服务模块 mvn spring-boot:run -pl your-module-name ``` --- ## 版本架构说明 ### 兼容性说明 官方 2021.x 分支对应的 Spring Boot 版本是 2.6.x(如 2.6.3、2.6.11、2.6.13),并非 2.7.x。 但实际上 Spring Boot 2.7.x 在 2021.x 分支下**可以使用**,只是官方没有列出正式的 2.7.x 对应版本,选用 `2021.0.5.0` 是目前兼容性最好的选择(有大量社区实践验证)。 --- 以下是基于官方组件版本关系表,为你补全的完整版本栈: ```markdown Spring Boot 2.7.18 (JDK 1.8) └── Spring Cloud Alibaba 2021.0.5.0 ├── Spring Cloud 2021.0.5 ├── 注册/配置 → Nacos 2.2.0 ├── 网关 → Spring Cloud Gateway 3.1.5 (随 Spring Cloud 2021.0.5) ├── 调用 → OpenFeign 3.1.x (随 Spring Cloud 2021.0.5) │ Dubbo 2.7.x (需单独引入,已移出主干) ├── 负载均衡 → Spring Cloud LoadBalancer 3.1.x (推荐) │ Ribbon (Spring Cloud 2020+ 已软弃用,不推荐新项目) ├── 熔断限流 → Sentinel 1.8.6 ├── 分布式事务 → Seata 1.6.1 ├── 消息 → RocketMQ 4.9.4 │ Kafka 3.x (独立维护,不在 SCA BOM 内) ├── 链路追踪 → SkyWalking Agent 8.x / 9.x (独立部署,不在 BOM 内) │ Zipkin 2.x + Spring Cloud Sleuth 3.1.x └── ORM → MyBatis-Plus 3.5.x (独立维护,不在 SCA BOM 内) ``` **注意事项:** **Dubbo:** 从 2021.0.1.0 起 Spring Cloud Dubbo 已被移除出主干,不再随主干演进。如需使用 Dubbo,要单独引入 `dubbo-spring-boot-starter`,推荐 `2.7.15` 或 `3.0.x`(3.x 兼容 JDK 8)。 **Ribbon vs LoadBalancer:** Spring Cloud 2021.x 起 Ribbon 已正式弃用,新项目直接用 `spring-cloud-starter-loadbalancer`。 **SkyWalking:** 以 Agent 方式无侵入接入,版本与 Spring Boot 无强绑定,推荐 `8.15.0` 或 `9.x`(均支持 JDK 8)。 **pom.xml 核心依赖配置参考:** ```xml 2.7.18 2021.0.5 2021.0.5.0 3.5.5 1.6.1 2.7.15 org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies ${spring-cloud-alibaba.version} pom import ``` --- ## 安装教程 ### 1. 前置依赖安装 **JDK 安装** ```bash # Windows 用户:下载 JDK 1.8 或以上版本 # https://www.oracle.com/java/technologies/javase-downloads.html # 或使用开源发行版 (OpenJDK, Eclipse Temurin 等) # 验证安装 java -version ``` **Maven 安装** ```bash # 下载 Maven 3.6.0 或以上版本 # http://maven.apache.org/download.cgi # 验证安装 mvn -v ``` ### 2. Nacos 部署(必需) ```bash # 从官方下载 Nacos(推荐 2.2.0+) # https://github.com/alibaba/nacos/releases # 启动 Nacos(单机模式) cd nacos/bin # Windows startup.cmd -m standalone # Linux/Mac ./startup.sh -m standalone # Nacos 控制台:http://localhost:8848/nacos # 默认用户名/密码:nacos/nacos ``` ### 3. 可选中间件部署 **RocketMQ(如使用消息队列功能)** - 参考:[RocketMQ 官方文档](https://rocketmq.apache.org/docs/quick-start/) **Seata(如使用分布式事务)** - 参考:[Seata 官方文档](http://seata.io/zh-cn/docs/user/quickstart.html) **SkyWalking(如使用链路追踪)** - 参考:[SkyWalking 官方文档](https://skywalking.apache.org/docs/main/latest/en/setup/backend/quick-start/) --- ## 使用说明 ### 项目结构示例 ``` spring-cloud-examples/ ├── common/ # 公共模块(工具类、常量等) ├── service-a/ # 微服务 A(服务提供方) │ ├── pom.xml │ └── src/ ├── service-b/ # 微服务 B(服务消费方) │ ├── pom.xml │ └── src/ ├── gateway/ # API 网关模块 │ ├── pom.xml │ └── src/ └── pom.xml # 父 POM 文件 ``` ### 核心配置文件 **application.yml(Nacos 配置)** ```yaml spring: application: name: your-service-name # 服务名称 cloud: nacos: server-addr: localhost:8848 # Nacos 地址 config: namespace: public # 命名空间 group: DEFAULT_GROUP # 配置分组 discovery: namespace: public group: DEFAULT_GROUP server: port: 8080 # 服务端口 ``` ### 常见使用场景 **场景 1:调用其他服务(OpenFeign)** ```java @FeignClient("service-name") public interface ServiceClient { @GetMapping("/api/resource") ResponseEntity queryData(); } ``` **场景 2:路由配置(Spring Cloud Gateway)** ```yaml spring: cloud: gateway: routes: - id: service-route uri: lb://service-name # 负载均衡 predicates: - Path=/service/** ``` **场景 3:限流熔断(Sentinel)** ```java @RestController @RequestMapping("/api") public class Controller { @GetMapping("/resource") @SentinelResource(value = "queryResource", fallback = "fallback") public ResponseEntity queryData() { return // your logic } public ResponseEntity fallback() { return ResponseEntity.ok().body(null); } } ``` ### 常见问题排查 | 问题 | 解决方案 | |------|--------| | 服务注册失败 | 1. 检查 Nacos 正常运行
2. 验证 `server-addr` 配置是否正确 | | 服务间调用超时 | 1. 检查 Ribbon 负载均衡配置
2. 调整 `ReadTimeout` 和 `ConnectTimeout` | | 端口被占用 | 修改 `application.yml` 中的 `server.port` | | 配置未生效 | 检查 Nacos 配置的 `namespace` 和 `group` 是否正确 | --- ## 参与贡献 我们欢迎各种形式的贡献! ### 贡献流程 1. **Fork 本仓库** - 在 Gitee 上 Fork 项目 2. **新建分支** - 基于 `main` 分支创建特性分支 ```bash git checkout -b feature/your-feature-name ``` 3. **开发与提交** - 进行开发并提交代码(遵循 [Commit 规范](#commit-规范)) 4. **推送分支** - 将分支推送到 Fork 后的仓库 5. **发起 Pull Request** - 提交 PR,填写详细的变更说明 ### Commit 规范 ``` ():