Sentinel 流控规则 · 流控模式

文章目录

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 阈值

参考官方文档

相关推荐
YOU OU9 天前
Alibaba Sentinel
sentinel
流烟默9 天前
Sentinel 规则持久化到 Nacos 实践(网关篇 + 普通应用篇)
nacos·sentinel·规则持久化
莫得感情 o11 天前
Redis 09 · 高可用:Sentinel 如何发现故障并完成切主
redis·缓存·sentinel
凌云若寒12 天前
CODESOFT试用流程
linux·运维·网络·数据库·sentinel
多敲代码防脱发18 天前
Alibaba Sentinel(熔断、限流)(国内下载Sentinel)
java·开发语言·sentinel
2601_9621819621 天前
【Spring Cloud Alibaba】Sentinel 服务熔断与流量控制
sentinel
lv__pf23 天前
Sentinel【TL微服务10、11】
java·微服务·sentinel
凤山老林23 天前
高可靠 API 网关架构:Spring Cloud Gateway 集成 Sentinel 实现智能限流、动态路由与安全管控
安全·spring cloud·架构·sentinel
就叫_这个吧25 天前
java springcloud熔断降级组件sentinel基础应用及nacos持久化处理
java·spring cloud·nacos·sentinel
aikopen25 天前
高可用 API 网关限流与微服务过载保护实战:从 Sentinel 动态阈值到 DB 连接池舱壁
数据库·微服务·sentinel