Sentinel 是面向分布式、多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性。
Springboot+Dubbo+Nacos 集成 Sentinel(入门)-CSDN博客
1.热点规则介绍
热点参数限流会统计传入参数中的热点参数,并根据配置的限流阈值与模式,对包含热点参数的资源调用进行限流。热点参数限流可以看做是一种特殊的流量控制,仅对包含热点参数的资源调用生效。

Sentinel 利用 LRU 策略统计最近最常访问的热点参数,结合令牌桶算法来进行参数级别的流控。
热点参数规则(ParamFlowRule
)类似于流量控制规则(FlowRule
):
属性 | 说明 | 默认值 |
---|---|---|
resource | 资源名,必填 | |
count | 限流阈值,必填 | |
grade | 限流模式 | QPS 模式 |
durationInSec | 统计窗口时间长度(单位为秒),1.6.0 版本开始支持 | 1s |
controlBehavior | 流控效果(支持快速失败和匀速排队模式),1.6.0 版本开始支持 | 快速失败 |
maxQueueingTimeMs | 最大排队等待时长(仅在匀速排队模式生效),1.6.0 版本开始支持 | 0ms |
paramIdx | 热点参数的索引,必填,对应 SphU.entry(xxx, args) 中的参数索引位置 |
|
paramFlowItemList | 参数例外项,可以针对指定的参数值单独设置限流阈值,不受前面 count 阈值的限制。仅支持基本类型和字符串类型) |
|
clusterMode | 是否是集群参数流控规则 | false |
clusterConfig | 集群流控相关配置 |

2.资源名(resource)
资源名,即限流规则的作用对象(可以是代码中指定名称或者指定路由)。

3.限流模式(grade)
默认QPS 模式。
4.参数索引(paramIdx)
热点参数的索引,必填,对应 SphU.entry(xxx, args) 中的参数索引位置。

5.限流阈值(count)

6.统计窗口时长(durationInSec)
统计窗口时间长度(单位为秒),1.6.0 版本开始支持。非集群模式。

7.流控效果(controlBehavior)
流控效果(支持快速失败和匀速排队模式),1.6.0 版本开始支持 ,默认快速失败。
7.1 最大排队等待时长(maxQueueingTimeMs)
最大排队等待时长(仅在匀速排队模式生效),1.6.0 版本开始支持。
8.是否是集群(clusterMode)
是否是集群参数流控规则。

8.1 集群阈值模式 (clusterConfig)
9.参数例外项(paramFlowItemList)
参数例外项,可以针对指定的参数值单独设置限流阈值,不受前面 count 阈值的限制。仅支持基本类型和字符串类型)。

10. 测试示例
10.1 客户端代码
java
@GetMapping(value = "/api/user/{id}")
@SentinelResource(value = "u-getUserById")
public String getUserById(@PathVariable("id") Long id) {
return userService.getName();
}
10.2 控制台-简单配置

10.3 控制台-例外配置
特别注意:例外参数类型要与客户端参数一致否则类外配置失效 !!!

11.规则持久化
持久化Json数据格式
java
[
{
"resource": "u-getUserById",
"grade": 1,
"paramIdx": 0,
"count": 1,
"controlBehavior": 0,
"maxQueueingTimeMs": 0,
"burstCount": 0,
"durationInSec": 1,
"paramFlowItemList": [
{
"object": "0",
"count": 20,
"classType": "long"
}
],
"clusterMode": false,
"clusterConfig": {
"flowId": null,
"thresholdType": 0,
"fallbackToLocalWhenFail": true,
"sampleCount": 10,
"windowIntervalMs": 1000
}
}
]
spring:
application:
name: user
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
prefix: dubbo-user-api
file-extension: yml
sentinel:
transport:
dashboard: 127.0.0.1:6780
eager: true # 取消懒加载
datasource:
ds-user-paramFlow-rule:
nacos:
server-addr: 127.0.0.1:8848
dataId: user-paramFlow-rule
groupId: user-sentinel
namespace: sentinel
rule-type: PARAM_FLOW # AUTHORITY(授权规则) DEGRADE(熔断规则) FLOW(流控规则) GW_API_GROUP GW_FLOW PARAM_FLOW(热点规则) SYSTEM(系统规则)