Spring Cloud Alibaba

Spring Cloud Alibaba 组件包括了 Sentinel、Nacos、RocketMQ、Dubbo、Seata、Alibaba Cloud OSS、Alibaba Cloud SchedulerX、Alibaba Cloud SMS

1. 简介

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

    2. 主要功能

  • 服务限流降级:默认支持 WebServlet、WebFlux, OpenFeign、RestTemplate、Spring Cloud Gateway, Zuul, Dubbo 和 RocketMQ 限流降级功能的接入,可以在运行时通过控制台实时修改限流降级规则,还支持查看限流降级 Metrics 监控。

  • 服务注册与发现:适配 Spring Cloud 服务注册与发现标准,默认集成了 Ribbon 的支持。
  • 分布式配置管理:支持分布式系统中的外部化配置,配置更改时自动刷新。
  • 消息驱动能力:基于 Spring Cloud Stream 为微服务应用构建消息驱动能力。
  • 分布式事务:使用 @GlobalTransactional 注解, 高效并且对业务零侵入地解决分布式事务问题。
  • 阿里云对象存储:阿里云提供的海量、安全、低成本、高可靠的云存储服务。支持在任何应用、任何时间、任何地点存储和访问任意类型的数据。
  • 分布式任务调度:提供秒级、精准、高可靠、高可用的定时(基于 Cron 表达式)任务调度服务。同时提供分布式的任务执行模型,如网格任务。网格任务支持海量子任务均匀分配到所有 Worker(schedulerx-client)上执行。
  • 阿里云短信服务:覆盖全球的短信服务,友好、高效、智能的互联化通讯能力,帮助企业迅速搭建客户触达通道。

    3. 相关组件

  • 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:一款在分布式架构环境中对应用配置进行集中管理和推送的应用配置中心产品。

    4. 基础使用

    4.1. 引入依赖

    如果需要使用已发布的版本,在 dependencyManagement 中添加如下配置。 ``` com.alibaba.cloud spring-cloud-alibaba-dependencies 2.2.7.RELEASE pom import
  1. 然后在 `dependencies` 中添加所需使用的依赖即可使用。
  2. ### 4.2. 官方演示 Demo
  3. 为了演示如何使用,Spring Cloud Alibaba 项目包含了一个子模块`spring-cloud-alibaba-examples`。此模块中提供了演示用的 example ,可以阅读对应的 example 工程下的 readme 文档,根据里面的步骤来体验。<br />Example 列表:
  4. - [Sentinel Example](682d2bed6296d256581ec150b2e5df36)
  5. - [Nacos Config Example](f0405a4da154614ddc8aece6c50167b8)
  6. - [Nacos Discovery Example](08ebb6c842ef438a65aae5599e59216a)
  7. - [RocketMQ Example](a7e0ee9ca9816f00f44df8e4256b9721)
  8. - [Seata Example](40567c1fe680fc7ef58e38a80aaf7d80)
  9. - [Alibaba Cloud OSS Example](https://github.com/alibaba/aliyun-spring-boot/tree/master/aliyun-spring-boot-samples/aliyun-oss-spring-boot-sample)
  10. - [Alibaba Cloud SMS Example](https://github.com/alibaba/aliyun-spring-boot/tree/master/aliyun-spring-boot-samples/aliyun-sms-spring-boot-sample)
  11. - [Alibaba Cloud SchedulerX Example](https://github.com/alibaba/aliyun-spring-boot)
  12. ## 5. 版本说明(2021.12.22日更新)
  13. ### 5.1. 组件版本关系
  14. 每个 Spring Cloud Alibaba 版本及其自身所适配的各组件对应版本(经过验证,自行搭配各组件版本不保证可用)如下表所示:
  15. |
  16. Spring Cloud Alibaba Version
  17. | Sentinel Version
  18. | Nacos Version
  19. | RocketMQ Version
  20. | Dubbo Version
  21. | Seata Version
  22. |
  23. | --- | --- | --- | --- | --- | --- |
  24. |
  25. 2.2.7.RELEASE(最新版本)
  26. | 1.8.1
  27. | 2.0.3
  28. | 4.6.1
  29. | 2.7.13
  30. | 1.3.0
  31. |
  32. |
  33. 2.2.6.RELEASE
  34. | 1.8.1
  35. | 1.4.2
  36. | 4.4.0
  37. | 2.7.8
  38. | 1.3.0
  39. |
  40. |
  41. 2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE
  42. | 1.8.0
  43. | 1.4.1
  44. | 4.4.0
  45. | 2.7.8
  46. | 1.3.0
  47. |
  48. |
  49. 2.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE
  50. | 1.8.0
  51. | 1.3.3
  52. | 4.4.0
  53. | 2.7.8
  54. | 1.3.0
  55. |
  56. |
  57. 2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE
  58. | 1.7.1
  59. | 1.2.1
  60. | 4.4.0
  61. | 2.7.6
  62. | 1.2.0
  63. |
  64. |
  65. 2.2.0.RELEASE
  66. | 1.7.1
  67. | 1.1.4
  68. | 4.4.0
  69. | 2.7.4.1
  70. | 1.0.0
  71. |
  72. |
  73. 2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE
  74. | 1.7.0
  75. | 1.1.4
  76. | 4.4.0
  77. | 2.7.3
  78. | 0.9.0
  79. |
  80. |
  81. 2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE
  82. | 1.6.3
  83. | 1.1.1
  84. | 4.4.0
  85. | 2.7.3
  86. | 0.7.1
  87. |
  88. ### 5.2. 毕业版本依赖关系(推荐使用)
  89. 下表为按时间顺序发布的 Spring Cloud Alibaba 以及对应的适配 Spring Cloud 和 Spring Boot 版本关系(由于 Spring Cloud 版本命名有调整,所以对应的 Spring Cloud Alibaba 版本号也做了对应变化)
  90. |
  91. Spring Cloud Alibaba Version
  92. | Spring Cloud Version
  93. | Spring Boot Version
  94. |
  95. | --- | --- | --- |
  96. |
  97. 2.2.7.RELEASE
  98. | Spring Cloud Hoxton.SR12
  99. | 2.3.12.RELEASE
  100. |
  101. |
  102. 2021.1
  103. | Spring Cloud 2020.0.1
  104. | 2.4.2
  105. |
  106. |
  107. 2.2.6.RELEASE
  108. | Spring Cloud Hoxton.SR9
  109. | 2.3.2.RELEASE
  110. |
  111. |
  112. 2.1.4.RELEASE
  113. | Spring Cloud Greenwich.SR6
  114. | 2.1.13.RELEASE
  115. |
  116. |
  117. 2.2.1.RELEASE
  118. | Spring Cloud Hoxton.SR3
  119. | 2.2.5.RELEASE
  120. |
  121. |
  122. 2.2.0.RELEASE
  123. | Spring Cloud Hoxton.RELEASE
  124. | 2.2.X.RELEASE
  125. |
  126. |
  127. 2.1.2.RELEASE
  128. | Spring Cloud Greenwich
  129. | 2.1.X.RELEASE
  130. |
  131. |
  132. 2.0.4.RELEASE(停止维护,建议升级)
  133. | Spring Cloud Finchley
  134. | 2.0.X.RELEASE
  135. |
  136. |
  137. 1.5.1.RELEASE(停止维护,建议升级)
  138. | Spring Cloud Edgware
  139. | 1.5.X.RELEASE
  140. |
  141. ### 5.3. 依赖管理
  142. Spring Cloud Alibaba BOM 包含了它所使用的所有依赖的版本(RELEASE 版本)。
  143. #### 5.3.1. Spring Cloud 2020
  144. 如果需要使用 Spring Cloud 2020 版本,请在 `dependencyManagement` 中添加如下内容
com.alibaba.cloud spring-cloud-alibaba-dependencies 2021.1 pom import
  1. #### 5.3.2. Spring Cloud Hoxton
  2. 如果需要使用 Spring Cloud Hoxton 版本,请在 `dependencyManagement` 中添加如下内容
com.alibaba.cloud spring-cloud-alibaba-dependencies 2.2.7.RELEASE pom import
  1. #### 5.3.3. Spring Cloud Greenwich
  2. 如果需要使用 Spring Cloud Greenwich 版本,请在 `dependencyManagement` 中添加如下内容
com.alibaba.cloud spring-cloud-alibaba-dependencies 2.1.4.RELEASE pom import
  1. #### 5.3.4. Spring Cloud Finchley
  2. 如果需要使用 Spring Cloud Finchley 版本,请在 `dependencyManagement` 中添加如下内容
com.alibaba.cloud spring-cloud-alibaba-dependencies 2.0.4.RELEASE pom import
  1. #### 5.3.5. Spring Cloud Edgware
  2. 如果需要使用 Spring Cloud Edgware 版本,请在 `dependencyManagement` 中添加如下内容
com.alibaba.cloud spring-cloud-alibaba-dependencies 1.5.1.RELEASE pom import
  1. ## 6. Spring Cloud Alibaba 示例微服务环境搭建准备
  2. 此学习 Spring Cloud Alibaba 的示例项目,主要包含商品、订单、用户等模块
  3. > - 示例项目地址:[https://github.com/MooNkirA/spring-cloud-note/spring-cloud-alibaba-2.1.x-sample](https://github.com/MooNkirA/spring-cloud-note/spring-cloud-alibaba-2.1.x-sample)
  4. > - 分支:feature-alibaba-2.1.x
  5. ### 6.1. 示例项目说明
  6. #### 6.1.1. 后端技术栈
  7. |
  8. 技术
  9. | 版本
  10. | 说明
  11. |
  12. | --- | --- | --- |
  13. |
  14. Sping Data Jpa
  15. |
  16. | 数据持久层框架
  17. |
  18. |
  19. Spring Boot
  20. | 2.1.13.RELEASE
  21. |
  22. |
  23. |
  24. Spring Cloud
  25. | Greenwich.RELEASE
  26. | 微服务框架
  27. |
  28. |
  29. Spring Cloud Alibaba
  30. | 2.1.2.RELEASE
  31. | 微服务框架
  32. |
  33. |
  34. mysql-connector-java
  35. | 5.1.48
  36. |
  37. |
  38. |
  39. fastjson
  40. | 1.2.76
  41. |
  42. |
  43. #### 6.1.2. 项目开发环境
  44. |
  45. 工具
  46. | 版本
  47. | 说明
  48. |
  49. | --- | --- | --- |
  50. |
  51. JDK
  52. | 1.8
  53. |
  54. |
  55. |
  56. Mysql
  57. | 5.7
  58. | 数据库
  59. |
  60. |
  61. maven
  62. | 3.8.4
  63. | 项目构建工具
  64. |
  65. |
  66. nacos
  67. | 2.0.3
  68. | 服务注册中心
  69. |
  70. #### 6.1.3. 涉及模块设计
  71. |
  72. 服务(项目)名
  73. | 部署端口
  74. | 说明
  75. |
  76. | --- | --- | --- |
  77. |
  78. spring-cloud-alibaba-2.1.x-sample
  79. |
  80. | 父项目
  81. |
  82. |
  83. modules-common
  84. |
  85. | 公共模块:实体类、工具类
  86. |
  87. |
  88. service-user
  89. | 807x
  90. | 用户微服务
  91. |
  92. |
  93. service-product
  94. | 808x
  95. | 商品微服务
  96. |
  97. |
  98. service-order
  99. | 809x
  100. | 订单微服务
  101. |
  102. |
  103. api-gateway
  104. | 7000
  105. | 网关服务
  106. |
  107. |
  108. 第三方应用服务
  109. | 部署端口
  110. | 说明
  111. |
  112. | --- | --- | --- |
  113. |
  114. Nacos server
  115. | 8848
  116. | nacos 服务注册中心
  117. |
  118. |
  119. Sentinel server
  120. | 8080
  121. | Sentinel 微服务容错
  122. |
  123. ### 6.2. 示例项目初始化
  124. #### 6.2.1. 创建聚合项目
  125. 创建 pom 类型工程 spring-cloud-alibaba-2.1.x-sample,在pom.xml文件中添加相关依赖
org.springframework.boot spring-boot-starter-parent 2.1.13.RELEASE 1.8 UTF-8 UTF-8 Greenwich.SR6 2.1.4.RELEASE 1.2.76 5.1.48 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. #### 6.2.2. 创建公共模块
  2. - 创建jar类型工程 modules-common,添加相关依赖
org.springframework.boot spring-boot-starter-data-jpa org.projectlombok lombok com.alibaba fastjson ${fastjson.version} mysql mysql-connector-java ${mysql-connector.version}
  1. - 创建相应数据库表的实体类
  2. #### 6.2.3. 创建三个业务模块的微服务
  3. 分别创建用户、商品、订单三个微服务工程。三个工程的步骤一样,下面以创建用户`service-user`工程为示例:
  4. - 创建jar类型工程,依赖相关依赖
org.springframework.boot spring-boot-starter-web com.moon modules-common 1.0-SNAPSHOT
  1. - 创建应用启动类

@SpringBootApplication public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); } }

  1. - 创建项目的配置文件 application.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

  1. # 指定数据库方言
  2. dialect: org.hibernate.dialect.MySQL5InnoDBDialect
  1. > 用户微服务端口:8071、商品微服务端口:8081、订单微服务端口:8091
  2. - 创建必要的接口和实现类(controller service dao)
  3. > 注:示例项目创建公共模块时,实体类的包路径不包含在各个微服务的启动类所扫描的路径下,如果需要使用`@EntityScan("com.moon.domain")`注解,指定扫描JPA实体类的包路径,否则会报找不到 `JpaRepository` 接口泛型指定的实体类的错误。
  4. # Spring Cloud Alibaba Nacos Discovery
  5. ## 1. Nacos
  6. ### 1.1. Nacos 简介
  7. Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。<br />Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。
  8. > 官网:[https://nacos.io/zh-cn/index.html](c8a7d43617ad45a4b68ef9c3163b1ccc)
  9. ### 1.2. Nacos 的关键特性
  10. Nacos 支持几乎所有主流类型的“服务”的发现、配置和管理:
  11. - Kubernetes Service
  12. - gRPC & Dubbo RPC Service
  13. - Spring Cloud RESTful Service
  14. Nacos 的关键特性包括:
  15. - **服务发现和服务健康监测**
  16. - Nacos 支持基于 DNS 和基于 RPC 的服务发现。Nacos 提供对服务的实时的健康检查,阻止向不健康的主机或服务实例发送请求。Nacos 支持传输层 (PING 或 TCP)和应用层 (如 HTTP、MySQL、用户自定义)的健康检查。 对于复杂的云环境和网络拓扑环境中(如 VPC、边缘网络等)服务的健康检查,Nacos 提供了 agent 上报模式和服务端主动检测2种健康检查模式。Nacos 还提供了统一的健康检查仪表盘,帮助您根据健康状态管理服务的可用性及流量。
  17. - **动态配置服务**
  18. - 动态配置服务可以让您以中心化、外部化和动态化的方式管理所有环境的应用配置和服务配置。
  19. - 动态配置消除了配置变更时重新部署应用和服务的需要,让配置管理变得更加高效和敏捷。
  20. - 配置中心化管理让实现无状态服务变得更简单,让服务按需弹性扩展变得更容易。
  21. - Nacos 提供了一个简洁易用的UI (控制台) 帮助您管理所有的服务和应用的配置。Nacos 还提供包括配置版本跟踪、金丝雀发布、一键回滚配置以及客户端配置更新状态跟踪在内的一系列开箱即用的配置管理特性,帮助您更安全地在生产环境中管理配置变更和降低配置变更带来的风险。
  22. - **动态 DNS 服务**
  23. - 动态 DNS 服务支持权重路由,让您更容易地实现中间层负载均衡、更灵活的路由策略、流量控制以及数据中心内网的简单DNS解析服务。动态DNS服务还能让您更容易地实现以 DNS 协议为基础的服务发现,以帮助您消除耦合到厂商私有服务发现 API 上的风险。
  24. - **服务及其元数据管理**
  25. - Nacos 能让您从微服务平台建设的视角管理数据中心的所有服务及元数据,包括管理服务的描述、生命周期、服务的静态依赖分析、服务的健康状态、服务的流量管理、路由及安全策略、服务的 SLA 以及最首要的 metrics 统计数据。
  26. ### 1.3. Nacos 相关资源
  27. - [NACOS架构与原理](https://www.yuque.com/nacos/ebook)
  28. ## 2. Nacos 快速开始
  29. ### 2.1. Nacos 服务端搭建
  30. 在Nacos的[release notes](https://github.com/alibaba/nacos/releases)及[博客](4acb1748be1cc43d5e7b5107763f9eff)中找到每个版本支持的功能的介绍,当前推荐的稳定版本为2.0.3。<br />Nacos 服务端搭建只需要下载官方安装包,
  31. > Nacos 服务端安装包官方下载地址:[https://github.com/alibaba/nacos/releases](https://github.com/alibaba/nacos/releases)
  32. #### 2.1.1. 预备环境准备
  33. Nacos 依赖 Java 环境来运行。如果是从代码开始构建并运行 Nacos,还需要为此配置 Maven 环境,请确保是在以下版本环境中安装使用:
  34. 1. 64 bit OS,支持 Linux/Unix/Mac/Windows,推荐选用 Linux/Unix/Mac。
  35. 1. 64 bit JDK 1.8+
  36. 1. Maven 3.2.x+
  37. #### 2.1.2. 下载源码或者安装包
  38. 可以通过源码和发行包两种方式来获取 Nacos。<br />**从 Github 上下载源码方式**

git clone https://github.com/alibaba/nacos.git cd nacos/ mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U ls -al distribution/target/

change the $version to your actual path

cd distribution/target/nacos-server-$version/nacos/bin

  1. **下载编译后压缩包方式**<br />可以直接从官方仓库(github)下载 `nacos-server-x.x.x.zip` 包(_笔记编写时最新版本是2.0.3_)。下载后压缩,进行bin目录运行即可

unzip nacos-server-$version.zip 或者 tar -xvf nacos-server-$version.tar.gz cd nacos/bin

  1. > 注:命令中的`$version`为nacos的版本号
  2. #### 2.1.3. 启动服务器
  3. **Windows 环境下的启动命令:**

startup.cmd -m standalone

  1. > 注:命令参数 `standalone` 代表着单机模式运行,非集群模式。_在2.0.3版本,如果直接点击startup.cmd脚本启动会报错,必须通过命令行方式指定参数运行_
  2. **Linux/Unix/Mac 环境下的启动命令:**

startup.sh -m standalone

  1. 如果使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:

startup.sh -m standalone

  1. #### 2.1.4. 关闭服务器
  2. **Windows 环境**

shutdown.cmd

  1. 或者双击shutdown.cmd运行文件。<br />**Linux/Unix/Mac 环境**

sh shutdown.sh

  1. ### 2.2. 访问 nacos 服务
  2. 打开浏览器输入 `http://127.0.0.1:8848/nacos`,即可访问服务, 默认账号和密码是nacos/nacos
  3. ### 2.3. 服务提供者注册到 Nacos(Spring Cloud 项目)
  4. > 前提条件:需要先下载 Nacos 并启动 Nacos server。
  5. 以下示例是在 Spring Cloud 项目中启动 Nacos 的服务发现功能。<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220101184857948_362.png)<br />修改商品模块的代码,将其注册到nacos服务上。
  6. - 修改 pom.xml 添加 nacos 注册中心客户端依赖
com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery
  1. > 注意:版本 2.1.x.RELEASE 对应的是 Spring Boot 2.1.x 版本。版本 2.0.x.RELEASE 对应的是 Spring Boot 2.0.x 版本,版本 1.5.x.RELEASE 对应的是 Spring Boot 1.5.x 版本。
  2. - 在项目的 application.yml/application.properties 配置文件中,配置 Nacos server 的地址。

server: port: 8081 spring: application: name: service-product cloud: nacos: discovery: server-addr: 127.0.0.1:8848 # 配置 Nacos server 的地址

  1. ```
  2. server.port=8070
  3. spring.application.name=service-provider
  4. spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
  • 在项目的启动类或者配置类中,通过 Spring Cloud 原生注解 @EnableDiscoveryClient 开启服务注册发现功能: ``` @SpringBootApplication // Spring Cloud 原生注解 @EnableDiscoveryClient 开启服务注册发现功能 @EnableDiscoveryClient public class ProductApplication { public static void main(String[] args) {
    1. SpringApplication.run(ProductApplication.class, args);
    } }
  1. 通过以上步骤后,即配置了服务提供者,从而服务提供者可以通过 Nacos 的服务注册发现功能将其服务注册到 Nacos server 上。服务注册成功后,启动服务能在 nacos 服务端的控制面板中看到相应注册的微服务。<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220101190918925_30556.png)
  2. ### 2.4. 服务消费者注册到 Nacos(Spring Cloud 项目)
  3. > 服务消费者注册到 Nacos 服务端的步骤与服务提供者一样。
  4. Spring Cloud 的 `DiscoveryClient` 接口是专门负责服务注册和发现的,可以通过它获取到注册到注册中心的所有服务实例。<br />修改服务消费者(订单服务),通过nacos注册中心获取到服务提供者(商品服务)的地址与端口,再通过 `RestTemplate` 进行服务调用。

@Autowired private DiscoveryClient discoveryClient;

@GetMapping(“/add/serviceInstance/{pid}”) public Order serviceInstancedAdd(@PathVariable(“pid”) Long pid) { log.info(“接收到ID为{}的商品下单请求”, pid);

  1. /*
  2. * 通过 DiscoveryClient 的 getInstances 方法可以获取到指定的服务实例对象(返回值是集合,因为服务会有集群的情况)。
  3. * 通过服务实例对象,可获取服务的地址和端口
  4. */
  5. List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
  6. // 此处测试只有一个商品微服务,所以直接获取服务列表第一个是没有问题,但如果是集群服务的话,这种调用方式显然有问题
  7. ServiceInstance serviceInstance = instances.get(0);
  8. String host = serviceInstance.getHost(); // 地址
  9. int port = serviceInstance.getPort(); // 端口
  10. Product product = restTemplate
  11. .getForObject(String.format("http://%s:%d/product/%s", host, port, pid), Product.class);
  12. Order order = new Order();
  13. if (product != null) {
  14. order.setProductId(pid);
  15. order.setNumber(1);
  16. order.setPrice(product.getPrice());
  17. order.setAmount(product.getPrice());
  18. order.setProductName(product.getProductName());
  19. // 暂写死用户
  20. order.setUserId(Long.parseLong("0"));
  21. order.setUsername("测试用户");
  22. // 创建订单
  23. orderService.createOrder(order);
  24. }
  25. return order;

}

  1. 启动服务提供者(商品服务)和服务消费者(订单服务),观察nacos的控制面板中是否已注册相应的服务,然后通过访问消费者服务验证调用是否成功
  2. ## 3. 基于 Ribbon 实现负载均衡
  3. > Ribbon 是 Spring Cloud 的一个组件,用于实现服务调用的负载均衡
  4. Nacos Discovery Starter 默认集成了 Ribbon ,所以对于使用了 Ribbon 做负载均衡的组件,可以直接使用 Nacos 的服务发现。具体实现步骤如下:
  5. - 在配置类中生成 `RestTemplate` 实例方法上添加 `@LoadBalanced` 注解,使得 `RestTemplate` 接入 Ribbon

@Bean @LoadBalanced // @LoadBalanced 注解让 RestTemplate 接入 Ribbon 实现负载均衡 public RestTemplate restTemplate() { return new RestTemplate(); }

  1. - 修改服务调用方法,直接通过在nacos上注册的服务名称进行调用

/ RestTemplate 接入 Ribbon 后,将原来的url+端口替换成注册中心上的服务名称 / Product product = restTemplate .getForObject(String.format(“http://service-product/product/%s“, pid), Product.class);

  1. - 通过修改 application.yml 配置项`被调用的服务名称.ribbon.NFLoadBalancerRuleClassName`,可以调整 Ribbon 的负载均衡策略。_如果不配置,则默认的负载均衡策略是轮询_

配置 Ribbon 的负载均衡策略

service-product: # 调用的提供者的名称 ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule # 随机选择一个server

  1. Ribbon 内置了多种负载均衡策略,内部负载均衡的顶级接口为 `com.netflix.loadbalancer.IRule`,具体的负载策略如下所示:<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220101224145518_11177.png)
  2. ## 4. 基于 Feign 实现服务调用
  3. ### 4.1. Feign 概述
  4. Feign 是 Spring Cloud 提供的一个声明式、模板化的伪 Http 客户端,它使得调用远程服务就像调用本地服务一样简单,只需要创建一个接口并添加一个注解即可。
  5. > 注:Feign 的详细使用文档,详见[《04-Spring-Cloud-OpenFeign.md》](/07-%E5%88%86%E5%B8%83%E5%BC%8F%E6%9E%B6%E6%9E%84&%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84/02-SpringCloud/04-Spring-Cloud-OpenFeign)
  6. Nacos 很好的兼容了 Feign,Feign 默认集成了 Ribbon,所以在 Nacos 下使用 Fegin 默认就实现了负载均衡的效果。
  7. ### 4.2. Feign 与 Nacos 整合实现服务调用
  8. - 修改服务消费者(订单模块),添加 Fegin 的依赖
org.springframework.cloud spring-cloud-starter-openfeign
  1. - 在项目启动类或者配置类上,添加 `@EnableFeignClients` 注解,开启 Feign 功能

@SpringBootApplication // Spring Cloud 原生注解 @EnableDiscoveryClient 开启服务注册发现功能 @EnableDiscoveryClient // 开启fegin的客户端 @EnableFeignClients public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } }

  1. - 创建一个 Feign 的服务接口,使用 `@FeignClient` 注解的接口,Feign 会生成此接口的代理对象。

/*

  • @FeignClient 注解,用于标识当前接口为Feign调用微服务的核心接口
  • value/name属性:指定需要调用的服务提供者注册的名称 / @FeignClient(“service-product”) // 或者:@FeignClient(name = “待调用的服务注册名称”) public interface ProductFeignClient { /
    • 创建需要调用的微服务接口方法,SpringCloud 对 Feign 进行了增强兼容了 SpringMVC 的注解
    • @FeignClient 的 value值 + @RequestMapping 的 value 值,相当于服务的请求地址:”http://service-product/product/“ + pid
    • 在使用的两个注意点:
      1. FeignClient 接口有参数时,必须在参数加@PathVariable(“XXX”)和@RequestParam(“XXX”)注解,并且必须要指定对应的参数值(原来SpringMVC是可以省略)
      1. feignClient 返回值为复杂对象时,其对象类型必须有无参构造函数
      1. 方法的名称不需要与被调用的服务接口名称一致 */ @GetMapping(“/product/{pid}”) Product findById(@PathVariable(“pid”) Long id); }
  1. > 注:Feign 定义调用的服务接口的方法是根据,`@FeignClient` 的 `value` 值 + `@RequestMapping` 的 `value` 值,相当于服务的请求地址。
  2. - 修改服务消费者,通过 Feign 的接口调用远程服务。

@RestController @RequestMapping(“order”) @Slf4j public class OrderFeignController { @Autowired private ProductFeignClient productFeignClient;

  1. @Autowired
  2. private OrderService orderService;
  3. /**
  4. * 下单请求方法,通过 Feign 实现负载均衡的服务调用
  5. *
  6. * @param pid
  7. * @return
  8. */
  9. @GetMapping("/feign/{pid}")
  10. public Order feignLoadBalanced(@PathVariable("pid") Long pid) {
  11. log.info("接收到ID为{}的商品下单请求", pid);
  12. /* 通过 FeignClient 接口调用本地方法的方式,实现服务的调用。 */
  13. Product product = productFeignClient.findById(pid);
  14. Order order = new Order();
  15. if (product != null) {
  16. order.setProductId(pid);
  17. order.setNumber(1);
  18. order.setPrice(product.getPrice());
  19. order.setAmount(product.getPrice());
  20. order.setProductName(product.getProductName());
  21. // 暂写死用户
  22. order.setUserId(Long.parseLong("0"));
  23. order.setUsername("测试用户");
  24. // 创建订单
  25. orderService.createOrder(order);
  26. }
  27. return order;
  28. }

}

  1. ## 5. Nacos Discovery 相关配置项
  2. |
  3. 配置项
  4. | key
  5. | 默认值
  6. | 说明
  7. |
  8. | --- | --- | --- | --- |
  9. |
  10. 服务端地址
  11. | `spring.cloud.nacos.discovery.server-addr`
  12. |
  13. |
  14. |
  15. |
  16. 服务名
  17. | `spring.cloud.nacos.discovery.service`
  18. | `spring.application.name`
  19. |
  20. |
  21. |
  22. 权重
  23. | `spring.cloud.nacos.discovery.weight`
  24. | 1
  25. | 取值范围 1 到 100,数值越大,权重越大
  26. |
  27. |
  28. 网卡名
  29. | `spring.cloud.nacos.discovery.network-interface`
  30. |
  31. | 当IP未配置时,注册的IP为此网卡所对应的IP地址,如果此项也未配置,则默认取第一块网卡的地址
  32. |
  33. |
  34. 注册的IP地址
  35. | `spring.cloud.nacos.discovery.ip`
  36. |
  37. | 优先级最高
  38. |
  39. |
  40. 注册的端口
  41. | `spring.cloud.nacos.discovery.port`
  42. | -1
  43. | 默认情况下不用配置,会自动探测
  44. |
  45. |
  46. 命名空间
  47. | `spring.cloud.nacos.discovery.namespace`
  48. |
  49. | 常用场景之一是不同环境的注册的区分隔离,例如开发测试环境和生产环境的资源(如配置、服务)隔离等。
  50. |
  51. |
  52. AccessKey
  53. | `spring.cloud.nacos.discovery.access-key`
  54. |
  55. |
  56. |
  57. |
  58. SecretKey
  59. | `spring.cloud.nacos.discovery.secret-key`
  60. |
  61. |
  62. |
  63. |
  64. Metadata
  65. | `spring.cloud.nacos.discovery.metadata`
  66. |
  67. | 使用Map格式配置
  68. |
  69. |
  70. 日志文件名
  71. | `spring.cloud.nacos.discovery.log-name`
  72. |
  73. |
  74. |
  75. |
  76. 接入点
  77. | `spring.cloud.nacos.discovery.endpoint`
  78. | UTF-8
  79. | 地域的某个服务的入口域名,通过此域名可以动态地拿到服务端地址
  80. |
  81. |
  82. 是否集成Ribbon
  83. | `ribbon.nacos.enabled`
  84. | true
  85. |
  86. |
  87. # Spring Cloud Alibaba Nacos Config
  88. ## 1. Nacos Config 概述
  89. ### 1.1. 简介
  90. Nacos 提供用于存储配置和其他元数据的 key/value 存储,为分布式系统中的外部化配置提供服务器端和客户端支持。使用 Spring Cloud Alibaba Nacos Config 可以在 Nacos Server 集中管理 Spring Cloud 应用的外部属性配置。<br />Spring Cloud Alibaba Nacos Config 是 Config Server 和 Client 的替代方案,客户端和服务器上的概念与 Spring Environment 和 PropertySource 有着一致的抽象,在特殊的 bootstrap 阶段,配置被加载到 Spring 环境中。当应用程序通过部署管道从开发到测试再到生产时,可以管理这些环境之间的配置,并确保应用程序具有迁移时需要运行的所有内容。
  91. ### 1.2. nacos config 相关概念
  92. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220108214758067_5476.png)
  93. - **命名空间(Namespace)**:命名空间可用于进行不同环境的配置隔离。一般一个环境划分到一个命名空间
  94. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220108175545845_6946.png)
  95. - **配置分组(Group)**:配置分组用于将不同的服务可以归类到同一分组。一般将一个项目的配置分到一组
  96. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220108175635513_13524.png)
  97. - **配置集(Data ID)**:在系统中,一个配置文件通常就是一个配置集。一般微服务的配置就是一个配置集
  98. ## 2. Nacos Config 快速开始
  99. 使用 nacos 作为配置中心,其实就是将 nacos 当做一个服务端,将各个微服务看成是客户端,将各个微服务的配置文件统一存放在 nacos 服务器上,然后各个微服务从 nacos 上拉取配置即可。_下面以商品服务做为示例_
  100. ### 2.1. Nacos 服务端搭建
  101. > 此部分详见 Nacos Discovery 章节
  102. ### 2.2. 修改微服务工程
  103. - 修改项目 pom.xml 文件,引入 Nacos Config Starter 依赖
com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config
  1. - 在应用的 `/src/main/resources/` 目录下,创建 bootstrap.properties/bootstrap.yml 配置文件,并配置 Nacos Config 元数据

配置一些必需项,其他的从注册中心获取

spring: application: name: service-product cloud: nacos: config: server-addr: 127.0.0.1:8848 # nacos 的服务端地址 file-extension: yaml # dataID后缀及内容文件格式,默认值是 properties profiles: active: dev # 环境标识

  1. > 更多配置项详见《Nacos Config 相关配置项》章节或者项目示例代码
  2. > 注意事项:
  3. > - **注意:不能使用原来的 application.yml 作为配置文件,必须新建一个 bootstrap.properties/bootstrap.yml 作为配置文件**
  4. > - **配置文件优先级(由高到低)**:bootstrap.properties -> bootstrap.yml -> application.properties -> application.yml<br />
  5. 注意当使用域名的方式来访问 Nacos 时,`spring.cloud.nacos.config.server-addr` 配置的方式为 `域名:port`。 例如 Nacos 的域名为`abc.com.nacos`,监听的端口为 `80`,则 `spring.cloud.nacos.config.server-addr=abc.com.nacos:80`。 注意 `80` 端口不能省略。
  6. - 完成上述两步后,应用会从 Nacos Config 中获取相应的配置,并添加在 Spring Environment 的 `PropertySources` 中。现在可以使用 `@Value` 注解来将对应的配置注入到 Spring 容器的相关实例中。示例创建 `NacosConfigSampleController` 类,定义 userName 和 age 字段用于读取配置中心设置的值,并添加 `@RefreshScope` 注解打开动态刷新功能。

@RestController @RequestMapping(“nacos-config”) @RefreshScope // Spring Cloud 原生注解,实现配置文件的动态加载。 public class NacosConfigSampleController implements EnvironmentAware {

  1. @Value("${user.name}") // 动态注入配置中心相应的配置项
  2. private String userName;
  3. @Value("${user.age}")
  4. private int age;
  5. private Environment environment;
  6. @GetMapping("value-annotation")
  7. public String getByValueAnnotation() {
  8. return String.format("通过@Value注解获取配置值,name: %s, age: %d", userName, age);
  9. }
  10. @GetMapping("environment")
  11. public String getByEnvironment() {
  12. String userName = environment.getProperty("name");
  13. String age = environment.getProperty("age");
  14. return String.format("通过 Environment 对象获取配置值,name: %s, age: %s", userName, age);
  15. }
  16. /**
  17. * Set the {@code Environment} that this component runs in.
  18. *
  19. * @param environment
  20. */
  21. @Override
  22. public void setEnvironment(Environment environment) {
  23. this.environment = environment;
  24. }

}

  1. ### 2.3. 在 nacos 控制台配置并测试
  2. - 点击配置列表,点击右边 + 号,新建配置。
  3. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220108171021072_10404.png)
  4. - 配置内容注意下面的细节
  5. - 注意 DataID 与 bootstrap 配置文件相应关系
  6. - 配置文件格式要跟配置文件的格式对应,且目前仅仅支持YAML和Properties
  7. - 配置内容按照上面选定的格式书写
  8. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220108173812062_26625.png)<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220108171931119_31604.png)
  9. - 删除本地项目的 application.yml 配置,启动程序进行测试。项目能成功启动,说明 nacos 的配置中心功能已经实现。
  10. ## 3. Nacos Config 高级使用
  11. ### 3.1. 配置动态刷新
  12. 通过项目的配置项 `spring.cloud.nacos.config.refresh-enabled` 来控制是否开启监听和自动刷新,默认是true(开启)。程序中动态获取最新配置的方法如下:
  13. #### 3.1.1. 方式一:硬编码方式
  14. 通过 `org.springframework.context.ConfigurableApplicationContext` 对象获取 `Environment` 实例,可以根据key值获取到相应的配置值

@Autowired private ConfigurableApplicationContext applicationContext;

@GetMapping(“configurableApplicationContext”) public String getByConfigurableApplicationContext() { String userName = applicationContext.getEnvironment().getProperty(“user.name”); String age = applicationContext.getEnvironment().getProperty(“user.age”); return String.format(“通过 ConfigurableApplicationContext.getEnvironment() 获取 Environment对象,获取配置值,name: %s, age: %s”, userName, age); }

  1. > 注:此方式与快速开始示例中的实现 `EnvironmentAware` 接口获取 `Environment` 实例的效果一样。
  2. #### 3.1.2. 方式二:注解方式(推荐)

@RestController @RequestMapping(“nacos-config”) @RefreshScope // Spring Cloud 原生注解,实现配置文件的动态加载。。只需要在需要动态读取配置的类上添加此注解即可。 public class NacosConfigSampleController implements EnvironmentAware { @Value(“${user.name}”) // 动态注入配置中心相应的配置项 private String userName;

  1. @Value("${user.age}")
  2. private int age;
  3. @GetMapping("value-annotation")
  4. public String getByValueAnnotation() {
  5. return String.format("通过@Value注解获取配置值,name: %s, age: %d", userName, age);
  6. }

}

  1. ### 3.2. 配置共享
  2. 配置共享,即将公共配置的部分抽取出来,在其他配置文件中引入。
  3. #### 3.2.1. 同一个微服务的不同环境配置文件之间共享配置
  4. 在同一个微服务的不同环境之间实现配置共享,只需要提取一个以 `spring.application.name` 命名的配置文件,然后将其所有环境的公共配置放在里面即可。_以商品微服务为例_
  5. - 新建一个名为 `service-product.yaml` 配置商品微服务的公共配置项
  6. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220108182242453_13869.png)
  7. - 新建一个名为 `service-product-test.yaml` 配置测试环境的配置项
  8. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220108182708074_20425.png)
  9. - 删除 `service-product-dev.yaml` 原来公共部分的配置
  10. - 修改本地的 `spring.profiles.active` 配置项的,切换不同的环境的配置文件进行测试。
  11. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220108183138237_20271.png)
  12. #### 3.2.2. 不同微服务之间共享配置
  13. 不同为服务之间实现配置共享的原理类似于文件引入,就是定义一个公共配置,然后在不同的服务的配置中引入。_下面以商品微服务为例_
  14. - 在 nacos 配置中心中定义一个 DataID 为 all-service.yaml 的配置,用于所有微服务共享
  15. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220108183519723_15552.png)
  16. - 在配置中心中,将商品服务的公共配置项删除
  17. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220108183653748_32626.png)
  18. - 修改商品服务本地的 bootstrap.yml/bootstrap.properties 配置文件,配置引入公共配置

配置一些必需项,其他的从注册中心获取

spring: application: name: service-product cloud: nacos: config: server-addr: 127.0.0.1:8848 # nacos 的服务端地址 file-extension: yaml # dataID后缀及内容文件格式,默认值是 properties

  1. # 引入公共配置,已经过时
  2. shared-dataids: all-service.yaml # 配置要引入的配置
  3. refreshable-dataids: all-service.yaml # 配置要实现动态配置刷新的配置

profiles: active: dev # 环境标识

  1. - 启动商品微服务进行测试
  2. ## 4. Nacos Config 相关配置项
  3. |
  4. 配置项
  5. | key
  6. | 默认值
  7. | 说明
  8. |
  9. | --- | --- | --- | --- |
  10. |
  11. 服务端地址
  12. | `spring.cloud.nacos.config.server-addr`
  13. |
  14. |
  15. |
  16. |
  17. DataId前缀
  18. | `spring.cloud.nacos.config.prefix`
  19. | `spring.application.name`
  20. |
  21. |
  22. |
  23. Group
  24. | `spring.cloud.nacos.config.group`
  25. | DEFAULT_GROUP
  26. |
  27. |
  28. |
  29. dataID后缀及内容文件格式
  30. | `spring.cloud.nacos.config.file-extension`
  31. | properties
  32. | dataId 的后缀,同时也是配置内容的文件格式,目前只支持 properties
  33. |
  34. |
  35. 配置内容的编码方式
  36. | `spring.cloud.nacos.config.encode`
  37. | UTF-8
  38. | 配置的编码
  39. |
  40. |
  41. 获取配置的超时时间
  42. | `spring.cloud.nacos.config.timeout`
  43. | 3000
  44. | 单位为 ms
  45. |
  46. |
  47. 配置的命名空间
  48. | `spring.cloud.nacos.config.namespace`
  49. |
  50. | 常用场景之一是不同环境的配置的区分隔离,例如开发测试环境和生产环境的资源隔离等。
  51. |
  52. |
  53. AccessKey
  54. | `spring.cloud.nacos.config.access-key`
  55. |
  56. |
  57. |
  58. |
  59. SecretKey
  60. | `spring.cloud.nacos.config.secret-key`
  61. |
  62. |
  63. |
  64. |
  65. 相对路径
  66. | `spring.cloud.nacos.config.context-path`
  67. |
  68. | 服务端 API 的相对路径
  69. |
  70. |
  71. 接入点
  72. | `spring.cloud.nacos.config.endpoint`
  73. | UTF-8
  74. | 地域的某个服务的入口域名,通过此域名可以动态地拿到服务端地址
  75. |
  76. |
  77. 是否开启监听和自动刷新
  78. | `spring.cloud.nacos.config.refresh-enabled`
  79. | true
  80. |
  81. |
  82. # Spring Cloud Alibaba Sentinel
  83. ## 1. Sentinel 概述
  84. ### 1.1. 简介
  85. 随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel (分布式系统的流量防卫兵) 是阿里开源的一套用于服务容错的综合性解决方案。以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。<br />**Sentinel 的主要特性**:
  86. - **丰富的应用场景**:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
  87. - **完备的实时监控**:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
  88. - **广泛的开源生态**:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入Sentinel。
  89. - **完善的 SPI 扩展点**:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。
  90. **Sentinel 的构架图**:<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20201020160928442_20800.png)
  91. ### 1.2. Sentinel 与 Hystrix 的区别
  92. ![](https://gitee.com/moonzero/images/raw/master/code-note/20201020161451416_30983.png)
  93. ### 1.3. 迁移方案
  94. Sentinel 官方提供了由 Hystrix 迁移到 Sentinel 的详细方法。详情参考 [Guideline: 从 Hystrix 迁移到 Sentinel](https://github.com/alibaba/Sentinel/wiki/Guideline:-%E4%BB%8E-Hystrix-%E8%BF%81%E7%A7%BB%E5%88%B0-Sentinel)
  95. |
  96. Hystrix 功能
  97. | 迁移方案
  98. |
  99. | --- | --- |
  100. |
  101. 线程池隔离/信号量隔离
  102. | Sentinel 不支持线程池隔离;信号量隔离对应 Sentinel 中的线程数限流,详见[此处](https://github.com/alibaba/Sentinel/wiki/Guideline:-%E4%BB%8E-Hystrix-%E8%BF%81%E7%A7%BB%E5%88%B0-Sentinel#%E4%BF%A1%E5%8F%B7%E9%87%8F%E9%9A%94%E7%A6%BB)
  103. |
  104. |
  105. 熔断器
  106. | Sentinel 支持按平均响应时间、异常比率、异常数来进行熔断降级。从 Hystrix 的异常比率熔断迁移的步骤详见[此处](https://github.com/alibaba/Sentinel/wiki/Guideline:-%E4%BB%8E-Hystrix-%E8%BF%81%E7%A7%BB%E5%88%B0-Sentinel#%E7%86%94%E6%96%AD%E9%99%8D%E7%BA%A7)
  107. |
  108. |
  109. Command 创建
  110. | 直接使用 Sentinel `SphU` API 定义资源即可,资源定义与规则配置分离,详见[此处](https://github.com/alibaba/Sentinel/wiki/Guideline:-%E4%BB%8E-Hystrix-%E8%BF%81%E7%A7%BB%E5%88%B0-Sentinel#command-%E8%BF%81%E7%A7%BB)
  111. |
  112. |
  113. 规则配置
  114. | 在 Sentinel 中可通过 API 硬编码配置规则,也支持多种动态规则源
  115. |
  116. |
  117. 注解支持
  118. | Sentinel 也提供注解支持,可以很方便地迁移,详见[此处](https://github.com/alibaba/Sentinel/wiki/Guideline:-%E4%BB%8E-Hystrix-%E8%BF%81%E7%A7%BB%E5%88%B0-Sentinel#%E6%B3%A8%E8%A7%A3%E6%94%AF%E6%8C%81)
  119. |
  120. |
  121. 开源框架支持
  122. | Sentinel 提供 Servlet、Dubbo、Spring Cloud、gRPC 的适配模块,开箱即用;若之前使用 Spring Cloud Netflix,可迁移至 [Spring Cloud Alibaba](https://github.com/spring-cloud-incubator/spring-cloud-alibaba)
  123. |
  124. ### 1.4. Sentinel 组成部分
  125. Sentinel 的使用可以分为两个部分:
  126. - 核心库(Java 客户端):不依赖任何框架/库,能够运行于 Java 8 及以上的版本的运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持(见 主流框架适配)。
  127. - 控制台(Dashboard):Dashboard 主要负责管理推送规则、监控、管理机器信息等。
  128. > 核心库不依赖 Dashboard,但是结合 Dashboard 可以取得最好的效果。
  129. ## 2. Sentinel 的概念和功能
  130. ### 2.1. Sentinel 基本概念
  131. #### 2.1.1. 资源
  132. 资源是 Sentinel 的关键概念。它可以是 Java 应用程序中的任何内容,例如,由应用程序提供的服务,或由应用程序调用的其它应用提供的服务,甚至可以是一段代码。**资源就是 Sentinel 要保护的东西**
  133. > 在此文档中的示例中,每个请求的方法都可以认为是一个资源
  134. 只要通过 Sentinel API 定义的代码,就是资源,能够被 Sentinel 保护起来。大部分情况下,可以使用方法签名,URL,甚至服务名称作为资源名来标示资源。
  135. #### 2.1.2. 规则
  136. **规则就是用来定义如何进行保护资源的**。作用在资源之上,定义以什么样的方式保护资源。<br />围绕资源的实时状态设定的规则,可以包括流量控制规则、熔断降级规则以及系统保护规则。**所有规则可以动态实时调整**。
  137. ### 2.2. Sentinel 功能和设计理念
  138. Sentinel 总体功能概述图:<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102105526948_10032.jpg)
  139. #### 2.2.1. 流量控制
  140. 流量控制在网络传输中是一个常用的概念,它用于调整网络包的发送数据。然而,从系统稳定性角度考虑,在处理请求的速度上,也有非常多的讲究。任意时间到来的请求往往是随机不可控的,而系统的处理能力是有限的。需要根据系统的处理能力对流量进行控制。Sentinel 作为一个调配器,可以根据需要把随机的请求调整成合适的形状,如下图所示:<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102112948483_8168.png)<br />流量控制有以下几个角度:
  141. - 资源的调用关系,例如资源的调用链路,资源和资源之间的关系;
  142. - 运行指标,例如 QPS、线程池、系统负载等;
  143. - 控制的效果,例如直接限流、冷启动、排队等。
  144. Sentinel 的设计理念是让开发者自由选择控制的角度,并进行灵活组合,从而达到想要的效果。
  145. #### 2.2.2. 熔断降级
  146. 除了流量控制以外,降低调用链路中的不稳定资源也是 Sentinel 的使命之一。由于调用关系的复杂性,如果调用链路中的某个资源出现了不稳定,最终会导致请求发生堆积。<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102113225803_12823.png)<br />Sentinel 和 Hystrix 的原则是一致的:当调用链路中某个资源出现不稳定,例如,表现为请求时长超时(timeout),异常比例升高的时候,则对这个资源的调用进行限制,并让请求快速失败,避免影响到其它的资源,最终产生雪崩的效果。<br />Sentinel 针对熔断降级设计,采取了两种手段:
  147. - **通过并发线程数进行限制**
  148. 和资源池隔离的方法不同,Sentinel 通过限制资源并发线程的数量,来减少不稳定资源对其它资源的影响。这样不但没有线程切换的损耗,也不需要您预先分配线程池的大小。当某个资源出现不稳定的情况下,例如响应时间变长,对资源的直接影响就是会造成线程数的逐步堆积。当线程数在特定资源上堆积到一定的数量之后,对该资源的新请求就会被拒绝。堆积的线程完成任务后才开始继续接收请求。
  149. - **通过响应时间对资源进行降级**
  150. 除了对并发线程数进行控制以外,Sentinel 还可以通过响应时间来快速降级不稳定的资源。当依赖的资源出现响应时间过长后,所有对该资源的访问都会被直接拒绝,直到过了指定的时间窗口之后才重新恢复。
  151. > 在限制的手段上,Sentinel 和 Hystrix 采取了完全不一样的方法。
  152. > Hystrix 通过线程池的方式,来对依赖(在我们的概念中对应资源)进行了隔离。这样做的好处是资源和资源之间做到了最彻底的隔离。缺点是除了增加了线程切换的成本,还需要预先给各个资源做线程池大小的分配。
  153. #### 2.2.3. 系统负载保护
  154. Sentinel 同时提供系统维度的自适应保护能力。当系统负载较高的时候,如果还持续让请求进入,可能会导致系统崩溃,无法响应。在集群环境下,网络负载均衡会把本应这台机器承载的流量转发到其它的机器上去。如果这个时候其它的机器也处在一个边缘状态的时候,这个增加的流量就会导致这台机器也崩溃,最后导致整个集群不可用。<br />针对这个情况,Sentinel 提供了对应的保护机制,让系统的入口流量和系统的负载达到一个平衡,保证系统在能力范围之内处理最多的请求。
  155. ### 2.3. Sentinel 的工作机制
  156. Sentinel 的主要工作机制如下:
  157. - 对主流框架提供适配或者显示的 API,来定义需要保护的资源,并提供设施对资源进行实时统计和调用链路分析。
  158. - 根据预设的规则,结合对资源的实时统计信息,对流量进行控制。同时,Sentinel 提供开放的接口,方便开发者定义及改变规则。
  159. - Sentinel 提供实时的监控系统,方便开发者快速了解目前系统的状态。
  160. ## 3. Sentinel 中的管理控制台(Dashboard)
  161. ### 3.1. 概述
  162. Sentinel 提供一个轻量级的开源控制台,它提供机器发现以及健康情况管理、监控(单机和集群),规则管理和推送的功能。
  163. > 官方团队同时也在阿里云上提供企业级的 Sentinel 服务:[AHAS Sentinel 控制台](https://github.com/alibaba/Sentinel/wiki/AHAS-Sentinel-%E6%8E%A7%E5%88%B6%E5%8F%B0),使用者只需要几个简单的步骤,就能最直观地看到控制台如何实现这些功能,并体验多样化的监控及全自动托管的集群流控能力。
  164. Sentinel 控制台包含如下功能:
  165. - **查看机器列表以及健康情况**:收集 Sentinel 客户端发送的心跳包,用于判断机器是否在线。
  166. - **监控 (单机和集群聚合)**:通过 Sentinel 客户端暴露的监控 API,定期拉取并且聚合应用监控信息,最终可以实现秒级的实时监控。
  167. - **规则管理和推送**:统一管理推送规则。
  168. - **鉴权**:生产环境中鉴权非常重要。这里每个开发者需要根据自己的实际情况进行定制。
  169. > 注意:Sentinel 控制台目前仅支持单机部署。Sentinel 控制台项目提供 Sentinel 功能全集示例,不作为开箱即用的生产环境控制台,若希望在生产环境使用,可参考 [官方文档](https://github.com/alibaba/Sentinel/wiki/%E5%9C%A8%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E4%B8%AD%E4%BD%BF%E7%94%A8-Sentinel) 自行进行定制和改造。
  170. ### 3.2. 获取 Sentinel 控制台
  171. 可以从官方网站中下载最新版本编译后的控制台 jar 包,本次示例使用v1.8.0版本
  172. > 官网下载地址:[https://github.com/alibaba/Sentinel/releases/](https://github.com/alibaba/Sentinel/releases/)
  173. 也可以从最新版本的源码自行构建 Sentinel 控制台:
  174. - 下载 [控制台](https://github.com/alibaba/Sentinel/tree/master/sentinel-dashboard) 工程
  175. - 使用以下命令将代码打包成一个 fat jar: `mvn clean package`
  176. ### 3.3. 启动 Sentinel 控制台
  177. 进入 Sentinel jar 包所在的目录,直接使用 jar 命令启动项目(控制台本身是一个SpringBoot项目)。具体命令如下:

$ java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar

  1. 参数说明:
  2. - `-Dserver.port=8080`:用于指定Sentinel控制台端口为8080
  3. - `-Dsentinel.dashboard.auth.username=sentinel`:用于指定控制台的登录用户名为`sentinel`;
  4. - `-Dsentinel.dashboard.auth.password=123456`:用于指定控制台的登录密码为`123456`;如果省略这两个参数,默认用户和密码均为`sentinel`;
  5. - `-Dserver.servlet.session.timeout=7200`:用于指定 Spring Boot 服务端 session 的过期时间,如 7200 表示 7200 秒;60m 表示 60 分钟,默认为 30 分钟;
  6. > 注:从 Sentinel 1.6.0 起,Sentinel 控制台引入基本的登录功能,默认用户名和密码都是`sentinel`。可以参考 [官方鉴权模块文档](https://sentinelguard.io/zh-cn/docs/dashboard.html#%E9%89%B4%E6%9D%83) 配置用户名和密码。启动Sentinel控制台需要JDK版本为1.8及以上版本。
  7. ### 3.4. 访问 Sentinel 控制台
  8. 通过浏览器访问 `http://127.0.0.1:部署的服务端口号` 进入控制台。默认用户名密码是 sentinel/sentinel<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102124849717_12171.png)
  9. ### 3.5. 扩展:了解控制台的使用原理
  10. Sentinel 的控制台其实就是一个 SpringBoot 编写的程序。只需要将相应的微服务程序注册到控制台上,即在微服务中指定控制台的地址,并且还要开启一个跟控制台传递数据的端口,控制台也可以通过此端口调用微服务中的监控程序获取微服务的各种信息。<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102123008079_15584.png)
  11. ## 4. Sentinel 快速开始
  12. 此部分主要介绍是核心库(Java 客户端)的基础使用示例。包括引入 Sentinel 依赖、定义服务保护的资源、配置参数、通过控制台查询信息等示例
  13. ### 4.1. 微服务客户端接入 Sentinel 控制台
  14. #### 4.1.1. 方式1:依赖 Transport 模块
  15. 客户端需要引入 Transport 模块来与 Sentinel 控制台进行通信。可以通过 pom.xml 引入相关依赖
com.alibaba.csp sentinel-transport-simple-http
  1. #### 4.1.2. 方式2(推荐):整合 Spring Cloud Alibaba
  2. 使用 Spring Cloud 整合了 Spring Cloud Alibaba 的方式来接入 Sentinel。
  3. > _注:需要注意Spring Cloud Alibaba与Spring Cloud的版本关系_。具体详见:[版本说明](#_5-%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E20211222%E6%97%A5%E6%9B%B4%E6%96%B0)章节
  4. 如果需要使用 Spring Cloud Greenwich 版本,在父工程`pom.xml`文件的`<dependencyManagement>`中添加如下内容:
com.alibaba.cloud spring-cloud-alibaba-dependencies 2.1.2.RELEASE pom import
  1. 如果应用使用了 Maven,为微服务集成 Sentinel 只需要在相应子工程中的 pom.xml 文件中加入 sentinel 的依赖即可:
com.alibaba.cloud spring-cloud-starter-alibaba-sentinel
  1. ### 4.2. 定义需要保护资源
  2. 资源是 Sentinel 中的核心概念之一。最常用的资源是项目代码中的 Java 方法。在订单服务中,定义一些请求的方法。参考代码如下:

@RestController @RequestMapping(“demo/sentinel”) public class SentinelDemoController {

  1. @RequestMapping("message1")
  2. public String message1() {
  3. return "message1";
  4. }

}

  1. 在引入 Sentinel 客户端后,以上定义的这些请求方法,都会
  2. ### 4.3. 配置启动参数
  3. 在相应工程的`application.yml`中添加开启`Sentinel`控制台配置信息,通过`spring.cloud.sentinel.transport.dashboard`属性配置控制台的请求路径。

spring: cloud:

  1. # Sentinel 相关配置
  2. sentinel:
  3. transport:
  4. port: 9999 # 跟控制台交流的端口,随意指定一个未使用的端口即可
  5. dashboard: localhost:8080 # 配置sentinel控制台的请求地址
  6. eager: true # 取消Sentinel控制台懒加载,当服务启动时立即加载到控制台中
  1. ### 4.4. 通过 Sentinel 控制台查看机器列表以及健康情况
  2. 默认情况下 Sentinel 会在客户端首次调用的时候才进行初始化,开始向控制台发送心跳包。也可以配置 `sentinel.eager=true`,取消Sentinel控制台懒加载。<br />_通过调用相关服务接口后,打开浏览器即可展示Sentinel的管理控制台,并且可以看到相应服务的列表以及相关信息_<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102142732979_17493.png)
  3. > 在控制台可以定义一些资源的保护规则,具体的规则分类、定义、使用等待详见《基本使用 - 资源与规则》章节
  4. ## 5. 基本使用 - 资源
  5. ### 5.1. 简介
  6. 在快速开始的章节中,已经实现了定义基础保护的资源,但是没有设置任何的保护规则。此章节主要介绍 Sentinel 核心库如何定义地铡来进行资源保护,主要分为几个步骤:
  7. 1. 定义资源
  8. 1. 定义规则
  9. 1. 检验规则是否生效
  10. 先把可能需要保护的资源定义好,之后再配置规则。也可以理解为,只要有了资源,我们就可以在任何时候灵活地定义各种流量控制规则。在编码的时候,只需要考虑这个代码是否需要保护,如果需要保护,就将之定义为一个资源。
  11. > 名词解释:
  12. > - **资源**:可以是任何东西,一个服务,服务里的方法,甚至是一段代码。
  13. > - **规则**:Sentinel 支持以下几种规则:流量控制规则、熔断降级规则、系统保护规则、来源访问控制规则和热点参数规则。Sentinel 的所有规则都可以在内存态中动态地查询及修改,修改之后立即生效
  14. ### 5.2. 定义资源
  15. #### 5.2.1. 方式一:主流框架的默认适配
  16. 对大部分的主流框架,例如 Web Servlet、Dubbo、Spring Cloud、gRPC、Spring WebFlux、Reactor 等都做了适配。只需要引入对应的依赖即可方便地整合 Sentinel。
  17. > _注:快速开始章节的示例介绍就是属于这种默认适配方式_
  18. #### 5.2.2. 方式二:抛出异常的方式定义资源(使用时再整理!)
  19. TODO: 待整理!
  20. #### 5.2.3. 方式三:返回布尔值方式定义资源(使用时再整理!)
  21. TODO: 待整理!
  22. #### 5.2.4. 方式四:注解方式定义资源(使用时再整理!)
  23. Sentinel 支持通过 `@SentinelResource` 注解定义资源,并可以配置 `blockHandler` 和 `fallback` 函数来进行限流之后的处理。示例:

// 原本的业务方法. @SentinelResource(blockHandler = “blockHandlerForGetUser”) public User getUserById(String id) { throw new RuntimeException(“getUserById command failed”); }

// blockHandler 函数,原方法调用被限流/降级/系统保护的时候调用 public User blockHandlerForGetUser(String id, BlockException ex) { return new User(“admin”); }

  1. > 注意 `blockHandler` 函数会在原方法被限流/降级/系统保护的时候调用,而 `fallback` 函数会针对所有类型的异常。请注意 `blockHandler` 和 `fallback` 函数的形式要求,更多指引可以参见 [Sentinel 注解支持文档](93bdbea4c45cf60c2389cfa0aeeac4db)。
  2. #### 5.2.5. 方式五:异步调用支持(使用时再整理!)
  3. TODO: 待整理!
  4. ## 6. 基本使用 - 规则
  5. ### 6.1. 规则的种类
  6. Sentinel 的所有规则都可以在内存态中动态地查询及修改,修改之后立即生效。同时 Sentinel 也提供相关 API,供您来定制自己的规则策略。<br />Sentinel 支持以下几种规则:**流量控制规则**、**熔断降级规则**、**系统保护规则**、**来源访问控制规则**和**热点参数规则**。
  7. ### 6.2. 流量控制规则 (FlowRule)
  8. #### 6.2.1. 定义
  9. 流量控制,其原理是监控应用流量的QPS(每秒查询率) 或并发线程数等指标,当达到指定的阈值时对流量进行控制,以避免被瞬时的流量高峰冲垮,从而保障应用的高可用性。
  10. #### 6.2.2. 控制台规则设置界面
  11. 点击簇点链路,我们就可以看到访问过的接口地址,然后点击对应的流控按钮,进入流控规则配置页面。新增流控规则界面如下<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102173337662_12947.png)<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102173401346_22455.png)<br />图形化界面设置项解析:
  12. - **资源名**:唯一名称,默认是请求路径,可自定义
  13. - **针对来源**:指定对来自哪个微服务的请求进行限流,默认指default,意思是不区分来源,全部限制
  14. - **阈值类型**/**单机阈值**:
  15. - QPS(每秒请求数量):当调用该接口的QPS达到阈值的时候,进行限流
  16. - 线程数:当调用该接口的线程数达到阈值的时候,进行限流
  17. - **是否集群**:暂不需要集群
  18. > **注:同一个资源可以同时有多个限流规则。**
  19. #### 6.2.3. 流量规则重要属性
  20. |
  21. Field
  22. | 说明
  23. | 默认值
  24. |
  25. | --- | --- | --- |
  26. |
  27. resource
  28. | 资源名,资源名是限流规则的作用对象
  29. |
  30. |
  31. |
  32. count
  33. | 限流阈值
  34. |
  35. |
  36. |
  37. grade
  38. | 限流阈值类型,QPS 或线程数模式
  39. | QPS 模式
  40. |
  41. |
  42. limitApp
  43. | 流控针对的调用来源
  44. | `default`,代表不区分调用来源
  45. |
  46. |
  47. strategy
  48. | 调用关系限流策略:直接、链路、关联
  49. | 根据资源本身(直接)
  50. |
  51. |
  52. controlBehavior
  53. | 流控效果(直接拒绝 / 排队等待 / 慢启动模式),不支持按调用关系限流
  54. | 直接拒绝
  55. |
  56. #### 6.2.4. 基于QPS/并发数的流量控制
  57. ##### 6.2.4.1. 基础配置
  58. 设置阈值类型为QPS,单机阈值为3。即每秒请求量大于3的时候开始限流。<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102174308402_1697.png)<br />在【流控规则】页面就可以查看新增的配置。<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102174342244_21718.png)<br />然后访问相应的接口url,快速点击刷新,当 `QPS > 3` 的时候,服务就不能正常响应,而是返回 `Blocked by Sentinel (flow limiting)` 结果。<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102174508588_30609.png)
  59. ##### 6.2.4.2. 配置流控模式
  60. 点击上面设置流控规则的编辑按钮,然后在编辑页面点击【高级选项】,会看到有流控模式一栏。<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102174842507_19646.png)<br />Sentinel 共有三种流控模式,分别是:
  61. - **直接(默认)流控模式**:当指定的接口达到限流条件时,开启限流。直接流控模式是最简单的模式,_在基础配置的章节中就是这种模式_
  62. - **关联流控模式**:当指定接口A关联的资源(接口B)达到限流条件时,开启对指定接口A的限流(适合做应用让步)
  63. 配置限流规则,将流控模式设置为关联,关联资源设置为的 `/demo/sentinel/message2`。<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102175629220_9414.png)<br />使用postman 在1s内连续请求 `/demo/sentinel/message2` 多次(`QPS>3`)<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102180344421_3804.png)<br />此时访问 `/demo/sentinel/message1`接口,会补限流
  64. - **链路流控模式**:当从某个接口过来的资源达到限流条件时,开启限流。(**目前有问题,暂未实现**)
  65. - 它的功能有点类似于针对来源配置项,区别在于:针对来源是针对上级微服务,而链路流控是针对上级接口,也就是说它的粒度更细。
  66. 第1步: 编写一个service,在里面添加一个方法message

@Service @Slf4j public class SentinelDemoServiceImpl implements SentinelDemoService { // Sentinel 支持通过 @SentinelResource 注解定义资源 @SentinelResource(“message”) @Override public void message() { log.info(“SentinelDemoServiceImpl.message 方法执行….”); } }

  1. 在请求类中定义两个请求方法,分别都调用`SentinelDemoServiceImpl`类设置的了保护的资源方法

@RestController @RequestMapping(“demo/sentinel”) public class SentinelDemoController { @Autowired private SentinelDemoService sentinelDemoService;

  1. /* 用于测试链路流控模式:当 /message3 与 /message4 都调用同样的受保护资源 message,
  2. * 可以指定其中一个受流量控制,另一个不受流量控制
  3. */
  4. @RequestMapping("message3")
  5. public String getMessage3() {
  6. sentinelDemoService.message();
  7. return "message3";
  8. }
  9. @RequestMapping("message4")
  10. public String getMessage4() {
  11. sentinelDemoService.message();
  12. return "message4";
  13. }

}

  1. 禁止收敛 URL 的入口 context
  2. > - 从 1.6.3 版本开始,Sentinel Web filter默认收敛所有URL的入口context,因此链路限流不生效。
  3. > - 1.7.0 版本开始(对应SCA的2.1.1.RELEASE),官方在CommonFilter 引入了
  4. > - `WEB_CONTEXT_UNIFY` 参数,用于控制是否收敛context。将其配置为 `false` 即可根据不同的 URL 进行链路限流。
  5. > - `spring.cloud.sentinel.web-context-unify=false`(百度上此方法无效)
  6. 如果是使用 Spring Cloud Alibaba 2.1.0 版本,是无法实现链路限流。如果使用 2.1.1+ 版本,则需要写代码的形式实现链路限流
  7. - 在配置文件中关闭 sentinel 的 `CommonFilter` 实例化

sentinel:

配置关闭 sentinel 的 CommonFilter 实例化。用于测试实现链路限流

filter: enabled: false

  1. 添加一个配置类,用于创建 `CommonFilter` 实例。

/**

  • 1.7.0 版本开始(对应 Spring Cloud Alibaba 的2.1.1.RELEASE),官方在CommonFilter 引入了 WEB_CONTEXT_UNIFY 参数,
  • 用于控制是否收敛context。将其配置为 false 即可根据不同的 URL 进行链路限流。
  • 注意:如果使用 Spring Cloud Alibaba 2.1.1 及更高的版本都需要手动配置此类 */ @Configuration public class FilterContextConfig { @Bean public FilterRegistrationBean sentinelFilterRegistration() {
    1. FilterRegistrationBean registration = new FilterRegistrationBean();
    2. registration.setFilter(new CommonFilter());
    3. registration.addUrlPatterns("/*");
    4. // 入口资源关闭聚合
    5. registration.addInitParameter(CommonFilter.WEB_CONTEXT_UNIFY, "false");
    6. registration.setName("sentinelFilter");
    7. registration.setOrder(1);
    8. return registration;
    } }
  1. 成功关闭后<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102221340116_7794.png)<br />控制台配置限流规则<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102191431937_12319.png)<br />分别访问 /demo/sentinel/message3 和 /demo/sentinel/message4 访问,发现3没问题,4被限流了<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102221508817_2361.png)
  2. ##### 6.2.4.3. 配置流控效果
  3. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220102181815268_9287.png)<br />共有三种选择:
  4. - **快速失败(默认)**: 直接失败,抛出异常,不做任何额外的处理,是最简单的效果
  5. - **Warm Up**:它从开始阈值到最大QPS阈值会有一个缓冲阶段,一开始的阈值是最大QPS阈值的1/3,然后慢慢增长,直到最大阈值,适用于将突然增大的流量转换为缓步增长的场景。
  6. - **排队等待**:让请求以均匀的速度通过,单机阈值为每秒通过数量,其余的排队等待; 它还会让设置一个超时时间,当请求超过超时间时间还未处理,则会被丢弃。
  7. ### 6.3. 熔断降级规则 (DegradeRule)
  8. #### 6.3.1. 定义
  9. 降级规则就是设置当满足什么条件的时候,对服务进行降级。
  10. > 注意:
  11. > - 本文档针对 Sentinel 1.8.0 及以上版本。1.8.0 版本对熔断降级特性进行了全新的改进升级,请使用最新版本以更好地利用熔断降级的能力。
  12. > - 同一个资源可以同时有多个降级规则。
  13. #### 6.3.2. 熔断策略
  14. Sentinel 提供以下几种熔断策略:
  15. - 慢调用比例/平均响应时间 (`SLOW_REQUEST_RATIO`):选择以慢调用比例作为阈值,需要设置允许的慢调用 RT(即最大的响应时间),请求的响应时间大于该值则统计为慢调用。当单位统计时长(`statIntervalMs`)内请求数目大于设置的最小请求数目,并且慢调用的比例大于阈值,则接下来的熔断时长内请求会自动被熔断。经过熔断时长后熔断器会进入探测恢复状态(HALF-OPEN 状态),若接下来的一个请求响应时间小于设置的慢调用 RT 则结束熔断,若大于设置的慢调用 RT 则会再次被熔断。
  16. - 异常比例 (`ERROR_RATIO`):当单位统计时长(`statIntervalMs`)内请求数目大于设置的最小请求数目,并且异常的比例大于阈值,则接下来的熔断时长内请求会自动被熔断。经过熔断时长后熔断器会进入探测恢复状态(HALF-OPEN 状态),若接下来的一个请求成功完成(没有错误)则结束熔断,否则会再次被熔断。异常比率的阈值范围是 `[0.0, 1.0]`,代表 0% - 100%。
  17. - 异常数 (`ERROR_COUNT`):当单位统计时长内的异常数目超过阈值之后会自动进行熔断。经过熔断时长后熔断器会进入探测恢复状态(HALF-OPEN 状态),若接下来的一个请求成功完成(没有错误)则结束熔断,否则会再次被熔断。
  18. #### 6.3.3. 熔断降级规则重要属性
  19. |
  20. Field
  21. | 说明
  22. | 默认值
  23. |
  24. | --- | --- | --- |
  25. |
  26. resource
  27. | 资源名,即规则的作用对象
  28. |
  29. |
  30. |
  31. grade
  32. | 熔断策略,支持慢调用比例/异常比例/异常数策略
  33. | 慢调用比例
  34. |
  35. |
  36. count
  37. | 慢调用比例模式下为慢调用临界 RT(超出该值计为慢调用);异常比例/异常数模式下为对应的阈值
  38. |
  39. |
  40. |
  41. timeWindow
  42. | 熔断时长,单位为 s
  43. |
  44. |
  45. |
  46. minRequestAmount
  47. | 熔断触发的最小请求数,请求数小于该值时即使异常比率超出阈值也不会熔断(1.7.0 引入)
  48. | 5
  49. |
  50. |
  51. statIntervalMs
  52. | 统计时长(单位为 ms),如 `60*1000` 代表分钟级(1.8.0 引入)
  53. | 1000 ms
  54. |
  55. |
  56. slowRatioThreshold
  57. | 慢调用比例阈值,仅慢调用比例模式有效(1.8.0 引入)
  58. |
  59. |
  60. #### 6.3.4. 慢调用比例/平均响应时间
  61. 点击簇点链路,可以看到访问过的接口地址,然后点击对应的降级按钮,进入降级规则配置页面。新增降级规则界面如下<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102232042593_16710.png)<br />慢调用比例/平均响应时间规则的设置<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102232325010_32670.png)<br />以上设置是当资源的平均响应时间超过阈值(以 ms 为单位)之后,资源进入准降级状态。如果接下来 1s 内持续进入 5 个请求,它们的 RT 都持续超过这个阈值,那么在接下的时间窗口(10s)之内,就会对这个方法进行服务降级。10s之后恢复正常,进行下一轮判断。<br />在【降级规则】可查询所有创建的降级规则<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102232459207_9502.png)
  62. > 注意:Sentinel 默认统计的 RT 上限是 `4900 ms`,超出此阈值的都会算作 `4900 ms`,若需要变更此上限可以通过启动配置项 `-Dcsp.sentinel.statistic.max.rt=xxx` 来配置。
  63. #### 6.3.5. 异常比例
  64. 定义请求方法模拟异常

int i = 0;

/ 模拟出现异常的请求方法 / @RequestMapping(“message5”) public String getMessage5() { i++; // 模拟异常出现的机率是0.333 if (i % 3 == 0) { throw new RuntimeException(); } return “message5”; }

  1. 设置异常比例规则<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220102233518765_16434.png)<br />以上设置是当资源的每秒异常总数占通过量的比值超过阈值(0.25)之后,资源进入降级状态,即在接下的时间窗口(5s)之内,对这个方法的调用都会自动地返回。
  2. #### 6.3.6. 异常数
  3. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220102233815788_1195.png)<br />以上设置是当资源近 1 分钟的异常数目超过阈值之后会进行服务降级。经过熔断时长(5s)后熔断器会进入探测恢复状态(HALF-OPEN 状态),若接下来的一个请求成功完成(没有错误)则结束熔断,否则会再次被熔断。
  4. ### 6.4. 热点规则 (ParamFlowRule)
  5. #### 6.4.1. 定义
  6. 热点即经常访问的数据。很多时候希望统计某个热点数据中访问频次最高的 Top K 数据,并对其访问进行限制。<br />热点参数限流会统计传入参数中的热点参数,并根据配置的限流阈值与模式,对包含热点参数的资源调用进行限流。热点参数限流可以看做是一种特殊的、更细粒度的流量控制,仅对包含热点参数的资源调用生效。<br />Sentinel 利用 LRU 策略统计最近最常访问的热点参数,结合令牌桶算法来进行参数级别的流控。
  7. #### 6.4.2. 热点参数规则重要属性
  8. 热点参数规则(ParamFlowRule)类似于流量控制规则(FlowRule)
  9. |
  10. 属性
  11. | 说明
  12. | 默认值
  13. |
  14. | --- | --- | --- |
  15. |
  16. resource
  17. | 资源名,必填
  18. |
  19. |
  20. |
  21. count
  22. | 限流阈值,必填
  23. |
  24. |
  25. |
  26. grade
  27. | 限流模式
  28. | QPS 模式
  29. |
  30. |
  31. durationInSec
  32. | 统计窗口时间长度(单位为秒),1.6.0 版本开始支持
  33. | 1s
  34. |
  35. |
  36. controlBehavior
  37. | 流控效果(支持快速失败和匀速排队模式),1.6.0 版本开始支持
  38. | 快速失败
  39. |
  40. |
  41. maxQueueingTimeMs
  42. | 最大排队等待时长(仅在匀速排队模式生效),1.6.0 版本开始支持
  43. | 0ms
  44. |
  45. |
  46. paramIdx
  47. | 热点参数的索引,必填,对应 `SphU.entry(xxx, args)` 中的参数索引位置
  48. |
  49. |
  50. |
  51. paramFlowItemList
  52. | 参数例外项,可以针对指定的参数值单独设置限流阈值,不受前面 `count` 阈值的限制。**仅支持基本类型和字符串类型**
  53. |
  54. |
  55. |
  56. clusterMode
  57. | 是否是集群参数流控规则
  58. | `false`
  59. |
  60. |
  61. clusterConfig
  62. | 集群流控相关配置
  63. |
  64. |
  65. #### 6.4.3. 热点规则基础使用
  66. - 编写资源,带有参数的请求方法

/ 测试热点规则限流 / @SentinelResource(“paramFlowRuleDemo”) // 注意:必须在资源上使用 @SentinelResource 注解,否则热点规则不生效 @RequestMapping(“paramFlowRuleDemo”) public String getMessage6(String name, Integer age) { return String.format(“name: %s, age: %d”, name, age); }

  1. - 配置热点规则
  2. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220103170238101_29386.jpg)
  3. - 分别用两个参数访问,会发现只对第一个参数限流了
  4. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220103170535312_32732.png)
  5. #### 6.4.4. 热点规则增强使用
  6. 在编辑热点规则中,有高级选项(_新增时没有_)。参数例外项允许对一个参数的具体值进行流控<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220103170706654_6964.png)<br />测试结果<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220103170919544_6649.png)
  7. ### 6.5. 访问控制规则(黑白名单)(AuthorityRule)(暂时有问题)
  8. #### 6.5.1. 定义
  9. 当需要根据调用方来限制资源是否通过,此时可以使用 Sentinel 的黑白名单控制的功能。黑白名单根据资源的请求来源(origin)限制资源是否通过,
  10. - 若配置白名单则只有请求来源位于白名单内时才可通过;
  11. - 若配置黑名单则请求来源位于黑名单时不通过,其余的请求通过。
  12. #### 6.5.2. 规则配置项
  13. 黑白名单规则(`AuthorityRule`)主要有以下配置项:
  14. - `resource`:资源名,即限流规则的作用对象
  15. - `limitApp`:对应的黑名单/白名单,不同 origin 用 `,` 分隔,如 `appA,appB`
  16. - `strategy`:限制模式,`AUTHORITY_WHITE` 为白名单模式,`AUTHORITY_BLACK` 为黑名单模式,默认为白名单模式
  17. #### 6.5.3. 基础使用
  18. - 定义规则
  19. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220103174843575_8687.jpg)<br />其实这个位置要填写的是来源标识,Sentinel 提供了 `RequestOriginParser` 接口来处理来源。只要 Sentinel 保护的接口资源被访问,Sentinel 就会调用 `RequestOriginParser` 的实现类去解析访问来源。
  20. - 编写自定义来源处理规则处理类

@Component public class RequestOriginParserHandler implements RequestOriginParser { @Override public String parseOrigin(HttpServletRequest request) { / 定义区分来源: 本质作用是通过 HttpServletRequest 对象获取请求一些信息 / String client = request.getParameter(“client”); if (StringUtils.isEmpty(client)) { throw new RuntimeException(“client is not empty”); } return client; } }

  1. - 测试访问:[http://127.0.0.1:8091/demo/sentinel/message1?client=pc](http://127.0.0.1:8091/demo/sentinel/message1?client=pc)
  2. ### 6.6. 系统保护规则 (SystemRule)
  3. #### 6.6.1. 定义
  4. Sentinel 系统自适应保护从整体维度对应用入口流量进行控制,结合应用的 Load、总体平均 RT、入口 QPS 和线程数等几个维度的监控指标,让系统的入口流量和系统的负载达到一个平衡,让系统尽可能跑在最大吞吐量的同时保证系统整体的稳定性。
  5. #### 6.6.2. 系统规则重要的属性
  6. |
  7. Field
  8. | 说明
  9. | 默认值
  10. |
  11. | --- | --- | --- |
  12. |
  13. highestSystemLoad
  14. | `load1` 触发值,用于触发自适应控制阶段
  15. | -1 (不生效)
  16. |
  17. |
  18. avgRt
  19. | 所有入口流量的平均响应时间
  20. | -1 (不生效)
  21. |
  22. |
  23. maxThread
  24. | 入口流量的最大并发数
  25. | -1 (不生效)
  26. |
  27. |
  28. qps
  29. | 所有入口资源的 QPS
  30. | -1 (不生效)
  31. |
  32. |
  33. highestCpuUsage
  34. | 当前系统的 CPU 使用率(0.0-1.0)
  35. | -1 (不生效)
  36. |
  37. #### 6.6.3. 系统规则支持类型
  38. 系统保护规则是应用整体维度的,而不是资源维度的,并且**仅对入口流量生效**。入口流量指的是进入应用的流量(`EntryType.IN`),比如 Web 服务或 Dubbo 服务端接收的请求,都属于入口流量。<br />系统规则支持以下的阈值类型:
  39. - **Load**(仅对 Linux/Unix-like 机器生效):当系统 load1 超过阈值,且系统当前的并发线程数超过系统容量时才会触发系统保护。系统容量由系统的 `maxQps * minRt` 计算得出。设定参考值一般是 `CPU cores * 2.5`。
  40. - **CPU usage**(1.5.0+ 版本):当系统 CPU 使用率超过阈值即触发系统保护(取值范围 0.0-1.0)。
  41. - **RT**:当单台机器上所有入口流量的平均 RT 达到阈值即触发系统保护,单位是毫秒。
  42. - **线程数**:当单台机器上所有入口流量的并发线程数达到阈值即触发系统保护。
  43. - **入口 QPS**:当单台机器上所有入口流量的 QPS 达到阈值即触发系统保护。
  44. ### 6.7. 自定义规则异常返回
  45. - 定义公共的响应类
  46. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220103232551532_23204.png)<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220103232603038_3660.png)
  47. - 实现 `com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler` 接口,在 Sentinel 进行规则拦截时,会调用到接口的 `blocked` 方法。所以在此方法中,根据不同的异常类型自定义相应的返回内容。
  48. #### 6.7.1. 旧版本(1.8以前版本)实现 UrlBlockHandler 的接口

@Component public class ExceptionPageHandler implements UrlBlockHandler { /**

  1. * Handle the request when blocked.
  2. *
  3. * @param request Servlet request
  4. * @param response Servlet response
  5. * @param ex the block exception.
  6. * @throws IOException some error occurs
  7. */
  8. @Override
  9. public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws IOException {
  10. response.setContentType("application/json;charset=utf-8");
  11. CommonResult responseData = null;
  12. // BlockException 是 Sentinel 异常接口,包含 Sentinel 的五种异常
  13. if (ex instanceof FlowException) {
  14. // FlowException 限流异常
  15. responseData = CommonResult.failed(ResultCode.SENTINEL_FLOW);
  16. } else if (ex instanceof DegradeException) {
  17. // DegradeException 降级异常
  18. responseData = CommonResult.failed(ResultCode.SENTINEL_DEGRADE);
  19. } else if (ex instanceof ParamFlowException) {
  20. // ParamFlowException 参数限流异常
  21. responseData = CommonResult.failed(ResultCode.SENTINEL_PARAM_FLOW);
  22. } else if (ex instanceof AuthorityException) {
  23. // AuthorityException 授权异常
  24. responseData = CommonResult.failed(ResultCode.SENTINEL_AUTHORITY);
  25. } else if (ex instanceof SystemBlockException) {
  26. // SystemBlockException 系统负载异常
  27. responseData = CommonResult.failed(ResultCode.SENTINEL_SYSTEM_BLOCK);
  28. }
  29. response.getWriter().write(JSON.toJSONString(responseData));
  30. }

}

  1. #### 6.7.2. 新版本(1.8版本)实现 BlockExceptionHandler 的接口

@Component public class ExceptionPageHandler implements BlockExceptionHandler { /**

  1. * Handle the request when blocked.
  2. *
  3. * @param request Servlet request
  4. * @param response Servlet response
  5. * @param e the block exception
  6. * @throws Exception users may throw out the BlockException or other error occurs
  7. */
  8. @Override
  9. public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
  10. response.setContentType("application/json;charset=utf-8");
  11. CommonResult responseData = null;
  12. // BlockException 是 Sentinel 异常接口,包含 Sentinel 的五种异常
  13. if (e instanceof FlowException) {
  14. // FlowException 限流异常
  15. responseData = CommonResult.failed(ResultCode.SENTINEL_FLOW);
  16. } else if (e instanceof DegradeException) {
  17. // DegradeException 降级异常
  18. responseData = CommonResult.failed(ResultCode.SENTINEL_DEGRADE);
  19. } else if (e instanceof ParamFlowException) {
  20. // ParamFlowException 参数限流异常
  21. responseData = CommonResult.failed(ResultCode.SENTINEL_PARAM_FLOW);
  22. } else if (e instanceof AuthorityException) {
  23. // AuthorityException 授权异常
  24. responseData = CommonResult.failed(ResultCode.SENTINEL_AUTHORITY);
  25. } else if (e instanceof SystemBlockException) {
  26. // SystemBlockException 系统负载异常
  27. responseData = CommonResult.failed(ResultCode.SENTINEL_SYSTEM_BLOCK);
  28. }
  29. response.getWriter().write(JSON.toJSONString(responseData));
  30. }

}

  1. ### 6.8. Sentinel 规则持久化
  2. #### 6.8.1. 概述
  3. 上面的规则配置,都是存在内存中的。即如果应用重启,这个规则就会失效。Sentinel 提供了开放的接口,可以通过实现 DataSource 接口的方式,来自定义规则的存储数据源。通常的建议有:
  4. - 整合动态配置系统,如 ZooKeeper、Nacos 等,动态地实时刷新配置规则
  5. - 结合 RDBMS、NoSQL、VCS 等来实现该规则
  6. - 配合 Sentinel Dashboard 使用
  7. #### 6.8.2. 规则推送原理
  8. 本地文件数据源会定时轮询文件的变更,读取规则。这样既可以在应用本地直接修改文件来更新规则,也可以通过 Sentinel 控制台推送规则。以本地文件数据源为例,推送过程如下图所示:<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220104113637133_22401.png)<br />首先 Sentinel 控制台通过 API 将规则推送至客户端并更新到内存中,接着注册的写数据源会将新的规则保存到本地的文件中。
  9. #### 6.8.3. 实现规则持久化步骤
  10. 注册数据源。可以借助 Sentinel 的 InitFunc SPI 扩展接口。只需要实现自己的 `InitFunc` 接口,在 `init` 方法中编写注册数据源的逻辑。

public class DataSourceInitFunc implements InitFunc { / 此处初始化时,是无法读取配置文件 / @Value(“${spring.application.name}”) private String appcationName;

  1. @Override
  2. public void init() throws Exception {
  3. // String ruleDir = System.getProperty("user.home") + "/sentinel-rules/" + appcationName;
  4. String ruleDir = "D:/deployment-environment/sentinel/sentinel-rules/service-order/";
  5. String flowRulePath = ruleDir + "/flow-rule.json";
  6. String degradeRulePath = ruleDir + "/degrade-rule.json";
  7. String systemRulePath = ruleDir + "/system-rule.json";
  8. String authorityRulePath = ruleDir + "/authority-rule.json";
  9. String paramFlowRulePath = ruleDir + "/param-flow-rule.json";
  10. this.mkdirIfNotExits(ruleDir);
  11. this.createFileIfNotExits(flowRulePath);
  12. this.createFileIfNotExits(degradeRulePath);
  13. this.createFileIfNotExits(systemRulePath);
  14. this.createFileIfNotExits(authorityRulePath);
  15. this.createFileIfNotExits(paramFlowRulePath);
  16. // 流控规则
  17. ReadableDataSource<String, List<FlowRule>> flowRuleRDS = new FileRefreshableDataSource<>(
  18. flowRulePath,
  19. flowRuleListParser
  20. );
  21. FlowRuleManager.register2Property(flowRuleRDS.getProperty());
  22. WritableDataSource<List<FlowRule>> flowRuleWDS = new FileWritableDataSource<>(
  23. flowRulePath,
  24. this::encodeJson
  25. );
  26. WritableDataSourceRegistry.registerFlowDataSource(flowRuleWDS);
  27. // 降级规则
  28. ReadableDataSource<String, List<DegradeRule>> degradeRuleRDS = new FileRefreshableDataSource<>(
  29. degradeRulePath,
  30. degradeRuleListParser
  31. );
  32. DegradeRuleManager.register2Property(degradeRuleRDS.getProperty());
  33. WritableDataSource<List<DegradeRule>> degradeRuleWDS = new FileWritableDataSource<>(
  34. degradeRulePath,
  35. this::encodeJson
  36. );
  37. WritableDataSourceRegistry.registerDegradeDataSource(degradeRuleWDS);
  38. // 系统规则
  39. ReadableDataSource<String, List<SystemRule>> systemRuleRDS = new FileRefreshableDataSource<>(
  40. systemRulePath,
  41. systemRuleListParser
  42. );
  43. SystemRuleManager.register2Property(systemRuleRDS.getProperty());
  44. WritableDataSource<List<SystemRule>> systemRuleWDS = new FileWritableDataSource<>(
  45. systemRulePath,
  46. this::encodeJson
  47. );
  48. WritableDataSourceRegistry.registerSystemDataSource(systemRuleWDS);
  49. // 授权规则
  50. ReadableDataSource<String, List<AuthorityRule>> authorityRuleRDS = new FileRefreshableDataSource<>(
  51. authorityRulePath,
  52. authorityRuleListParser
  53. );
  54. AuthorityRuleManager.register2Property(authorityRuleRDS.getProperty());
  55. WritableDataSource<List<AuthorityRule>> authorityRuleWDS = new FileWritableDataSource<>(
  56. authorityRulePath,
  57. this::encodeJson
  58. );
  59. WritableDataSourceRegistry.registerAuthorityDataSource(authorityRuleWDS);
  60. // 热点参数规则
  61. ReadableDataSource<String, List<ParamFlowRule>> paramFlowRuleRDS = new FileRefreshableDataSource<>(
  62. paramFlowRulePath,
  63. paramFlowRuleListParser
  64. );
  65. ParamFlowRuleManager.register2Property(paramFlowRuleRDS.getProperty());
  66. WritableDataSource<List<ParamFlowRule>> paramFlowRuleWDS = new FileWritableDataSource<>(
  67. paramFlowRulePath,
  68. this::encodeJson
  69. );
  70. ModifyParamFlowRulesCommandHandler.setWritableDataSource(paramFlowRuleWDS);
  71. }
  72. private Converter<String, List<FlowRule>> flowRuleListParser = source -> JSON.parseObject(
  73. source,
  74. new TypeReference<List<FlowRule>>() {
  75. }
  76. );
  77. private Converter<String, List<DegradeRule>> degradeRuleListParser = source -> JSON.parseObject(
  78. source,
  79. new TypeReference<List<DegradeRule>>() {
  80. }
  81. );
  82. private Converter<String, List<SystemRule>> systemRuleListParser = source -> JSON.parseObject(
  83. source,
  84. new TypeReference<List<SystemRule>>() {
  85. }
  86. );
  87. private Converter<String, List<AuthorityRule>> authorityRuleListParser = source -> JSON.parseObject(
  88. source,
  89. new TypeReference<List<AuthorityRule>>() {
  90. }
  91. );
  92. private Converter<String, List<ParamFlowRule>> paramFlowRuleListParser = source -> JSON.parseObject(
  93. source,
  94. new TypeReference<List<ParamFlowRule>>() {
  95. }
  96. );
  97. private void mkdirIfNotExits(String filePath) throws IOException {
  98. File file = new File(filePath);
  99. if (!file.exists()) {
  100. file.mkdirs();
  101. }
  102. }
  103. private void createFileIfNotExits(String filePath) throws IOException {
  104. File file = new File(filePath);
  105. if (!file.exists()) {
  106. file.createNewFile();
  107. }
  108. }
  109. private <T> String encodeJson(T t) {
  110. return JSON.toJSONString(t);
  111. }

}

  1. 在对应的类名添加到位于资源目录(通常是 resource 目录)下的 `META-INF/services` 目录下的 `com.alibaba.csp.sentinel.init.InitFunc` 文件中,添加自定义 `InitFunc` 接口实现类全限定名

com.moon.order.config.DataSourceInitFunc

  1. 当初次访问任意资源的时候,Sentinel 就可以自动去注册对应的数据源了。
  2. ## 7. [SentinelResource ](/SentinelResource ) 注解
  3. ### 7.1. 定义
  4. Sentinel 提供了 `@SentinelResource` 注解用于定义资源,并提供了 AspectJ 的扩展用于自动定义资源、处理 `BlockException` 等。
  5. ### 7.2. 注解属性
  6. `@SentinelResource` 用于定义资源,并提供可选的异常处理和 `fallback` 配置项。 `@SentinelResource` 注解包含以下属性:
  7. - `value`:资源名称,必需项(不能为空)
  8. - `entryType`:entry 类型,标记流量的方向,可选项 `EntryType.IN`/`EntryType.OUT`(默认为 `EntryType.OUT`)
  9. - `blockHandler` / `blockHandlerClass`: `blockHandler` 对应处理 `BlockException` 的函数名称,可选项。函数签名和位置要求:
  10. - `blockHandler` 函数访问范围需要是 `public`
  11. - 返回类型需要与原方法相匹配,参数类型需要和原方法相匹配并且最后加一个额外的参数,类型为 `BlockException`。
  12. - `blockHandler` 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 `blockHandlerClass` 为对应的类的 `Class` 对象,注意对应的函数必需为 `static` 函数,否则无法解析。
  13. - `fallback`/`fallbackClass`:`fallback` 函数名称,可选项,用于在抛出异常的时候提供 `fallback` 处理逻辑。`fallback` 函数可以针对所有类型的异常(除了 `exceptionsToIgnore` 里面排除掉的异常类型)进行处理。`fallback` 函数签名和位置要求:
  14. - 返回值类型必须与原函数返回值类型一致;
  15. - 方法参数列表需要和原函数一致,或者可以额外多一个 `Throwable` 类型的参数用于接收对应的异常。
  16. - `fallback` 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 `fallbackClass` 为对应的类的 `Class` 对象,注意对应的函数必需为 `static` 函数,否则无法解析。
  17. - `defaultFallback`(since 1.6.0):默认的 `fallback` 函数名称,可选项,通常用于通用的 `fallback` 逻辑(即可以用于很多服务或方法)。默认 `fallback` 函数可以针对所有类型的异常(除了`exceptionsToIgnore`里面排除掉的异常类型)进行处理。若同时配置了 `fallback` 和 `defaultFallback`,则只有 `fallback` `会生效。defaultFallback` 函数签名要求:
  18. - 返回值类型必须与原函数返回值类型一致;
  19. - 方法参数列表需要为空,或者可以额外多一个 `Throwable` 类型的参数用于接收对应的异常。
  20. - `defaultFallback` 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 `fallbackClass` 为对应的类的 `Class` 对象,注意对应的函数必需为 `static` 函数,否则无法解析。
  21. - `exceptionsToIgnore`(since 1.6.0):用于指定哪些异常被排除掉,不会计入异常统计中,也不会进入 `fallback` 逻辑中,而是会原样抛出。
  22. - `exceptionsToTrace`:需要trace的异常。
  23. - `resourceType`(since 1.7.0):分类
  24. > 注:
  25. > - 1.8.0 版本开始,`defaultFallback` 支持在类级别进行配置。
  26. > - 1.6.0 之前的版本 `fallback` 函数只针对降级异常(`DegradeException`)进行处理,**不能针对业务异常进行处理**。
  27. 特别地,若 `blockHandler` 和 `fallback` 都进行了配置,则被限流降级而抛出 `BlockException` 时只会进入 `blockHandler` 处理逻辑。若未配置 `blockHandler`、`fallback` 和 `defaultFallback`,则被限流降级时会将 `BlockException` **直接抛出**(若方法本身未定义 throws BlockException 则会被 JVM 包装一层 `UndeclaredThrowableException`)。
  28. ### 7.3. 定义限流和降级后的处理方法
  29. #### 7.3.1. 保护方法与处理方法定义在同一类中

@Service @Slf4j public class SentinelDemoServiceImpl implements SentinelDemoService {

  1. private static int count = 0;
  2. /*
  3. * @SentinelResource 注解是用于指定需要 Sentinel 保护的方法上
  4. * blockHandler属性:声明熔断时调用的降级方法
  5. * fallback属性:声明抛出异常时执行的降级方法
  6. * value属性:设置自定义的资源名称,如不设置,默认值是“当前全类名.方法名”
  7. */
  8. @SentinelResource(value = "sentinelResourceBlockHandler", blockHandler = "blockHandler")
  9. @Override
  10. public String sentinelResourceBlockHandler(String text) {
  11. return "BlockException 异常处理测试方法。函数默认需要和原方法在同一个类中";
  12. }
  13. @SentinelResource(value = "sentinelResourceFallback", fallback = "fallback")
  14. @Override
  15. public String sentinelResourceFallback(String text) {
  16. count++;
  17. if (count % 3 == 0) {
  18. throw new RuntimeException("发生异常了。");
  19. }
  20. return "抛出异常处理测试方法。函数默认需要和原方法在同一个类中";
  21. }
  22. /*
  23. * 定义@SentinelResource注解相应的熔断降级方法,函数的要求:
  24. * 1.必须是public修饰
  25. * 2.返回类型与原方法一致
  26. * 3.参数类型需要和原方法相匹配,并在最后加BlockException类型的参数
  27. * 4.默认需和原方法在同一个类中。若希望使用其他类的函数,可配置blockHandlerClass属性,并指定blockHandlerClass里面的方法,注意函数必需为 `static` 修饰的
  28. */
  29. public String blockHandler(String text, BlockException e) {
  30. log.info("当前方法入参text: {}", text);
  31. e.printStackTrace();
  32. return "触发本类内熔断的降级方法";
  33. }
  34. /*
  35. * 定义@SentinelResource注解相应的抛出异常的降级方法,函数的要求:
  36. * 1.返回类型与原方法一致
  37. * 2.参数类型需要和原方法相匹配,Sentinel 1.6开始,也可在方法最后加Throwable类型的参数
  38. * 3.默认需和原方法在同一个类中。若希望使用其他类的函数,可配置fallbackClass,并指定fallbackClass里面的方法,注意函数必需为 `static` 修饰的
  39. */
  40. public String fallback(String text, Throwable throwable) {
  41. log.info("当前方法入参text: {}", text);
  42. throwable.printStackTrace();
  43. return "触发本类内抛出异常执行的降级方法";
  44. }

}

  1. #### 7.3.2. 处理方法定义在外部类中
  2. 定义需要保护的方法

@Service @Slf4j public class SentinelDemoServiceImpl implements SentinelDemoService {

  1. private static int count = 0;
  2. @SentinelResource(value = "sentinelResourceBlockHandlerOut",
  3. blockHandlerClass = BlockHandlerOutDemo.class,
  4. blockHandler = "blockHandler")
  5. @Override
  6. public String sentinelResourceBlockHandlerOut(String text) {
  7. return "外部类方式处理 BlockException 异常的测试方法";
  8. }
  9. @SentinelResource(value = "sentinelResourceFallbackOut",
  10. fallbackClass = FallbackOutDemo.class,
  11. fallback = "fallback")
  12. @Override
  13. public String sentinelResourceFallbackOut(String text) {
  14. count++;
  15. if (count % 3 == 0) {
  16. throw new RuntimeException("发生异常了。");
  17. }
  18. return "外部类方式处理抛出异常处理的测试方法";
  19. }

}

  1. 在其它的类分别定义处理方法,注意:**对应的函数必需为 `static` 函数**

@Slf4j public class BlockHandlerOutDemo { public static String blockHandler(String text, BlockException e) { log.info(“当前方法入参text: {}”, text); e.printStackTrace(); return “外部类方式处理 BlockException 异常”; } }

@Slf4j public class FallbackOutDemo { public static String fallback(String text, Throwable throwable) { log.info(“当前方法入参text: {}”, text); throwable.printStackTrace(); return “外部类方式处理抛出异常处理”; } }

  1. ## 8. 基于 Sentinel 的服务保护
  2. ### 8.1. Sentinel 对通用资源保护
  3. #### 8.1.1. 基础说明
  4. 通用资源保护是指,无论是使用哪种远程调用的技术,只在需要被保护的方法上使用`@SentinelResource`注解进行熔断配置即可。与Hystrix不同的是,Sentinel对抛出异常和熔断降级做了更加细致的区分,通过`blockHandler`属性指定熔断降级方法;通过`fallback`属性指定触发异常执行的降级方法。<br />**特别注意:若`blockHandler`和`fallback`都进行了配置,则被限流降级而抛出`BlockException`时只会进入`blockHandler`处理逻辑。若未配置`blockHandler`、`fallback`和`defaultFallback`,则被限流降级时会将`BlockException`直接抛出。**
  5. #### 8.1.2. 使用示例
  6. 修改`shop-service-order-resttemplate`工程的`OrderController`,在方法上使用`@SentinelResource`注解增加熔断保护配置,并编写熔断、异常的降级方法

@RestController @RequestMapping(“order”) public class OrderController { / 日志对象 / private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);

  1. // 注入HTTP请求工具类RestTemplate
  2. @Autowired
  3. private RestTemplate restTemplate;
  4. /*
  5. * @SentinelResource注解是用于指定需要熔断保护的方法上
  6. * blockHandler属性:声明熔断时调用的降级方法
  7. * fallback属性:声明抛出异常时执行的降级方法
  8. * value属性:设置自定义的资源名称,如不设置,默认值是“当前全类名.方法名”
  9. */
  10. @SentinelResource(value = "createOrderById", blockHandler = "createOrderBlockHandler", fallback = "createOrderFallback")
  11. @PostMapping("/{id}")
  12. public String createOrder(@PathVariable Long id) {
  13. // 调用服务
  14. Product product = restTemplate.getForObject("http://shop-service-product/product/" + id, Product.class);
  15. LOGGER.info("当前下单的商品是: ${}", product);
  16. return "创建订单成功";
  17. }
  18. /*
  19. * 定义@SentinelResource注解相应的熔断降级方法,函数的要求:
  20. * 1.必须是public修饰
  21. * 2.返回类型与原方法一致
  22. * 3.参数类型需要和原方法相匹配,并在最后加BlockException类型的参数
  23. * 4.默认需和原方法在同一个类中。若希望使用其他类的函数,可配置blockHandlerClass属性,并指定blockHandlerClass里面的方法
  24. */
  25. public String createOrderBlockHandler(Long id, BlockException e) {
  26. LOGGER.info("当前下单的商品id是: ${}", id);
  27. e.printStackTrace();
  28. return "触发熔断的降级方法";
  29. }
  30. /*
  31. * 定义@SentinelResource注解相应的抛出异常的降级方法,函数的要求:
  32. * 1.返回类型与原方法一致
  33. * 2.参数类型需要和原方法相匹配,Sentinel 1.6开始,也可在方法最后加Throwable类型的参数
  34. * 3.默认需和原方法在同一个类中。若希望使用其他类的函数,可配置fallbackClass,并指定fallbackClass里面的方法
  35. */
  36. public String createOrderFallback(Long id, Throwable throwable) {
  37. LOGGER.info("当前下单的商品id是: ${}", id);
  38. throwable.printStackTrace();
  39. return "抛出异常执行的降级方法";
  40. }

}

  1. #### 8.1.3. 测试
  2. 直接通过控制台方式添加/修改降级规则如下:<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20201022090424537_5177.png)<br />测试结果:使用post请求`http://127.0.0.1:9002/order/1`成功,然后请求`http://127.0.0.1:9002/order/2`两次后都执行了异常处理的方法,此时再次请求`http://127.0.0.1:9002/order/1`,会执行熔断方法,等待10s后,再次请求就会恢复成功响应<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20201022091446319_28012.png)
  3. #### 8.1.4. Sentinel 加载本地配置
  4. **一条限流规则主要由下面几个因素组成**:
  5. - `resource`:资源名,即限流规则的作用对象
  6. - `count`:限流阈值
  7. - `grade`:限流阈值类型(QPS 或并发线程数)
  8. - `limitApp`:流控针对的调用来源,若为`default`则不区分调用来源
  9. - `strategy`:调用关系限流策略
  10. - `controlBehavior`:流量控制效果(直接拒绝、Warm Up、匀速排队)
  11. Sentinel可以通过控制台的方式修改相应服务的降级规则等相关参数。<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20201022101559417_4291.png)<br />但会存在一定问题,因为通过控制台新增/修改的规则配置,都保存在相应服务的内存中,如果此服务重启后,内存中这些配置都没有了。所以一般都会将相应的Sentinel规则保存在本地文件,然后设置Sentinel加载本地配置。实现本地配置的步骤如下:
  12. 1. 修改服务工程的`application.yml`配置文件,增加以下配置

配置Sentinel读取本地文件配置限流规则

spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json spring.cloud.sentinel.datasource.ds1.file.data-type=json spring.cloud.sentinel.datasource.ds1.file.rule-type=flow

  1. ```
  2. spring:
  3. cloud:
  4. # Sentinel 相关配置
  5. sentinel:
  6. # 配置Sentinel读取本地文件配置限流规则
  7. datasource:
  8. ds1:
  9. file:
  10. file: classpath:flowrule.json
  11. data-type: json
  12. rule-type: flow
  1. 在工程的resources目录下,创建flowrule.json文件,具体配置内容如下: ``` [ { “resource”: “orderFindById”, “controlBehavior”: 0, “count”: 1, “grade”: 1, “limitApp”: “default”, “strategy”: 0 } ]
  1. > _注意:其中`resource`字段是相应需要保存的资源(接口)的名称,即`@SentinelResource`注解中配置的`value`属性值_
  2. > 相应的可设置项参考`sentinel-core`包下的`com.alibaba.csp.sentinel.slots.block.RuleConstant`类
  3. 3. 启动服务,在Sentinel控制台可以看到相应的配置
  4. ![](https://gitee.com/moonzero/images/raw/master/code-note/20201022103834045_8987.png)
  5. ### 8.2. RestTemplate 基于 Sentinel 实现熔断
  6. #### 8.2.1. 基础说明
  7. Spring Cloud Alibaba Sentinel 支持对 `RestTemplate` 的服务调用使用 Sentinel 进行保护,在构造`RestTemplate`对象的时候需要加上 `@SentinelRestTemplate` 注解即可

@Bean @SentinelRestTemplate(blockHandler = “handleException”, blockHandlerClass = ExceptionUtil.class) public RestTemplate restTemplate() { return new RestTemplate(); }

  1. `@SentinelRestTemplate`注解的属性支持限流(`blockHandler`,`blockHandlerClass`)和降级(`fallback`,`fallbackClass`)的处理。其中`blockHandler`或`fallback`属性对应的方法必须是对应`blockHandlerClass`或`fallbackClass`属性中的静态方法。<br />该方法的参数跟返回值跟`org.springframework.http.client.ClientHttpRequestInterceptor#interceptor`方法一致,其中参数多出了一个`BlockException`参数用于获取Sentinel捕获的异常。<br />比如上述 `@SentinelRestTemplate` 注解中 `ExceptionUtil` 的 `handleException` 属性对应的方法声明如下:

public class ExceptionUtil { public static ClientHttpResponse handleException(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException exception) { … } }

  1. #### 8.2.2. 使用示例
  2. 1. 修改`shop-service-order-resttemplate`工程的配置类`HttpConfig`,在创建`RestTemplate`对象方法上增加``@SentinelRestTemplate`注解

@LoadBalanced @Bean(“restTemplate”) /*

  • @SentinelRestTemplate注解表示使用Sentinel对象RestTemplate的支持
  • blockHandler属性:指定熔断时降级方法
  • blockHandlerClass属性:指定熔断降级配置类
  • fallback属性:指定异常时降级方法
  • fallbackClass属性:指定异常限级配置类 */ @SentinelRestTemplate(blockHandler = “handleBlock”, blockHandlerClass = ExceptionUtil.class,
    1. fallback = "handleFallback", fallbackClass = ExceptionUtil.class)
    public RestTemplate createRestTemplate() { return new RestTemplate(); }
  1. 2. 创建处理熔断降级的处理类`ExceptionUtil`,在此类中定义相应的熔断、异常降级方法

package com.moon.order.exception;

import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse; import com.alibaba.csp.sentinel.slots.block.BlockException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution;

/**

  • Sentinel 对 RestTemplate 支持的熔断降级处理类
  • 其中 blockHandler 或 fallback 属性对应的方法必须是对应 blockHandlerClass 或 fallbackClass 属性中的静态方法。
    1. 熔断与异常的处理方法都必须为static修饰的静态方法
    1. 方法的返回值必须为SentinelClientHttpResponse对象
    1. 方法的形参为 HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException ex
  • 即该方法的参数跟返回值跟 org.springframework.http.client.ClientHttpRequestInterceptor#interceptor 方法一致,
  • 其中参数多出了一个 BlockException 参数用于获取 Sentinel 捕获的异常。 / public class ExceptionUtil { / 日志对象 */ private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionUtil.class);

    // 熔断降级业务逻辑 public static SentinelClientHttpResponse handleBlock(HttpRequest request, byte[] body,

    1. ClientHttpRequestExecution execution, BlockException ex) {
    2. LOGGER.error("熔断降级业务逻辑:${}", ex.getClass().getCanonicalName());
    3. return new SentinelClientHttpResponse("handleBlock");

    }

    // 异常降级业务逻辑 public static SentinelClientHttpResponse handleFallback(HttpRequest request, byte[] body,

    1. ClientHttpRequestExecution execution, BlockException ex) {
    2. LOGGER.error("异常降级业务逻辑:${}", ex.getClass().getCanonicalName());
    3. return new SentinelClientHttpResponse("handleFallback");

    } }

  1. 3. 在Sentinel控制台中配置相应的接口限流规则。Sentinel RestTemplate 限流的资源规则提供两种粒度:
  2. - `httpmethod:schema://host:port/path`:协议、主机、端口和路径
  3. - `httpmethod:schema://host:port`:协议、主机和端口
  4. ![](https://gitee.com/moonzero/images/raw/master/code-note/20201022140326992_13863.png)<br />设置相应的“流控”模块,发送测试测试,观察后台日志是否实现熔断降级<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20201022141019674_1083.png)
  5. #### 8.2.3. [SentinelRestTemplate ](/SentinelRestTemplate ) 相关属性
  6. |
  7. 属性名
  8. | 作用
  9. | 取值
  10. |
  11. | --- | --- | --- |
  12. |
  13. `blockHandler`
  14. | 指定熔断时降级方法
  15. |
  16. |
  17. |
  18. `blockHandlerClass`
  19. | 指定熔断降级配置类
  20. | `Class<?>`
  21. |
  22. |
  23. `fallback`
  24. | 指定异常时降级方法
  25. |
  26. |
  27. |
  28. `fallbackClass`
  29. | 指定异常限级配置类
  30. | `Class<?>`
  31. |
  32. |
  33. `urlCleaner`
  34. |
  35. |
  36. |
  37. |
  38. `urlCleanerClass`
  39. |
  40. | `Class<?>`
  41. |
  42. ### 8.3. Feign 基于 Sentinel 实现熔断
  43. #### 8.3.1. 基础说明
  44. Sentinel 适配了`OpenFeign`组件。如果想使用,除了引入 `sentinel-starter` 的依赖外还需要2个步骤:
  45. - 配置文件打开sentinel对feign的支持:`feign.sentinel.enabled=true`
  46. - 加入 `openfeign starter` 依赖使 `sentinel starter` 中的自动化配置类生效:
  47. > 注:Feign 对应的接口中的资源名策略定义:`httpmethod:protocol://requesturl`。`@FeignClient` 注解中的所有属性,Sentinel 都做了兼容。
  48. > 下面示例的`ProductFeginClient`接口中方法 `findById` 对应的资源名为 `GET:http://shop-service-product/product/{str}`
  49. #### 8.3.2. 使用示例
  50. 1. 引入依赖`openfeign`与`sentinel`的依赖
org.springframework.cloud spring-cloud-starter-openfeign com.alibaba.cloud spring-cloud-starter-alibaba-sentinel
  1. 2. 在工程的`application.yml`中开启 sentinel 对 feign 的支持

开启 feign 对 sentinel 的支持

feign: sentinel: enabled: true # 激活sentinel的支持

  1. 3. 和使用 Hystrix 一样,编写熔断容错处理类。此类需要实现相应的 FeignClient 的接口。当 FeignClient 接口调用出错后,会调用到当前容错实现类中同名的方法

@Component public class ProductFeignClientCallBack implements ProductFeignClient { / 日志对象 / private static final Logger LOGGER = LoggerFactory.getLogger(ProductFeignClientCallBack.class);

  1. /**
  2. * 此方法为ProductFeignClient接口中相应方法的降级实现
  3. *
  4. * @param id
  5. * @return
  6. */
  7. @Override
  8. public Product findById(Long id) {
  9. LOGGER.info("当前下单商品的id是: " + id + ",触发ProductFeignClientCallBack类中熔断的findById降级方法");
  10. Product product = new Product();
  11. product.setProductName("feign调用触发熔断降级方法");
  12. return product;
  13. }

}

  1. 4. 和使用 Hystrix 的方式基本一致,配置 FeignClient 的接口,在接口标识`@FeignClient`注解,通过`name`属性指定服务名称以及通过`fallback`属性指定处理熔断降级实现类

@FeignClient(name = “shop-service-product”, fallback = ProductFeignClientCallBack.class) public interface ProductFeignClient { …. }

  1. 5. 测试,修改资源相应的降级规则,测试的结果与 Hystrix 一样
  2. ![](https://gitee.com/moonzero/images/raw/master/code-note/20201022152754030_3953.png)
  3. #### 8.3.3. 从容错类中获取具体的错误信息
  4. 上面章节的容错方式在出现异常时,不能获取到异常的信息。如果需要获取容错发生时的具体的异常信息。则需要实现 Feign 提供的 `feign.hystrix.FallbackFactory` 接口。具体实现步骤如下:
  5. - 创建容错处理类,实现 `FallbackFactory` 接口。此接口的泛型 `T` 为需要容错的 Feign 接口类型

/**

  • 商品服务 Feign 调用容错处理类。
  • 与直接实现 Feign 接口的方式不同的地点在于,在此接口的 create 方法,可以获取到容错时发生的异常的信息。
  • FallbackFactory<T> 的泛型为容错的 Feign 的接口 / @Slf4j @Service public class ProductFeignClientFallBackFactory implements FallbackFactory { /*
    • Returns an instance of the fallback appropriate for the given cause.
    • 此方法返回相应的 Feign 接口实现类。所以直接创建 Feign 接口的实现类,在重写里面所有方法,
    • 执行的效果与容错类直接实现 Feign 接口一样 *
    • @param cause 这就是 fegin 在调用过程中产生异常 */ @Override public ProductFeignClient create(Throwable cause) { return new ProductFeignClient() {
      1. @Override
      2. public Product findById(Long id) {
      3. // 此处就可以获取到异常发生的具体信息,做相应的处理和分析
      4. log.error("ProductFeignClientFallBackFactory 容错获取到的异常信息是:{}", cause);
      5. // 调用报错时的处理逻辑
      6. Product product = new Product();
      7. product.setId(Long.parseLong("-1"));
      8. product.setProductName("查询产品出错了");
      9. return product;
      10. }
      }; } }
  1. - 修改接口的 `@FeignClient` 注解,通过 `fallbackFactory` 属性指定相应的容错处理类。

@FeignClient(value = “service-product”, fallbackFactory = ProductFeignClientFallBackFactory.class) public interface ProductFeignClient { …. }

  1. - 测试效果
  2. **需要注意:`fallback` 和 `fallbackFactory` 只能使用其中一种方式**
  3. ### 8.4. 示例项目
  4. #### 8.4.1. spring-cloud-greenwich-sample 项目中的示例
  5. 参考`08-springcloud-hystrix-resttemplate`与`09-springcloud-hystrix-feign`工程,创建`10-springcloud-alibaba-sentinel`,删除hystrix组件部分内容,创建两个order服务,一个使用`RestTemplate`一个使用`Feign`<br />具体项目代码参考`spring-cloud-note\spring-cloud-greenwich-sample\10-springcloud-alibaba-sentinel`
  6. #### 8.4.2. spring-cloud-alibaba-2.1.x-sample 项目中示例
  7. 具体项目代码参考`spring-cloud-note\spring-cloud-alibaba-2.1.x-sample\service-order`
  8. # Alibaba Cloud SMS
  9. ## 1. 短信服务介绍
  10. 短信服务(Short Message Service)是阿里云为用户提供的一种通信服务的能力。
  11. > - 产品优势:覆盖全面、高并发处理、消息堆积处理、开发管理简单、智能监控调度
  12. > - 产品功能:短信通知、短信验证码、推广短信、异步通知、数据统计
  13. > - 应用场景:短信验证码、系统信息推送、推广短信等
  14. ## 2. 短信服务使用
  15. 阿里云短信服务的使用流程;<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220107154800250_25701.jpg)
  16. ### 2.1. 阿里云短信服务准备
  17. [短信服务官方说明文档](https://www.aliyun.com/product/sms)
  18. #### 2.1.1. 开通短信服务
  19. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220107160028242_16385.png)
  20. #### 2.1.2. 申请认证秘钥
  21. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220107161652865_14772.png)
  22. #### 2.1.3. 申请短信签名
  23. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220107162140571_2292.png)<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220107162150635_32669.png)
  24. #### 2.1.4. 申请短信模板
  25. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220107162220137_25109.png)<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220107162242052_19521.png)
  26. ### 2.2. 短信服务API介绍
  27. 短信服务提供的相关API接口及各API接口的用法
  28. - [API 概览官方文档](https://help.aliyun.com/document_detail/102715.html?spm=5176.25163407.help.dexternal.37a2bb6e0UlvAt)
  29. - [短信服务 API 概览](https://next.api.aliyun.com/product/Dysmsapi)
  30. #### 2.2.1. 短信发送(SendSms)
  31. - [短信发送(SendSms)](e37297da526c4920605d6239177ea9b3)
  32. #### 2.2.2. 短信查询(QuerySendDetails)
  33. - [短信查询(QuerySendDetails)](5ba34879735ba01e0d9a0f11122a6c66)
  34. ### 2.3. 短信发送基础功能测试
  35. - 引入阿里云服务依赖
com.alibaba.cloud spring-cloud-alicloud-sms
  1. - 使用阿里云提供的Demo测试短信发送

public class SmsDemoTest {

  1. //产品名称:云通信短信API产品,开发者无需替换
  2. static final String product = "Dysmsapi";
  3. //产品域名,开发者无需替换
  4. static final String domain = "dysmsapi.aliyuncs.com";
  5. // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
  6. static final String accessKeyId = "yourAccessKeyId";
  7. static final String accessKeySecret = "yourAccessKeySecret";
  8. //短信发送
  9. public static SendSmsResponse sendSms() throws ClientException, com.aliyuncs.exceptions.ClientException {
  10. //可自助调整超时时间
  11. System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
  12. System.setProperty("sun.net.client.defaultReadTimeout", "10000");
  13. //初始化acsClient,暂不支持region化
  14. IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
  15. DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
  16. IAcsClient acsClient = new DefaultAcsClient(profile);
  17. //组装请求对象-具体描述见控制台-文档部分内容
  18. SendSmsRequest request = new SendSmsRequest();
  19. //必填:待发送手机号
  20. request.setPhoneNumbers("15000000000");
  21. //必填:短信签名-可在短信控制台中找到
  22. request.setSignName("云通信");
  23. //必填:短信模板-可在短信控制台中找到
  24. request.setTemplateCode("SMS_1000000");
  25. //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
  26. request.setTemplateParam("{\"name\":\"Tom\", \"code\":\"123\"}");
  27. //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
  28. //request.setSmsUpExtendCode("90997");
  29. //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
  30. request.setOutId("yourOutId");
  31. //hint 此处可能会抛出异常,注意catch
  32. SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
  33. return sendSmsResponse;
  34. }
  35. //短信查询
  36. public static QuerySendDetailsResponse querySendDetails(String bizId) throws ClientException, com.aliyuncs.exceptions.ClientException {
  37. //可自助调整超时时间
  38. System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
  39. System.setProperty("sun.net.client.defaultReadTimeout", "10000");
  40. //初始化acsClient,暂不支持region化
  41. IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
  42. DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
  43. IAcsClient acsClient = new DefaultAcsClient(profile);
  44. //组装请求对象
  45. QuerySendDetailsRequest request = new QuerySendDetailsRequest();
  46. //必填-号码
  47. request.setPhoneNumber("15000000000");
  48. //可选-流水号
  49. request.setBizId(bizId);
  50. //必填-发送日期 支持30天内记录查询,格式yyyyMMdd
  51. SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
  52. request.setSendDate(ft.format(new Date()));
  53. //必填-页大小
  54. request.setPageSize(10L);
  55. //必填-当前页码从1开始计数
  56. request.setCurrentPage(1L);
  57. //hint 此处可能会抛出异常,注意catch
  58. QuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request);
  59. return querySendDetailsResponse;
  60. }
  61. public static void main(String[] args) throws ClientException, InterruptedException, com.aliyuncs.exceptions.ClientException {
  62. //发短信
  63. SendSmsResponse response = sendSms();
  64. System.out.println("短信接口返回的数据----------------");
  65. System.out.println("Code=" + response.getCode());
  66. System.out.println("Message=" + response.getMessage());
  67. System.out.println("RequestId=" + response.getRequestId());
  68. System.out.println("BizId=" + response.getBizId());
  69. Thread.sleep(3000L);
  70. //查明细
  71. if (response.getCode() != null && response.getCode().equals("OK")) {
  72. QuerySendDetailsResponse querySendDetailsResponse = querySendDetails(response.getBizId());
  73. System.out.println("短信明细查询接口返回数据----------------");
  74. System.out.println("Code=" + querySendDetailsResponse.getCode());
  75. System.out.println("Message=" + querySendDetailsResponse.getMessage());
  76. int i = 0;
  77. for (QuerySendDetailsResponse.SmsSendDetailDTO smsSendDetailDTO : querySendDetailsResponse.getSmsSendDetailDTOs()) {
  78. System.out.println("SmsSendDetailDTO[" + i + "]:");
  79. System.out.println("Content=" + smsSendDetailDTO.getContent());
  80. System.out.println("ErrCode=" + smsSendDetailDTO.getErrCode());
  81. System.out.println("OutId=" + smsSendDetailDTO.getOutId());
  82. System.out.println("PhoneNum=" + smsSendDetailDTO.getPhoneNum());
  83. System.out.println("ReceiveDate=" + smsSendDetailDTO.getReceiveDate());
  84. System.out.println("SendDate=" + smsSendDetailDTO.getSendDate());
  85. System.out.println("SendStatus=" + smsSendDetailDTO.getSendStatus());
  86. System.out.println("Template=" + smsSendDetailDTO.getTemplateCode());
  87. }
  88. System.out.println("TotalCount=" + querySendDetailsResponse.getTotalCount());
  89. System.out.println("RequestId=" + querySendDetailsResponse.getRequestId());
  90. }
  91. }

}

  1. ## 3. 消息中间件 RocketMQ 与短信服务综合案例
  2. 案例需求:模拟场景,下单成功之后,向下单用户发送短信。设计图如下:<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220106213121261_11258.png)
  3. ### 3.1. 订单微服务发送信息
  4. - 在订单微服务中添加 RocketMQ 的依赖
org.apache.rocketmq rocketmq-spring-boot-starter 2.1.1
  1. - 修改配置文件,添加 Nacos 相关配置

RocketMQ 配置

rocketmq: name-server: 127.0.0.1:9876 # RocketMQ 服务的地址 producer: group: producer-order # 生产者组名称

  1. - 修改

@Slf4j @RestController @RequestMapping(“order_sms”) public class OrderSmsController { @Autowired private ProductFeignClient productFeignClient;

  1. @Autowired
  2. private OrderService orderService;
  3. @Autowired
  4. private RocketMQTemplate rocketMQTemplate;
  5. /**
  6. * 下单请求方法,通过 Feign 实现负载均衡的服务调用
  7. *
  8. * @param pid
  9. * @return
  10. */
  11. @GetMapping("/createOrder/{pid}")
  12. public Order createOrderAndSendMessage(@PathVariable("pid") Long pid) {
  13. log.info("接收到ID为{}的商品下单请求", pid);
  14. /* 通过 FeignClient 接口调用本地方法的方式,实现服务的调用。 */
  15. Product product = productFeignClient.findById(pid);
  16. Order order = new Order();
  17. if (product != null) {
  18. order.setProductId(pid);
  19. order.setNumber(1);
  20. order.setPrice(product.getPrice());
  21. order.setAmount(product.getPrice());
  22. order.setProductName(product.getProductName());
  23. // 暂写死用户
  24. order.setUserId(Long.parseLong("1"));
  25. order.setUsername("夜神月");
  26. // 创建订单
  27. orderService.createOrder(order);
  28. /*
  29. * void convertAndSend(D destination, Object payload) throws MessagingException;
  30. * 发送异步消息到 RocketMQ
  31. * D destination:主题名称
  32. * Object payload:消息体
  33. */
  34. rocketMQTemplate.convertAndSend("alibaba-sample-sms-topic", order);
  35. }
  36. return order;
  37. }

}

  1. ### 3.2. 用户微服务接收信息
  2. - 用户微服务中添加相关的依赖,包括 nacos 客户端、RocketMQ 客户端、Alibaba Cloud SMS 等依赖
com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery org.apache.rocketmq rocketmq-spring-boot-starter 2.1.1 com.alibaba.cloud spring-cloud-alicloud-sms 2.1.1.RELEASE
  1. - 修改配置文件,添加 Nacos、RocketMQ 相关配置

spring: cloud: nacos: discovery: server-addr: 127.0.0.1:8848 # 配置 Nacos server 的地址

RocketMQ 配置

rocketmq: name-server: 127.0.0.1:9876 # RocketMQ 服务的地址

  1. - 将阿里云短信给出的demo封装成工具类(参考源代码)
  2. - 创建短信服务监听类,监听 RocketMQ 消息

@Slf4j @Service // @RocketMQMessageListener 注解用于配置消费者相关信息 @RocketMQMessageListener( consumerGroup = “consume-user”, // 消费者分组 topic = “alibaba-sample-sms-topic”, // 要消费的主题 consumeMode = ConsumeMode.CONCURRENTLY, // 消费模式:无序和有序,默认是无序 messageModel = MessageModel.CLUSTERING // 消息模式:广播和集群,默认是集群 ) public class SmsService implements RocketMQListener { @Autowired private UserDao userDao;

  1. @Override
  2. public void onMessage(Order message) {
  3. log.info("接收到了一个订单信息消息:{},准备发送短信通知", JSON.toJSONString(message));
  4. // 根据用户 id 查询用户信息
  5. User user = userDao.findById(message.getUserId()).get();
  6. log.info("根据用户 id 获取用户信息:{}", JSON.toJSONString(user));
  7. Map<String, String> params = new HashMap<>();
  8. // 设置验证码,简单写死
  9. params.put("code", "123456");
  10. try {
  11. // 发送短信的模拟 {"code":"123456"}
  12. SmsUtil.sendSms("13800000000", "短信信息", "SMS_170836451", JSON.toJSONString(params));
  13. log.info("短信发送成功");
  14. } catch (Exception e) {
  15. e.printStackTrace();
  16. }
  17. }

}

  1. # Seata
  2. ## 1. 概述
  3. Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220109080311518_31131.png)
  4. ### 1.1. 发展历史
  5. 2019 年 1 月,阿里巴巴中间件团队发起了开源项目 Fescar(Fast & EaSy Commit And Rollback),其愿景是让分布式事务的使用像本地事务的使用一样,简单和高效,并逐步解决开发者们遇到的分布式事务方面的所有难题。后来更名为 Seata,意为:Simple Extensible AutonomousTransaction Architecture,是一套分布式事务解决方案。<br />Seata 的设计目标是对业务无侵入,因此从业务无侵入的 2PC 方案着手,在传统 2PC 的基础上演进。它把一个分布式事务理解成一个包含了若干分支事务的全局事务。全局事务的职责是协调其下管辖的分支事务达成一致,要么一起成功提交,要么一起失败回滚。此外,通常分支事务本身就是一个关系数据库的本地事务。
  6. ### 1.2. Seata 三个重要组件
  7. - **TC (Transaction Coordinator) - 事务协调者**
  8. 维护全局和分支事务的状态,驱动全局事务提交或回滚。
  9. - **TM (Transaction Manager) - 事务管理器**
  10. 定义全局事务的范围:开始全局事务、提交或回滚全局事务。
  11. - **RM (Resource Manager) - 资源管理器**
  12. 管理分支事务处理的资源,与TC交谈以注册分支事务和报告分支事务的状态,并驱动分支事务提交或回滚。
  13. ### 1.3. Seata 的执行流程
  14. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220109123257491_7540.png)
  15. 1. A 服务的 TM 向 TC 申请开启一个全局事务,TC 就会创建一个全局事务并返回一个唯一的 XID
  16. 1. A 服务的 RM 向 TC 注册分支事务,并及其纳入 XID 对应全局事务的管辖
  17. 1. A 服务执行分支事务,向数据库做操作
  18. 1. A 服务开始远程调用 B 服务,此时 XID 会在微服务的调用链上传播
  19. 1. B 服务的 RM 向 TC 注册分支事务,并将其纳入 XID 对应的全局事务的管辖
  20. 1. B 服务执行分支事务,向数据库做操作
  21. 1. 全局事务调用链处理完毕,TM 根据有无异常向 TC 发起全局事务的提交或者回滚
  22. 1. TC 协调其管辖之下的所有分支事务,决定是否回滚
  23. ### 1.4. Seata 实现 2PC 与传统 2PC 的差别
  24. 1. 架构层次方面,传统 2PC 方案的 RM 实际上是在数据库层,RM 本质上就是数据库自身,通过 XA 协议实现,而 Seata 的 RM 是以 jar 包的形式作为中间件层部署在应用程序这一侧的。
  25. 1. 两阶段提交方面,传统 2PC 无论第二阶段的决议是 commit 还是 rollback,事务性资源的锁都要保持到 Phase2 完成才释放。而 Seata 的做法是在 Phase1 就将本地事务提交,这样就可以省去 Phase2 持锁的时间,整体提高效率。
  26. ## 2. Seata 快速开始
  27. 快速开始的示例通过Seata中间件实现分布式事务,模拟电商中的下单和扣库存的过程。案例通过订单微服务执行下单操作,然后由订单微服务调用商品微服务扣除库存<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220109082858197_17747.png)
  28. ### 2.1. 编写业务处理逻辑
  29. 此部分详见案例项目 `spring-cloud-note\spring-cloud-alibaba-2.1.x-sample\examples\spring-cloud-alibaba-seata-example\`
  30. ### 2.2. 启动 Seata 服务(0.9.0 版服务端)
  31. #### 2.2.1. 下载 Seata
  32. 官方下载地址:[https://github.com/seata/seata/releases](https://github.com/seata/seata/releases)<br />本次示例 Seata 版本为 0.9.0。
  33. > demo中使用的相关版本号,具体请看代码。如果搭建个人demo不成功,验证是否是由版本导致,由于目前这几个项目更新比较频繁,版本稍有变化便会出现许多奇怪问题
  34. #### 2.2.2. 修改 Seata 配置
  35. 将Seata压缩包进行解压,进入conf目录,修改以下的配置文件:
  36. - registry.conf。该配置用于指定 TC 的注册中心和配置文件,默认都是 `file`;如果使用其他的注册中心,要求 Seata-Server 也注册到该配置中心上。_此示例使用了 nacos,修改type值,将其他不需要的内容删除。_

registry {

file 、nacos 、eureka、redis、zk、consul、etcd3、sofa

type = “nacos”

nacos { serverAddr = “127.0.0.1:8848” namespace = “public” cluster = “default” } }

config {

file、nacos 、apollo、zk、consul、etcd3

type = “nacos”

nacos { serverAddr = “127.0.0.1:8848” namespace = “public” cluster = “default” } }

  1. - nacos-config.txt 用于定义导入到 nocao 配置中心的相关内容。这里需要增加本次示例服务的服务名称。
  2. - 这里的语法为:`service.vgroup_mapping.${your-service-gruop}=default`,中间的 `${your-service-gruop}` 为自定义的服务组名称,这里需要在程序的配置文件中配置。

service.vgroup_mapping.product-service=default service.vgroup_mapping.order-service=default

  1. #### 2.2.3. 初始化 seata 相关配置至 nacos
  2. 确保正常运行 nacos 服务后,进行 seata 的 config 目录,运行以下命令:

nacos-config.sh 127.0.0.1

  1. ![](https://gitee.com/moonzero/images/raw/master/code-note/20220109214120078_31887.png)<br />执行成功后可以打开 Nacos 的控制台,在配置列表中,可以看到初始化了很多 Group 为 SEATA_GROUP 的配置。也可以找到 nacos-config.txt 增加的服务名称映射。<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220109214316908_11520.png)
  2. #### 2.2.4. 启动 Seata 服务
  3. 进行 Seata 的 bin 目录。(windows 运行 .bat 脚本,linux 运行 .sh 的脚本)

cd /d E:\deployment-environment\seata\bin\ seata-server.bat -p 9000 -m file

  1. 启动成功后,在 Nacos 的服务列表下面可以看到一个名为 serverAddr 的服务。<br />![](https://gitee.com/moonzero/images/raw/master/code-note/20220109214919700_31871.png)
  2. ### 2.3. 微服务使用 Seata 实现事务控制(0.7.1 版客户端)
  3. #### 2.3.1. 添加依赖
  4. 在需要进行分布式控制的微服务中(_示例是订单微服务、商品微服务_),添加 Spring Cloud Alibaba 依赖管理工具和 Seata 依赖<br />**特别注意有坑:这个 Spring Cloud Alibaba 的版本是 2.1.0.RELEASE!!!如果使用更高的版本,io.seata:seata-all 的版本就不是0.7.1,以下示例相关配置将不会生效!!!**
com.alibaba.cloud spring-cloud-alibaba-dependencies 2.1.0.RELEASE pom import com.alibaba.cloud spring-cloud-starter-alibaba-seata
  1. 需要注意的是 Spring Cloud Alibaba 的毕业版本的 GroupId 是 `com.alibaba.cloud`。`spring-cloud-starter-alibaba-seata` 这个依赖中只依赖了 `spring-cloud-alibaba-seata`,所以在项目中添加 `spring-cloud-starter-alibaba-seata` 和 `spring-cloud-alibaba-seata` 是一样的
  2. > 此示例是使用nacos作为注册配置中心,需要再引入 `spring-cloud-starter-alibaba-nacos-config` 相关的依赖
  3. #### 2.3.2. 初始化 undo_log 表
  4. 在微服务业务相关的数据库中添加 undo_log 表,这是Seata记录事务日志要用到的表,用于保存需要回滚的数据

CREATE TABLE undo_log ( id BIGINT ( 20 ) NOT NULL AUTO_INCREMENT, branch_id BIGINT ( 20 ) NOT NULL, xid VARCHAR ( 100 ) NOT NULL, context VARCHAR ( 128 ) NOT NULL, rollback_info LONGBLOB NOT NULL, log_status INT ( 11 ) NOT NULL, log_created DATETIME NOT NULL, log_modified DATETIME NOT NULL, ext VARCHAR ( 100 ) DEFAULT NULL, PRIMARY KEY ( id ), UNIQUE KEY ux_undo_log ( xid, branch_id ) ) ENGINE = INNODB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;

  1. #### 2.3.3. 注入数据源
  2. Seata 是通过代理数据源实现事务分支的,所以需要配置 `io.seata.rm.datasource.DataSourceProxy` 的 Bean,且是 `@Primary` 默认的数据源,否则事务不会回滚,无法实现分布式事务
  3. > 值得注意:MyBatis 和 JPA 都需要注入 `io.seata.rm.datasource.DataSourceProxy`, 不同的是,MyBatis 还需要额外注入 `org.apache.ibatis.session.SqlSessionFactory`
  4. - JPA 配置

@Configuration public class DataSourceProxyConfig { @Bean @ConfigurationProperties(prefix = “spring.datasource”) public DruidDataSource druidDataSource() { return new DruidDataSource(); }

  1. @Primary
  2. @Bean
  3. public DataSourceProxy dataSource(DruidDataSource druidDataSource) {
  4. return new DataSourceProxy(druidDataSource);
  5. }

}

  1. - MyBatis 配置

@Configuration public class DataSourceProxyConfig { @Bean @ConfigurationProperties(prefix = “spring.datasource”) public DataSource dataSource() { return new DruidDataSource(); }

  1. @Bean
  2. public DataSourceProxy dataSourceProxy(DataSource dataSource) {
  3. return new DataSourceProxy(dataSource);
  4. }
  5. @Bean
  6. public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
  7. SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
  8. sqlSessionFactoryBean.setDataSource(dataSourceProxy);
  9. return sqlSessionFactoryBean.getObject();
  10. }

}

  1. #### 2.3.4. 修改微服务项目配置文件
  2. - 修改项目配置文件,创建 `bootstrap.yaml`/`bootstrap.properties` 文件,增加 nacos 配置中心与 seata 的配置

spring: cloud: nacos: config: server-addr: 127.0.0.1:8848 # nacos 的服务端地址 group: SEATA_GROUP # 默认值:DEFAULT_GROUP namespace: public # 配置的命名空间。常用场景之一是不同环境的配置的区分隔离,例如开发测试环境和生产环境的资源隔离等。

  1. # 配置seata
  2. alibaba:
  3. seata:
  4. tx-service-group: ${spring.application.name}
  1. - 在 resources 目录下添加 Seata 的配置文件 registry.conf
  2. #### 2.3.5. 在微服务开启全局事务
  3. Seata 实现全局事务,只需要在业务的发起方的方法上使用 `@GlobalTransactional` 注解,即可开启全局事务,Seata 会将事务的 xid 通过拦截器添加到调用其他服务的请求中,实现分布式事务。

@Override @GlobalTransactional public Order createOrder(Long pid) { …. }

```

3. Seata 运行流程分析

03-Spring-Cloud-Alibaba - 图1
要点说明:

  1. 每个 RM 使用 DataSourceProxy 连接数据库,其目的是使用 ConnectionProxy,使用数据源和数据连接代理的目的就是在第一阶段将 undo_log 和业务数据放在一个本地事务提交,这样就保存了只要有业务操作就一定有 undo_log。
  2. 在第一阶段 undo_log 中存放了数据修改前和修改后的值,为事务回滚作好准备,所以第一阶段完成就已经将分支事务提交,也就释放了锁资源。
  3. TM 开启全局事务开始,将 XID 全局事务 id 放在事务上下文中,通过 feign 调用也将 XID 传入下游分支事务,每个分支事务将自己的 Branch ID 分支事务 ID 与 XID 关联。
  4. 第二阶段全局事务提交,TC 会通知各各分支参与者提交分支事务,在第一阶段就已经提交了分支事务,这里各各参与者只需要删除 undo_log 即可,并且可以异步执行,第二阶段很快可以完成。
  5. 第二阶段全局事务回滚,TC 会通知各各分支参与者回滚分支事务,通过 XID 和 Branch ID 找到相应的回滚日志,通过回滚日志生成反向的 SQL 并执行,以完成分支事务回滚到之前的状态,如果回滚失败则会重试回滚操作。