文章目录
- [Sentinel 流控规则 · 流控模式](#Sentinel 流控规则 · 流控模式)
-
- 一、依赖与接入
-
- [1.1 pom.xml](#1.1 pom.xml)
- [1.2 application-dev.yml](#1.2 application-dev.yml)
- [1.3 启动 Dashboard](#1.3 启动 Dashboard)
- 二、流控模式是什么
- 三、控制台参数字段(完整)
- 四、代码案例(order-service)
- 五、三种模式详解
-
- [5.1 直接(strategy=0)](#5.1 直接(strategy=0))
- [5.2 链路(strategy=2)](#5.2 链路(strategy=2))
- [5.3 关联(strategy=1)](#5.3 关联(strategy=1))
- [六、JSON 规则片段](#六、JSON 规则片段)
- 参考官方文档
Sentinel 流控规则 · 流控模式
基于
spring-cloud-demo· order-service · Boot 4.1.0 + Cloud 2025.1.2 + Alibaba 2025.1.0.0
一、依赖与接入
1.1 pom.xml
xml
<!-- Sentinel 客户端:流控、熔断、热点等 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
1.2 application-dev.yml
yaml
spring:
cloud:
sentinel:
web-context-unify: false # 按 URL 细分 Web 资源,便于链路模式选入口
transport:
dashboard: 127.0.0.1:8100 # Sentinel 控制台地址
feign:
sentinel:
enabled: true # Feign 调用纳入 Sentinel 资源统计
1.3 启动 Dashboard
这里的指令是8100作为控制台端口号
账号:admin
密码:admin
bash
java -Dserver.port=8100 -Dsentinel.dashboard.auth.username=admin -Dsentinel.dashboard.auth.password=admin -Dserver.servlet.session.timeout=1440m -jar sentinel-dashboard-1.8.10.jar
二、流控模式是什么
流控模式(strategy)决定 按什么维度统计流量 并触发限流。
| 模式 | strategy | 统计方式 |
|---|---|---|
| 直接 | 0 | 资源全局计数 |
| 关联 | 1 | 当关联资源超阈值时,限当前资源 |
| 链路 | 2 | 入口处统计 |

三、控制台参数字段(完整)
| 控制台字段 | JSON 字段 | 取值 | 说明 |
|---|---|---|---|
| 资源名 | resource | 字符串 | 与簇点链路完全一致,如 /order/write |
| 针对来源 | limitApp | default / 来源名 | default 表示不区分调用方 |
| 阈值类型 | grade | 0 / 1 | 0=并发线程数,1=QPS |
| 单机阈值 | count | 数字 | 阈值上限 |
| 流控模式 | strategy | 0 / 1 / 2 | 0直接,1关联,2链路 |
| 关联资源 / 入口资源 | refResource | 字符串 | 关联或链路模式必填 |
| 流控效果 | controlBehavior | 0 / 1 / 2 | 见流控效果篇 |
| 是否集群 | clusterMode | true / false | 单机演示一般 false |
四、代码案例(order-service)
java
@RequestMapping("/order") // 类级路径前缀
@RestController
public class OrderController {
@RequestMapping("read") // Web 资源:/order/read
public String read() {
return orderService.print(); // 内部再进入 order-service-print
}
@RequestMapping("write") // Web 资源:/order/write
public String write() {
return orderService.print(); // 与 read 共用 print()
}
}
java
@SentinelResource(value = "order-service-print", blockHandler = "handleBlock") // 手动资源名
public String print() {
return "order-service is running"; // 业务逻辑
}
调用关系:

五、三种模式详解
5.1 直接(strategy=0)
对 order-service-print 设 QPS=5:
- read、write 共用 5 QPS
- 不区分从哪个 HTTP 入口进来

5.2 链路(strategy=2)
对 order-service-print 设 QPS=50,入口资源 选 /order/write:
- 仅统计「从 write 进来再调 print」的流量
/order/read不受此规则限制


5.3 关联(strategy=1)
例:当 /order/write 的 QPS 超阈值时,才限制 order-service-print。
- 用于「写接口流量大时,保护下游共用逻辑」
refResource填被关联的资源名

六、JSON 规则片段
持久化配置见 sentinel-规则持久化.md。
直接模式示例:
json
[
{
"resource": "order-service-print",
"limitApp": "default",
"grade": 1,
"count": 5,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]
链路模式示例:
json
[
{
"resource": "order-service-print",
"limitApp": "default",
"grade": 1,
"count": 5,
"strategy": 2,
"refResource": "/order/write",
"controlBehavior": 0,
"clusterMode": false
}
]
| JSON 字段 | 说明 |
|---|---|
| resource | 被限流的资源名 |
| strategy | 0直接 / 2链路 |
| refResource | 链路模式下入口资源名 |
| grade | 1=QPS |
| count | 阈值 |