背景

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

Spring Cloud Alibaba Sentinel集成Spring Cloud 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 分组

关于Spring Cloud Gateway这里就不做过多介绍,可以看我之前的文章springcloud 入门 之网关 springcloud gateway

集成入门

  1. 新建一个springboot项目子模块cloud_gateway

父模块依赖如下:

<properties>
<java.version>1.8</java.version>
<spring.cloud.version>2021.0.1</spring.cloud.version>
<springboot.version>2.6.4</springboot.version>
<spring.cloud.alibaba.version>2021.0.1.0</spring.cloud.alibaba.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

若想跟 Sentinel Starter 配合使用,需要加上 ​​spring-cloud-alibaba-sentinel-gateway​​​ 依赖,同时需要添加 ​​spring-cloud-starter-gateway​​​ 依赖来让 ​​spring-cloud-alibaba-sentinel-gateway​​ 模块里的 Spring Cloud Gateway 自动化配置类生效,子模块cloud_gateway引入依赖:

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

同时请将 ​​spring.cloud.sentinel.filter.enabled​​ 配置项置为 false(若在网关流控控制台上看到了 URL 资源,就是此配置项没有置为 false)。Sentinel 网关流控默认的粒度是 route 维度以及自定义 API 分组维度,默认不支持 URL 粒度。

  1. 修改配置文件

server.port=8086

# 应用名
spring.application.name=SentinelGateway
spring.cloud.gateway.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true

# spring cloud gateway 采用的是netty+webflux
spring.main.web-application-type=reactive

spring.cloud.sentinel.filter.enabled=false

# 路由配置
spring.cloud.gateway.routes[0].id=my_csdn_route
spring.cloud.gateway.routes[0].ur
spring.cloud.gateway.routes[0].predicates[0]=Path=/qq_39654841

如果项目中引入了 ​​spring-boot-starter-web​​​依赖, ​​spring.main.web-application-type=reactive​​​ 这个配置一定要加上,因为Spring Cloud Gateway是使用netty+webflux方式实现的,跟​​spring-boot-starter-web​​冲突,启动时会报错。

  1. 测试

在配置文件中,我们配置了断言路径​​qq_39654841​​,在我们访问http://localhost:8086/qq_39654841路径时,Spring Cloud Gateway会帮我们重新定向到

总结

本篇文章我们只是简单的介绍和使用了 Sentinel集成Spring Cloud Gateway,算是对 Sentinel网关限流有了一个简单的认识,想认识的更多可以去官网看看。

参考资料:

​​​​https://github.com/alibaba/Sentinel/wiki/网关限流​

Spring Cloud Alibaba Sentinel集成Spring Cloud Gateway_spring_02