news 2026/4/15 15:12:12

Sentinel 网关流控实现原理

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Sentinel 网关流控实现原理

网关流量控制

Sentinel 支持对 Spring Cloud Gateway、Zuul 等主流的 API Gateway 进行限流。

Sentinel 1.6.0 引入了 Sentinel API Gateway Adapter Common 模块,此模块中包含网关限流的规则和自定义 API 的实体和管理逻辑:

  • GatewayFlowRule:网关限流规则,针对 API Gateway 的场景定制的限流规则,可以针对不同 route 或自定义的 API 分组进行限流,支持针对请求中的参数、Header、来源 IP 等进行定制化的限流。
  • ApiDefinition:用户自定义的 API 定义分组,可以看做是一些 URL 匹配的组合。比如我们可以定义一个 API 叫my_api,请求 path 模式为/foo/**/baz/**的都归到my_api这个 API 分组下面。限流的时候可以针对这个自定义的 API 分组维度进行限流。

其中网关限流规则GatewayFlowRule的字段解释如下:

  • resource:资源名称,可以是网关中的 route 名称或者用户自定义的 API 分组名称。

  • resourceMode:规则是针对 API Gateway 的 route(RESOURCE_MODE_ROUTE_ID)还是用户在 Sentinel 中定义的 API 分组(RESOURCE_MODE_CUSTOM_API_NAME),默认是 route。

  • grade:限流指标维度,同限流规则的grade字段。

  • count:限流阈值

  • intervalSec:统计时间窗口,单位是秒,默认是 1 秒。

  • controlBehavior:流量整形的控制效果,同限流规则的controlBehavior字段,目前支持快速失败和匀速排队两种模式,默认是快速失败。

  • burst:应对突发请求时额外允许的请求数目。

  • maxQueueingTimeoutMs:匀速排队模式下的最长排队时间,单位是毫秒,仅在匀速排队模式下生效。

  • paramItem

    :参数限流配置。若不提供,则代表不针对参数进行限流,该网关规则将会被转换成普通流控规则;否则会转换成热点规则。其中的字段:

    • parseStrategy:从请求中提取参数的策略,目前支持提取来源 IP(PARAM_PARSE_STRATEGY_CLIENT_IP)、Host(PARAM_PARSE_STRATEGY_HOST)、任意 Header(PARAM_PARSE_STRATEGY_HEADER)和任意 URL 参数(PARAM_PARSE_STRATEGY_URL_PARAM)四种模式。
    • fieldName:若提取策略选择 Header 模式或 URL 参数模式,则需要指定对应的 header 名称或 URL 参数名称。
    • pattern:参数值的匹配模式,只有匹配该模式的请求属性值会纳入统计和流控;若为空则统计该请求属性的所有值。(1.6.2 版本开始支持)
    • matchStrategy:参数值的匹配策略,目前支持精确匹配(PARAM_MATCH_STRATEGY_EXACT)、子串匹配(PARAM_MATCH_STRATEGY_CONTAINS)和正则匹配(PARAM_MATCH_STRATEGY_REGEX)。(1.6.2 版本开始支持)

用户可以通过GatewayRuleManager.loadRules(rules)手动加载网关规则,或通过GatewayRuleManager.register2Property(property)注册动态规则源动态推送(推荐方式)。

Spring Cloud Gateway

从 1.6.0 版本开始,Sentinel 提供了 Spring Cloud Gateway 的适配模块,可以提供两种资源维度的限流:

  • route 维度:即在 Spring 配置文件中配置的路由条目,资源名为对应的 routeId
  • 自定义 API 维度:用户可以利用 Sentinel 提供的 API 来自定义一些 API 分组

使用时需引入以下模块(以 Maven 为例):

<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId> <version>x.y.z</version> </dependency>

使用时只需注入对应的SentinelGatewayFilter实例以及SentinelGatewayBlockExceptionHandler实例即可。比如:

@Configuration public class GatewayConfiguration { private final List<ViewResolver> viewResolvers; private final ServerCodecConfigurer serverCodecConfigurer; public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider, ServerCodecConfigurer serverCodecConfigurer) { this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList); this.serverCodecConfigurer = serverCodecConfigurer; } @Bean @Order(Ordered.HIGHEST_PRECEDENCE) public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() { // Register the block exception handler for Spring Cloud Gateway. return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer); } @Bean @Order(-1) public GlobalFilter sentinelGatewayFilter() { return new SentinelGatewayFilter(); } }

Demo 示例:sentinel-demo-spring-cloud-gateway

比如我们在 Spring Cloud Gateway 中配置了以下路由:

server: port: 8090 spring: application: name: spring-cloud-gateway cloud: gateway: enabled: true discovery: locator: lower-case-service-id: true routes: # Add your routes here. - id: product_route uri: lb://product predicates: - Path=/product/** - id: httpbin_route uri: https://httpbin.org predicates: - Path=/httpbin/** filters: - RewritePath=/httpbin/(?<segment>.*), /$\{segment}

同时自定义了一些 API 分组:

private void initCustomizedApis() { Set<ApiDefinition> definitions = new HashSet<>(); ApiDefinition api1 = new ApiDefinition("some_customized_api") .setPredicateItems(new HashSet<ApiPredicateItem>() {{ add(new ApiPathPredicateItem().setPattern("/product/baz")); add(new ApiPathPredicateItem().setPattern("/product/foo/**") .setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_PREFIX)); }}); ApiDefinition api2 = new ApiDefinition("another_customized_api") .setPredicateItems(new HashSet<ApiPredicateItem>() {{ add(new ApiPathPredicateItem().setPattern("/ahas")); }}); definitions.add(api1); definitions.add(api2); GatewayApiDefinitionManager.loadApiDefinitions(definitions); }

那么这里面的 route ID(如product_route)和 API name(如some_customized_api)都会被标识为 Sentinel 的资源。比如访问网关的 URL 为http://localhost:8090/product/foo/22的时候,对应的统计会加到product_routesome_customized_api这两个资源上面,而http://localhost:8090/httpbin/json只会对应到httpbin_route资源上面。

注意:有的时候 Spring Cloud Gateway 会自己在 route 名称前面拼一个前缀,如ReactiveCompositeDiscoveryClient_xxx这种。请观察簇点链路页面实际的资源名。

您可以在GatewayCallbackManager注册回调进行定制:

  • setBlockHandler:注册函数用于实现自定义的逻辑处理被限流的请求,对应接口为BlockRequestHandler。默认实现为DefaultBlockRequestHandler,当被限流时会返回类似于下面的错误信息:Blocked by Sentinel: FlowException

注意

  • Sentinel 网关流控默认的粒度是 route 维度以及自定义 API 分组维度,默认不支持 URL 粒度。若通过 Spring Cloud Alibaba 接入,请将spring.cloud.sentinel.filter.enabled配置项置为 false(若在网关流控控制台上看到了 URL 资源,就是此配置项没有置为 false)。
  • 若使用 Spring Cloud Alibaba Sentinel 数据源模块,需要注意网关流控规则数据源类型是gw-flow,若将网关流控规则数据源指定为 flow 则不生效。

Zuul 1.x

Sentinel 提供了 Zuul 1.x 的适配模块,可以为 Zuul Gateway 提供两种资源维度的限流:

  • route 维度:即在 Spring 配置文件中配置的路由条目,资源名为对应的 route ID(对应RequestContext中的proxy字段)
  • 自定义 API 维度:用户可以利用 Sentinel 提供的 API 来自定义一些 API 分组

使用时需引入以下模块(以 Maven 为例):

<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-zuul-adapter</artifactId> <version>x.y.z</version> </dependency>

若使用的是 Spring Cloud Netflix Zuul,我们可以直接在配置类中将三个 filter 注入到 Spring 环境中即可:

@Configuration public class ZuulConfig { @Bean public ZuulFilter sentinelZuulPreFilter() { // We can also provider the filter order in the constructor. return new SentinelZuulPreFilter(); } @Bean public ZuulFilter sentinelZuulPostFilter() { return new SentinelZuulPostFilter(); } @Bean public ZuulFilter sentinelZuulErrorFilter() { return new SentinelZuulErrorFilter(); } }

Sentinel Zuul Adapter 生成的调用链路类似于下面,其中的资源名都是 route ID 或者自定义的 API 分组名称:

-EntranceNode: sentinel_gateway_context$$route$$another-route-b(t:0 pq:0.0 bq:0.0 tq:0.0 rt:0.0 prq:0.0 1mp:8 1mb:1 1mt:9) --another-route-b(t:0 pq:0.0 bq:0.0 tq:0.0 rt:0.0 prq:0.0 1mp:4 1mb:1 1mt:5) --another_customized_api(t:0 pq:0.0 bq:0.0 tq:0.0 rt:0.0 prq:0.0 1mp:4 1mb:0 1mt:4) -EntranceNode: sentinel_gateway_context$$route$$my-route-1(t:0 pq:0.0 bq:0.0 tq:0.0 rt:0.0 prq:0.0 1mp:6 1mb:0 1mt:6) --my-route-1(t:0 pq:0.0 bq:0.0 tq:0.0 rt:0.0 prq:0.0 1mp:2 1mb:0 1mt:2) --some_customized_api(t:0 pq:0.0 bq:0.0 tq:0.0 rt:0.0 prq:0.0 1mp:2 1mb:0 1mt:2)

发生限流之后的处理流程 :

  • 发生限流之后可自定义返回参数,通过实现SentinelFallbackProvider接口,默认的实现是DefaultBlockFallbackProvider
  • 默认的 fallback route 的规则是 route ID 或自定义的 API 分组名称。

比如:

// 自定义 FallbackProvider public class MyBlockFallbackProvider implements ZuulBlockFallbackProvider { private Logger logger = LoggerFactory.getLogger(DefaultBlockFallbackProvider.class); // you can define route as service level @Override public String getRoute() { return "/book/app"; } @Override public BlockResponse fallbackResponse(String route, Throwable cause) { RecordLog.info(String.format("[Sentinel DefaultBlockFallbackProvider] Run fallback route: %s", route)); if (cause instanceof BlockException) { return new BlockResponse(429, "Sentinel block exception", route); } else { return new BlockResponse(500, "System Error", route); } } } // 注册 FallbackProvider ZuulBlockFallbackManager.registerProvider(new MyBlockFallbackProvider());

限流发生之后的默认返回:

{ "code":429, "message":"Sentinel block exception", "route":"/" }

注意

  • Sentinel 网关流控默认的粒度是 route 维度以及自定义 API 分组维度,默认不支持 URL 粒度。若通过 Spring Cloud Alibaba 接入,请将spring.cloud.sentinel.filter.enabled配置项置为 false(若在网关流控控制台上看到了 URL 资源,就是此配置项没有置为 false)。
  • 若使用 Spring Cloud Alibaba Sentinel 数据源模块,需要注意网关流控规则数据源类型是gw-flow,若将网关流控规则数据源指定为 flow 则不生效。

Zuul 2.x

注:从 1.7.2 版本开始支持,需要 Java 8 及以上版本。

Sentinel 提供了 Zuul 2.x 的适配模块,可以为 Zuul Gateway 提供两种资源维度的限流:

  • route 维度:对应 SessionContext 中的routeVIP
  • 自定义 API 维度:用户可以利用 Sentinel 提供的 API 来自定义一些 API 分组

使用时需引入以下模块(以 Maven 为例):

<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-zuul2-adapter</artifactId> <version>x.y.z</version> </dependency>

然后配置对应的 filter 即可:

filterMultibinder.addBinding().toInstance(new SentinelZuulInboundFilter(500)); filterMultibinder.addBinding().toInstance(new SentinelZuulOutboundFilter(500)); filterMultibinder.addBinding().toInstance(new SentinelZuulEndpoint());

可以参考 sentinel-demo-zuul2-gateway 示例。

网关流控实现原理

当通过GatewayRuleManager加载网关流控规则(GatewayFlowRule)时,无论是否针对请求属性进行限流,Sentinel 底层都会将网关流控规则转化为热点参数规则(ParamFlowRule),存储在GatewayRuleManager中,与正常的热点参数规则相隔离。转换时 Sentinel 会根据请求属性配置,为网关流控规则设置参数索引(idx),并同步到生成的热点参数规则中。

外部请求进入 API Gateway 时会经过 Sentinel 实现的 filter,其中会依次进行路由/API 分组匹配请求属性解析参数组装。Sentinel 会根据配置的网关流控规则来解析请求属性,并依照参数索引顺序组装参数数组,最终传入SphU.entry(res, args)中。Sentinel API Gateway Adapter Common 模块向 Slot Chain 中添加了一个GatewayFlowSlot,专门用来做网关规则的检查。GatewayFlowSlot会从GatewayRuleManager中提取生成的热点参数规则,根据传入的参数依次进行规则检查。若某条规则不针对请求属性,则会在参数最后一个位置置入预设的常量,达到普通流控的效果。

网关流控控制台

Sentinel 1.6.3 引入了网关流控控制台的支持,用户可以直接在 Sentinel 控制台上查看 API Gateway 实时的 route 和自定义 API 分组监控,管理网关规则和 API 分组配置。

在 API Gateway 端,用户只需要在原有启动参数的基础上添加如下启动参数即可标记应用为 API Gateway 类型:

# 注:通过 Spring Cloud Alibaba Sentinel 自动接入的 API Gateway 整合则无需此参数 -Dcsp.sentinel.app.type=1

添加正确的启动参数并有访问量后,我们就可以在 Sentinel 上面看到对应的 API Gateway 了。我们可以查看实时的 route 和自定义 API 分组的监控和调用信息:

我们可以在控制台配置自定义的 API 分组,将一些 URL 匹配模式归为一个 API 分组:

然后我们可以在控制台针对预设的 route ID 或自定义的 API 分组配置网关流控规则:

网关规则动态配置及网关集群流控可以参考 AHAS Sentinel 网关流控。


source

api-gateway-flow-control | Sentinel

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/14 6:42:34

大型流水线贴膜机项目程序:初学者的福音

大型流水线贴膜机完成项目程序&#xff0c;包含PLC程序和触摸屏程序&#xff0c;程序内 包含上下气缸控制&#xff0c;夹紧气缸控制&#xff0c;输送带电机控制&#xff0c;贴膜伺服控制&#xff0c;旋转电机控制等类容&#xff0c;非常适合学习简单控制工艺及运动控制初学者学…

作者头像 李华
网站建设 2026/4/12 16:20:08

电场流线生成技巧

comsol针尖刺穿表皮细胞数值模拟&#xff0c;可以得到组织细胞浓度分布及电磁场分布&#xff0c;最近在搞生物电穿孔的模拟&#xff0c;拿COMSOL折腾了个针尖刺穿表皮细胞的模型。这玩意儿能同时看到细胞被扎穿时的离子浓度变化和电场分布&#xff0c;特别适合研究微针给药时的…

作者头像 李华
网站建设 2026/4/15 4:38:19

产业链影响:上游受益,下游分化,本土配套加速

产业链影响&#xff1a;上游受益&#xff0c;下游分化&#xff0c;本土配套加速 上游&#xff08;芯片配套 / 服务器 / 数据中心&#xff09;&#xff1a;英伟达在华研发、生产、供应链投入&#xff08;如上海张江基地&#xff09;&#xff0c;将带动 PCB、散热、服务器制造、数…

作者头像 李华
网站建设 2026/4/13 4:39:51

吉时利2400 2410 2430精密数字源表

‌吉时利2400是Tektronix&#xff08;原Keithley&#xff09;推出的图形化系列源测量单元&#xff08;SMU&#xff09;‌&#xff0c;集精密电压/电流源与测量功能于一体&#xff0c;配备触摸屏界面&#xff0c;适用于半导体、医疗等领域的精密电子测试。‌‌ 1‌‌ 2 核心功能…

作者头像 李华
网站建设 2026/4/14 19:21:00

极简排班(安卓)手机端免费排班工具,轮班倒班轻松记录

多数排班工具都以电脑端为主&#xff0c;而这款极简排班专为安卓手机打造&#xff0c;用手机随时查看、设置排班更便捷&#xff0c;且目前完全免费&#xff0c;适配日常排班记录、轮班制办公需求 软件下载地址 软件操作极简&#xff0c;上手无门槛&#xff1a;先添加班次&…

作者头像 李华
网站建设 2026/4/10 12:11:41

计算机Java毕设实战-基于springboot的二手手机销售系统电子产品交易销售管理系统【完整源码+LW+部署说明+演示视频,全bao一条龙等】

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华