顾名思义,网关限流是通过网关层对我们的服务进行限流,达到保护后端服务的作用。在微服务架构的系统中,网关层可以屏蔽外部的请求直接对服务进行调用,网关层可以对内部服务进行隔离,保护的作用。
Sentinel 支持对 Spring Cloud Gateway、Zuul 等主流的 API Gateway 进行限流。由于Zuul已经处于停更的状态,所以我们使用Sentinel与Spring Cloud Gateway设计案例,为大家讲解如何使用sentinel实现网关限流。
如下图所示,Sentinel通过引入 Sentinel API Gateway Adapter Common 模块,以此实现了网关规则管理、自定义API分组管理,进而对网关进行限流操作。
我们在Sentinel的控制台中并没有看到对网关流控的配置界面。是由于我们启动控制台时使用默认配置,我们只需要在原有启动参数的基础上添加如下启动参数即可标记应用为 API Gateway 类型:
# 注:通过 Spring Cloud Alibaba Sentinel 自动接入的 API Gateway 整合则无需此参数
-Dcsp.sentinel.app.type=1
启动网关控制台:
登录sentinel的控制台,并有访问量后,我们就可以在 Sentinel上面看到对应的 API Gateway 了,如下图所示:
使用时引入依赖:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
<version>x.y.z</version>
</dependency>
server:
port: 7004
spring:
application:
name: cloudalibaba-sentinel-gateway
cloud:
nacos:
discovery:
#nacos服务注册中心地址
server-addr: localhost:8848
sentinel:
transport:
#配置sentinel dashboard地址
dashboard: localhost:8080
port: 8719 #默认8719端口
scg: #配置限流之后,响应内容
fallback:
mode: response
response-status: 426
response-body: 限流了
gateway:
routes:
- id: order_routh #路由的ID,没有固定规则但要求唯一,建议配合服务名
# uri: http://localhost:1001 #单个实例,匹配后提供服务的路由地址 (启动,cloudalibaba-provider-order1001服务)
uri: lb://nacos-order-provider #使用nacos作为注册中心,匹配后提供服务的路由地址(集群实例配置方式)
predicates:
- Path=/order/** #断言,路径相匹配的进行路由
此案例中,我们利用原来案例nacos-order-provider作为服务端,使用nacos作为服务的注册中心,实现服务的负载均衡。使用gateway作为网关,配置断言,/order/**
进行匹配路由,进而访问nacos-order-provider服务的接口。
如下图所示:
此时由于我们还没有配置限流规则,不论如何调用接口http://localhost:7004/order/2,接口都不会被限流。案例比较简单,下面我们开始学习网关的流控规则。
Sentinel 提供了 Spring Cloud Gateway 的适配模块,提供了两种资源维度的限流:
我们对于流控规则并不陌生,阈值类型,流控方式,我们原来都讲过,就不再细讲,大家可以查看Sentinel流控规则简介与实践。我们只对资源维度进行详细的介绍。
route维度比较好理解,就是根据网关中配置的路由配置作为资源名,我们只配置了order_routh
一个路由策略。
使用order_routh
配置限流规则,阈值类型,以QPS为例,只有QPS阈值大于每秒2个请求,我们就对请求进行限流,流控方式快速失败。Burst size是指应对突发请求时额外允许的请求数目,我们配置为0。
sentinel限流默认返回提示:Blocked by Sentinel: ParamFlowException
,如下图所示:
限流规则还可以支持对请求中的参数、Header、来源 IP 等进行定制化的限流。比较简单,我们就不演示了。
API分组就是对接口进行分组,然后对不同组的接口,实施不同的限流策略。
对API进行限流,需要先对API进行分组。如下图所示:
配置模式:精确、前缀和正则三种模式。
精确模式是指对URL的路径完全匹配时,进行限流。例如,匹配串配置为/order/1
前缀模式是指对URL的路径前缀匹配时,进行限流。例如,匹配串配置为/order/*
正则模式是指对URL的路径符合正则表示式规则时,进行限流。例如,匹配串配置为\/order\/\d*
如下图所示:
根据API分组配置限流规则,API名称可以选择不同的API分组,进行配置,如下图所示:
我们以精确配置URL进行演示,其他两种大家可以自行实践。
访问接口http://localhost:7004/order/1,只要请求QPS数量每秒大于2次就会被限流,访问其他接口例如,http://localhost:7004/order/2,不论QPS多大都不会限流。
用户根据自定义的API分组,可以更加细致的对URL限流配置。对特殊的URL进行私人定制。
我们发现异常默认返回并不够人性化,如何能够自定义配置流控异常信息?
sentinel为我们实现了自定义流控异常的返回内容。在配置文件中添加配置如下:
spring:
cloud:
sentinel:
scg: #配置限流之后,响应内容
fallback:
mode: response
response-status: 426
response-body: 限流了
网关降级规则,与接口的降级规则是一样的,并没有增加新的内容,大家可以截止回顾原来的博文即可,Sentinel降级规则简介与实践
对于Sentinel使用Nacos作为持久化的数据源,我们已经介绍过了,不再过多的介绍,我们做了一个与网关结合的案例
cloudalibaba-sentinel-gateway-nacos7005
配置文件:
spring:
application:
name: cloudalibaba-sentinel-gateway
cloud:
nacos:
discovery:
#nacos服务注册中心地址
server-addr: localhost:8848
sentinel:
transport:
#配置sentinel dashboard地址
dashboard: localhost:8080
port: 8719 #默认8719端口
datasource:
flow:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-flow-rules
groupid: DEFAULT_GROUP
data-type: json
rule-type: flow
gw-flow:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-gw-flow-rules
groupid: DEFAULT_GROUP
data-type: json
rule-type: gw-flow
gw-api-group:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-gw-api-group-rules
groupid: DEFAULT_GROUP
data-type: json
rule-type: gw-api-group
# scg: #配置限流之后,自定义响应内容
# fallback:
# mode: response
# response-status: 426
# response-body: 限流了
gateway:
routes:
- id: order_routh #路由的ID,没有固定规则但要求唯一,建议配合服务名
# uri: http://localhost:1001 #单个实例,匹配后提供服务的路由地址 (启动,cloudalibaba-provider-order1001服务)
uri: lb://nacos-order-provider #使用nacos作为注册中心,匹配后提供服务的路由地址(集群实例配置方式)
predicates:
- Path=/order/** #断言,路径相匹配的进行路由
Nacos的配置内容,如下:
DateID:cloudalibaba-sentinel-gateway-flow-rules
配置内容:
[
{
"resource":"order_routh",
"limitApp":"default",
"grade":1,
"count":2,
"strategy":0,
"controlBehavior":0,
"clusterMode":false
}
]
DateID:cloudalibaba-sentinel-gateway-gw-flow-rules
[
{
"resource": "order_group_regular",
"count": 5,
"grade":1,
"resourceMode":1
}
]
DateID:cloudalibaba-sentinel-gateway-gw-api-group-rules
[
{
"apiName": "order_group",
"predicateItems": [
{
"pattern": "/order/1",
"matchStrategy": 0
}
]
},
{
"apiName": "order_group_prefix",
"predicateItems": [
{
"pattern": "/order/*",
"matchStrategy": 1
}
]
},
{
"apiName": "order_group_regular",
"predicateItems": [
{
"pattern": "\/order\/\d*",
"matchStrategy": 2
}
]
}
]
1、Spring Gateway结合Sentinel完美实现网关限流
2、Sentinel搭配Nacos实现配置的持久化
3、Spring Gateway搭配Nacos实现服务的负载均衡
三者强强联合,真正的网关限流三剑客。
本文示例读者可以通过查看下面仓库中的项目,如下所示:
<module>cloudalibaba-provider-order1001</module>
<module>cloudalibaba-provider-order1002</module>
<module>cloudalibaba-sentinel-gateway7004</module>
<module>cloudalibaba-sentinel-gateway-nacos7005</module>