Spring Cloud Gateway如何与Sentinel实现流控功能?

2026-05-24 03:312阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1397个文字,预计阅读时间需要6分钟。

Spring Cloud Gateway如何与Sentinel实现流控功能?

目录+概述+快速开始+添加Sentinel配置文件+编写配置文件+测试+概述+Sentinel+支持对Spring Cloud Gateway、Zuul等主流API Gateway进行限流。

Sentinel 1.6.0 引入了Sentinel API Gateway Adapter Common 模块。

目录
  • 概述
  • 快速开始
  • 添加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的字段解释如下:

Spring Cloud Gateway如何与Sentinel实现流控功能?

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

这些参数在集成Nacos注册动态规则源动态推送的时候会用到,也可以通过Sentinel控制台添加,缺点就是服务重启所有规则都会丢失

  • 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 版本开始支持)

快速开始

使用Spring Cloud Alibab能够很方便的集成Sentinel,首先需要导入Sentinel相关的依赖

       <dependency>            <groupId>com.alibaba.cloud</groupId>            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>        </dependency>        <dependency>            <groupId>com.alibaba.csp</groupId>            <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>            <version>1.8.3</version>        </dependency>

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

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

添加Sentinel配置文件

package cuit.epoch.pymjl.config; import java.util.Collections; import java.util.HashMap; import java.util.List; import javax.annotation.PostConstruct; import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter; import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler; import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager; import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler; import org.springframework.beans.factory.ObjectProvider; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.user-service         predicates:           - Path=/user/get/{id}       - id: user_service_test         uri: lb://user-service         predicates:           - Path=/user/test

测试

先启动userservice和网关,然后访问一次对应的接口,观察Sentinel控制台

我们给user_service_test添加对应的流控规则:

然后再来访问测试:

如图所示,流控生效

到此这篇关于Spring Cloud Gateway集成Sentinel流控详情的文章就介绍到这了,更多相关Spring Cloud Gateway集成Sentinel 内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

本文共计1397个文字,预计阅读时间需要6分钟。

Spring Cloud Gateway如何与Sentinel实现流控功能?

目录+概述+快速开始+添加Sentinel配置文件+编写配置文件+测试+概述+Sentinel+支持对Spring Cloud Gateway、Zuul等主流API Gateway进行限流。

Sentinel 1.6.0 引入了Sentinel API Gateway Adapter Common 模块。

目录
  • 概述
  • 快速开始
  • 添加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的字段解释如下:

Spring Cloud Gateway如何与Sentinel实现流控功能?

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

这些参数在集成Nacos注册动态规则源动态推送的时候会用到,也可以通过Sentinel控制台添加,缺点就是服务重启所有规则都会丢失

  • 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 版本开始支持)

快速开始

使用Spring Cloud Alibab能够很方便的集成Sentinel,首先需要导入Sentinel相关的依赖

       <dependency>            <groupId>com.alibaba.cloud</groupId>            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>        </dependency>        <dependency>            <groupId>com.alibaba.csp</groupId>            <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>            <version>1.8.3</version>        </dependency>

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

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

添加Sentinel配置文件

package cuit.epoch.pymjl.config; import java.util.Collections; import java.util.HashMap; import java.util.List; import javax.annotation.PostConstruct; import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter; import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler; import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager; import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler; import org.springframework.beans.factory.ObjectProvider; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.user-service         predicates:           - Path=/user/get/{id}       - id: user_service_test         uri: lb://user-service         predicates:           - Path=/user/test

测试

先启动userservice和网关,然后访问一次对应的接口,观察Sentinel控制台

我们给user_service_test添加对应的流控规则:

然后再来访问测试:

如图所示,流控生效

到此这篇关于Spring Cloud Gateway集成Sentinel流控详情的文章就介绍到这了,更多相关Spring Cloud Gateway集成Sentinel 内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!