一、背景
我的项目需要引入限流,降级,熔断框架,由于 Spring Cloud 2022.0.4 已经不再支持 Hystrix,Spring Cloud 提供了替代方案,如 Resilience4j,可以使用它来替换 Hystrix。但是网上搜了一下国内Resilience4j使用的人很少,相反Alibaba的Sentinel使用的人非常多,并且也一直在维护,最新的版本v1.8.7 是3周前发布的


二、Sentinel 介绍
Sentinel 的使用可以分为两个部分:
- 核心库(Java 客户端):不依赖任何框架/库,能够运行于 Java 8 及以上的版本的运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持(见 主流框架适配)。
- 控制台(Dashboard):Dashboard 主要负责管理推送规则、监控、管理机器信息等。
三、安装Dashboard
1. 下载 Dashboard
地址:https://github.com/alibaba/Sentinel/releases
当前最新的版本是
2. 点击下载

3. 上传到服务器
启动程序,我这里把启动端口改成了8090,因为8080是自己写程序的默认端口,看着挺奇怪的
bash
java -Dserver.port=8090 -Dcsp.sentinel.dashboard.server=localhost:8090 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
如果需要设置开机自起,可以参考我的另一篇文章:Centos7 如何设置开机启动某个程序-CSDN博客
4. 访问面板
这里的IP改成自己机器IP
默认用户名和密码都是 sentinel

四、SpringBoot 3.1.7 应用程序如何集成
1. 添加依赖包
XML
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- 添加依赖管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2022.0.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
这里一定一定要注意版本一致性问题,如果版本引用错了,服务启动时不会加载sentinel
我的SpringBoot3.1.7 对应的Alibaba.cloud 版本号是2022.0.0.0
2. 添加配置信息
Lua
spring:
cloud:
sentinel:
transport:
dashboard: 192.168.31.110:8090
port: 8719
这里的dashboard 配的与sentinel.dashboard 服务的地址
port 则是当前服务的agent 端口,sentinel 会在你的应用中启动另一个特殊端口与sentinel.dashboard通信
3. 启动测试sentinel
启动我们的SpringBoot服务
随便用个接口请求一下自己SpringBoot服务

如果能看到下面这几行日志,说明本地的sentinel agent 启动成功了

4. 查看sentinel 面板
顺利的话已经有监控数据了

我在操作的时候刚开始,sentinel.dashboard部署在本机Window就正常通信,一旦我部署到虚拟机的Centos上就通信失败,后来找到原因是因为sentinel.dashboard需要反过来跟sentinel agent 通信,所以也就是说,双方的网络都必须是互通的,本来我们网络是互通的,但犹豫我Window电脑有3个网卡,注册的时候系统随机选了一个网卡的IP地址,导致Centos无法访问我的Window上的Agent应用解决方案:SpringBoot 服务注册IP选择问题-CSDN博客

五、配置限流规则并验证
访问sentinel.dashboard,
1. 单击 流控规则 ,然后点击 "新增流控规则"

2. 填写需要限流的 URL 相对路径
单机阈值选择需要限流的阈值,点击新增进行确认。(为了便于演示效果,这里将值设置成了 1)。

3. 快速点击postman请求,此时我们发现已经返回了限流

六、给sentinel配置数据源
1. 介绍
如果不配置数据源,当应用重启后,限流等配置信息都会丢失,需要再次重新配置
因为我选用的Consul作为我的注册和配置中心,那么我就选用Consul作为sentinel的数据源。
如果不是用Consul作为我的注册和配置中心可以跳过这一步
参考文档:
官方介绍说: Sentinel starter 整合了目前存在的几类 ReadableDataSource。只需要在配置文件中进行相关配置,即可在 Spring 容器中自动注册 DataSource。
但是很遗憾: 目前支持file
, nacos
, zk
, apollo
,redis
这5种类型。
于是我找到了sentinel-datasource-consul 的git地址:
https://github.com/alibaba/Sentinel/tree/1.8.6/sentinel-extension/sentinel-datasource-consul
难过的是官网就这么点文档(内心:虽然我写代码也经常不喜欢写文档,但是你好歹写一下配置文档啊):

2. 添加依赖
XML
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-consul</artifactId>
<version>1.8.6</version>
</dependency>
3. 查看配置过程
无奈,我只能翻翻源码,看看怎么配置,找到这个类
com.alibaba.cloud.sentinel.datasource.config.ConsulDataSourceProperties

看来要配置这几个类

结果启动包这个错误
java.lang.NullPointerException: Cannot invoke "com.alibaba.cloud.sentinel.datasource.RuleType.getName()" because the return value of "com.alibaba.cloud.sentinel.datasource.config.AbstractDataSourceProperties.getRuleType()" is null
于是我找到对应的RuleType,Flow 表示限流类型

终于不报错误了
4. 最终配置如下
Lua
spring:
cloud:
sentinel:
transport:
dashboard: 192.168.31.110:8090
port: 8719
datasource:
ds1:
consul:
host: 192.168.31.110
port: 8500
ruleKey: sentinel_flow_rule
watchTimeout: 5
ruleType: flow
5. 配置限流规则

然后重启服务,规则仍然没有

查找资料才知道:
在Sentinel Dashboard上修改流控规则后并不会同步到Nacos,目前Sentinel Dashboard不支持该功能。
(希望未来能支持吧)
那么只能我们手动配置
6. 限流配置文档参考文档
7. 限流配置步骤
登录 Consul平台
http://192.168.31.110:8500/ui/dc1/kv
点击create按钮

Lua
[
{
"resource": "/submitOrder",
"count": 5
}
]
再次打开Sentinel 控制面板的流控规则,可以看到Consul配置的规则已经加载进去了

8. 在线更新Consul配置值
不需要重启应用,实时同步到Sentinel

七、限流后的压力测试
1. 准备工作
上次测试单机最高吞吐量为110/s
这次我们限流 50 QPS,用100个线程去压测5分钟,看看程序能抗QPS为多少?(忽略出现限流错误)


2. 压测结果
从下图中我们可以看到压测的最高的QPS 已经达到了上千,程序依旧稳定运行,并没有崩溃,并且每秒有50QPS仍然正常给用户提供服务,说明sentinel 给我们程序提供了很好的保护作用


对比不加sentinel 的100线程压测结果,吞吐量 115 -> 741 (提升了很多)

3. 提示压测线程数300结果
无限流

限流50

加了sentinel 限流吞吐量增加了10倍,关键是平均时延迟从原来的2885ms降到了之前的1/10 正常耗时范围200多ms,也就是说,请求的用户量越多,sentinel 限流的作用越明显!!!