Appearance
Spring Cloud Alibaba 简介
Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案。此项目包含开发分布式应用微服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。
依托 Spring Cloud Alibaba,只需要添加一些注解和少量配置,就可以将 Spring Cloud 应用接入阿里微服务解决方案,通过阿里中间件来迅速搭建分布式应用系统。
- 官网:https://spring.io/projects/spring-cloud-alibaba#overview
- 官方文档:https://github.com/alibaba/spring-cloud-alibaba/wiki
Spring Cloud Alibaba 主要功能
- 服务限流降级:默认支持 WebServlet、WebFlux, OpenFeign、RestTemplate、Spring Cloud Gateway, Zuul, Dubbo 和 RocketMQ 限流降级功能的接入,可以在运行时通过控制台实时修改限流降级规则,还支持查看限流降级 Metrics 监控。
- 服务注册与发现:适配 Spring Cloud 服务注册与发现标准,默认集成了 Ribbon 的支持。
- 分布式配置管理:支持分布式系统中的外部化配置,配置更改时自动刷新。
- 消息驱动能力:基于 Spring Cloud Stream 为微服务应用构建消息驱动能力。
- 分布式事务:使用
@GlobalTransactional注解, 高效并且对业务零侵入地解决分布式事务问题。 - 阿里云对象存储:阿里云提供的海量、安全、低成本、高可靠的云存储服务。支持在任何应用、任何时间、任何地点存储和访问任意类型的数据。
- 分布式任务调度:提供秒级、精准、高可靠、高可用的定时(基于 Cron 表达式)任务调度服务。同时提供分布式的任务执行模型,如网格任务。网格任务支持海量子任务均匀分配到所有 Worker(schedulerx-client)上执行。
- 阿里云短信服务:覆盖全球的短信服务,友好、高效、智能的互联化通讯能力,帮助企业迅速搭建客户触达通道。
Spring Cloud Alibaba 相关组件
Spring Cloud Alibaba 组件包括了:
- Sentinel:把流量作为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
- Nacos:一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
- RocketMQ:一款开源的分布式消息系统,基于高可用分布式集群技术,提供低延时的、高可靠的消息发布与订阅服务。
- Dubbo:Apache Dubbo™ 是一款高性能 Java RPC 框架。
- Seata:阿里巴巴开源产品,一个易于使用的高性能微服务分布式事务解决方案。
- Alibaba Cloud OSS: 阿里云对象存储服务(Object Storage Service,简称 OSS),是阿里云提供的海量、安全、低成本、高可靠的云存储服务。您可以在任何应用、任何时间、任何地点存储和访问任意类型的数据。
- Alibaba Cloud SchedulerX: 阿里中间件团队开发的一款分布式任务调度产品,提供秒级、精准、高可靠、高可用的定时(基于 Cron 表达式)任务调度服务。
- Alibaba Cloud SMS: 覆盖全球的短信服务,友好、高效、智能的互联化通讯能力,帮助企业迅速搭建客户触达通道。
- Alibaba Cloud ACM:一款在分布式架构环境中对应用配置进行集中管理和推送的应用配置中心产品。
基础使用
引入依赖
如果需要使用已发布的版本,在 dependencyManagement 中添加如下配置。
xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.7.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>然后在 dependencies 中添加所需使用的依赖即可使用。
官方演示 Demo
为了演示如何使用,Spring Cloud Alibaba 项目包含了一个子模块spring-cloud-alibaba-examples。此模块中提供了演示用的 example ,可以阅读对应的 example 工程下的 readme 文档,根据里面的步骤来体验。
Example 列表:
- Sentinel Example
- Nacos Config Example
- Nacos Discovery Example
- RocketMQ Example
- Seata Example
- Alibaba Cloud OSS Example
- Alibaba Cloud SMS Example
- Alibaba Cloud SchedulerX Example
版本说明(2021.12.22日更新)
官方说明地址:https://github.com/alibaba/spring-cloud-alibaba/wiki/版本说明
组件版本关系
每个 Spring Cloud Alibaba 版本及其自身所适配的各组件对应版本如下表所示(注意,Spring Cloud Dubbo 从 2021.0.1.0 起已被移除出主干,不再随主干演进):
| Spring Cloud Alibaba Version | Sentinel Version | Nacos Version | RocketMQ Version | Dubbo Version | Seata Version |
|---|---|---|---|---|---|
| 2.2.10-RC1 | 1.8.6 | 2.2.0 | 4.9.4 | ~ | 1.6.1 |
| 2022.0.0.0-RC1 | 1.8.6 | 2.2.1-RC | 4.9.4 | ~ | 1.6.1 |
| 2.2.9.RELEASE | 1.8.5 | 2.1.0 | 4.9.4 | ~ | 1.5.2 |
| 2021.0.4.0 | 1.8.5 | 2.0.4 | 4.9.4 | ~ | 1.5.2 |
| 2.2.8.RELEASE | 1.8.4 | 2.1.0 | 4.9.3 | ~ | 1.5.1 |
| 2021.0.1.0 | 1.8.3 | 1.4.2 | 4.9.2 | ~ | 1.4.2 |
| 2.2.7.RELEASE | 1.8.1 | 2.0.3 | 4.6.1 | 2.7.13 | 1.3.0 |
| 2.2.6.RELEASE | 1.8.1 | 1.4.2 | 4.4.0 | 2.7.8 | 1.3.0 |
| 2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE | 1.8.0 | 1.4.1 | 4.4.0 | 2.7.8 | 1.3.0 |
| 2.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE | 1.8.0 | 1.3.3 | 4.4.0 | 2.7.8 | 1.3.0 |
| 2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE | 1.7.1 | 1.2.1 | 4.4.0 | 2.7.6 | 1.2.0 |
| 2.2.0.RELEASE | 1.7.1 | 1.1.4 | 4.4.0 | 2.7.4.1 | 1.0.0 |
| 2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE | 1.7.0 | 1.1.4 | 4.4.0 | 2.7.3 | 0.9.0 |
| 2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE | 1.6.3 | 1.1.1 | 4.4.0 | 2.7.3 | 0.7.1 |
毕业版本依赖关系(推荐使用)
由于 Spring Boot 3.0,Spring Boot 2.7~2.4 和 2.4 以下版本之间变化较大,目前企业级客户老项目相关 Spring Boot 版本仍停留在 Spring Boot 2.4 以下,为了同时满足存量用户和新用户不同需求,社区以 Spring Boot 3.0 和 2.4 分别为分界线,同时维护 2022.x、2021.x、2.2.x 三个分支迭代。如果不想跨分支升级,如需使用新特性,请升级为对应分支的新版本。为了规避相关构建过程中的依赖冲突问题,我们建议可以通过 云原生应用脚手架 进行项目创建。
2022.x 分支
适配 Spring Boot 3.0,Spring Cloud 2022.x 版本及以上的 Spring Cloud Alibaba 版本按从新到旧排列如下表(最新版本用*标记): (注意,该分支 Spring Cloud Alibaba 版本命名方式进行了调整,未来将对应 Spring Cloud 版本,前三位为 Spring Cloud 版本,最后一位为扩展版本,比如适配 Spring Cloud 2022.0.0 版本对应的 Spring Cloud Alibaba 第一个版本为:2022.0.0.0,第个二版本为:2022.0.0.1,依此类推)
| Spring Cloud Alibaba Version | Spring Cloud Version | Spring Boot Version |
|---|---|---|
2022.0.0.0-RC* | Spring Cloud 2022.0.0 | 3.0.0 |
2021.x 分支
适配 Spring Boot 2.4,Spring Cloud 2021.x 版本及以上的 Spring Cloud Alibaba 版本按从新到旧排列如下表(最新版本用*标记):
| Spring Cloud Alibaba Version | Spring Cloud Version | Spring Boot Version |
|---|---|---|
2021.0.4.0* | Spring Cloud 2021.0.4 | 2.6.11 |
| 2021.0.1.0 | Spring Cloud 2021.0.1 | 2.6.3 |
| 2021.1 | Spring Cloud 2020.0.1 | 2.4.2 |
2.2.x 分支
适配 Spring Boot 为 2.4,Spring Cloud Hoxton 版本及以下的 Spring Cloud Alibaba 版本按从新到旧排列如下表(最新版本用*标记):
| Spring Cloud Alibaba Version | Spring Cloud Version | Spring Boot Version |
|---|---|---|
2.2.10-RC1* | Spring Cloud Hoxton.SR12 | 2.3.12.RELEASE |
| 2.2.9.RELEASE | Spring Cloud Hoxton.SR12 | 2.3.12.RELEASE |
| 2.2.8.RELEASE | Spring Cloud Hoxton.SR12 | 2.3.12.RELEASE |
| 2.2.7.RELEASE | Spring Cloud Hoxton.SR12 | 2.3.12.RELEASE |
| 2.2.6.RELEASE | Spring Cloud Hoxton.SR9 | 2.3.2.RELEASE |
| 2.2.1.RELEASE | Spring Cloud Hoxton.SR3 | 2.2.5.RELEASE |
| 2.2.0.RELEASE | Spring Cloud Hoxton.RELEASE | 2.2.X.RELEASE |
| 2.1.4.RELEASE | Spring Cloud Greenwich.SR6 | 2.1.13.RELEASE |
| 2.1.2.RELEASE | Spring Cloud Greenwich | 2.1.X.RELEASE |
| 2.0.4.RELEASE(停止维护,建议升级) | Spring Cloud Finchley | 2.0.X.RELEASE |
| 1.5.1.RELEASE(停止维护,建议升级) | Spring Cloud Edgware | 1.5.X.RELEASE |
依赖管理
Spring Cloud Alibaba BOM 包含了它所使用的所有依赖的版本(RELEASE 版本)。
spring-cloud-alibaba-dependencies
在 pom.xml 中的 dependencyManagement 中添加 spring-cloud-alibaba-dependencies 依赖项:
xml
<!-- 版本管理 -->
<properties>
<!--
不同的 Spring Cloud 相应的 Spring Cloud Alibaba 版本不一样,如:
使用 Spring Cloud 2020 时相应的版本是 2021.1
使用 Spring Cloud Hoxton 时相应的版本是 2.2.7.RELEASE
使用 Spring Cloud Greenwich 时相应的版本是 2.1.4.RELEASE
使用 Spring Cloud Finchley 时相应的版本是 2.0.4.RELEASE
使用 Spring Cloud Edgware 时相应的版本是 1.5.1.RELEASE
-->
<spring-cloud-alibaba.version>2.1.4.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>aliyun-spring-boot-dependencies
如果想使用阿里云服务的 Spring Boot Starters,在 pom.xml 添加 Aliyun Spring Boot BOM 的依赖。
xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>aliyun-spring-boot-dependencies</artifactId>
<version>{project-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>Spring Cloud Alibaba 示例微服务环境搭建准备
此学习 Spring Cloud Alibaba 的示例项目,主要包含商品、订单、用户等模块
- 示例项目地址:https://github.com/MooNkirA/spring-cloud-note/spring-cloud-alibaba-2.1.x-sample
- 分支:feature-alibaba-2.1.x
示例项目说明
后端技术栈
| 技术 | 版本 | 说明 |
|---|---|---|
| Sping Data Jpa | 数据持久层框架 | |
| Spring Boot | 2.1.13.RELEASE | |
| Spring Cloud | Greenwich.RELEASE | 微服务框架 |
| Spring Cloud Alibaba | 2.1.2.RELEASE | 微服务框架 |
| mysql-connector-java | 5.1.48 | |
| fastjson | 1.2.76 |
项目开发环境
| 工具 | 版本 | 说明 |
|---|---|---|
| JDK | 1.8 | |
| Mysql | 5.7 | 数据库 |
| maven | 3.8.4 | 项目构建工具 |
| nacos | 2.0.3 | 服务注册中心 |
涉及模块设计
| 服务(项目)名 | 部署端口 | 说明 |
|---|---|---|
| spring-cloud-alibaba-2.1.x-sample | 父项目 | |
| modules-common | 公共模块:实体类、工具类 | |
| service-user | 807x | 用户微服务 |
| service-product | 808x | 商品微服务 |
| service-order | 809x | 订单微服务 |
| api-gateway | 7000 | 网关服务 |
| 第三方应用服务 | 部署端口 | 说明 |
|---|---|---|
| Nacos server | 8848 | nacos 服务注册中心 |
| Sentinel server | 8080 | Sentinel 微服务容错 |
示例项目初始化
创建聚合项目
创建 pom 类型工程 spring-cloud-alibaba-2.1.x-sample,在pom.xml文件中添加相关依赖
xml
<!-- Spring Boot 父项目依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.13.RELEASE</version>
</parent>
<!-- 版本管理 -->
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Greenwich.SR6</spring-cloud.version>
<spring-cloud-alibaba.version>2.1.4.RELEASE</spring-cloud-alibaba.version>
<fastjson.version>1.2.76</fastjson.version>
<mysql-connector.version>5.1.48</mysql-connector.version>
</properties>
<!-- 依赖版本的锁定 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>创建公共模块
- 创建jar类型工程 modules-common,添加相关依赖
xml
<!-- 配置公共依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector.version}</version>
</dependency>
</dependencies>- 创建相应数据库表的实体类
创建三个业务模块的微服务
分别创建用户、商品、订单三个微服务工程。三个工程的步骤一样,下面以创建用户service-user工程为示例:
- 创建jar类型工程,依赖相关依赖
xml
<dependencies>
<!-- springboot-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- common 模块 -->
<dependency>
<groupId>com.moon</groupId>
<artifactId>modules-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>- 创建应用启动类
java
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}- 创建项目的配置文件 application.yml。
yml
server:
port: 8071
spring:
application:
name: service-user
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///springcloud_alibaba_sample_db?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: 123456
jpa:
properties:
hibernate:
hbm2ddl:
auto: update
# 指定数据库方言
dialect: org.hibernate.dialect.MySQL5InnoDBDialect用户微服务端口:8071、商品微服务端口:8081、订单微服务端口:8091
- 创建必要的接口和实现类(controller service dao)
注:示例项目创建公共模块时,实体类的包路径不包含在各个微服务的启动类所扫描的路径下,如果需要使用
@EntityScan("com.moon.domain")注解,指定扫描JPA实体类的包路径,否则会报找不到JpaRepository接口泛型指定的实体类的错误。