小白学习gateway集成sentinel

网关流控定义

Sentinel支持对Spring Cloud Gateway、Zuul等主流的API Gataway 进行限流,作用在网关的流控称之为网关流控,其实现原理请点击网关限流进入官方Wiki查看。

这里只把原理图从官方文档摘出来,需多关注图中提到的模块名和几个类名,因为都是核心级别的存在。

image.png

规则类型gw-flowgw-api-group为网关流控规则,具体类型请查看规则类型枚举RuleType

 

java

复制代码

/** * flow 流控规则 */ FLOW("flow", FlowRule.class), /** * degrade 降级规则 */ DEGRADE("degrade", DegradeRule.class), /** * param flow 热点规则 */ PARAM_FLOW("param-flow", ParamFlowRule.class), /** * system 系统规则 */ SYSTEM("system", SystemRule.class), /** * authority 授权规则 */ AUTHORITY("authority", AuthorityRule.class), /** * gateway flow 网关限流规则 */ GW_FLOW("gw-flow","com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule"), /** * api 用户自定义的 API 定义分组,可以看做是一些 URL 匹配的组合 */ GW_API_GROUP("gw-api-group","com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition");

image.png

网关流控规则

Field 说明 默认值
resource 资源名称,网关route或自定义API分组名称(注:网关route这里的值不是route.id,可调试
resourceMode 限流资源类型,网关route【0】或自定义API分组【1】(详细查看GatewayFlowRuleSentinelGatewayConstants 网关route
grade 限流阈值类型,QPS【1】或线程数【0】 QPS
count 限流阈值,QPS阈值或线程数值
intervalSec 统计时间间隔,单位秒 1秒
controlB
从 1.6.0 版本开始,Sentinel 提供了 Spring Cloud Gateway 的适配模块,可实现两者集成,提供 route 维度和自定义 API 维度的限流 [^3]。集成步骤如下: 1. **添加依赖**:添加 Sentinel 相关依赖到项目中。 2. **添加 sentinel 控制台配置**:配置 Sentinel 控制台的相关信息,如地址等。 3. **在 spring 容器中配置一个 Sentinel 的全局过滤器**:确保请求经过 Sentinel 进行限流等操作。 4. **测试**:完成上述配置后,可进行测试以验证集成是否成功 [^1]。 若要实现规则持久化,有不同方式及对应步骤: - **规则持久化到文件**: - 配置依赖。 - 配置文件。 - 编写代码 [^1]。 - **规则持久化到 Nacos**: - 添加 sentinel - datasource - nacos 依赖。 - 在 application.properties 配置持久化数据源。 - 在 nacos 配置中心配置流控规则 [^1]。 同时,还需注意以下内容: - 下载 Sentinel 并启动,下载地址为 https://github.com/alibaba/Sentinel/releases ,启动命令为 `java -Dserver.port=8888 -jar sentinel-dashboard-1.8.6.jar`,之后可在 sentinel 控制台添加流控规则 [^4]。 - 对于客户端,由于重启项目后内存中的限流、降级等规则会丢失,所以项目启动时需要重新加载配置规则到内存中,Gateway 服务需增加配置类 [^2]。 - 若要自定义 API 拦截规则,如只拦截以 `{tenant}/service/content/place` 形式的接口,需要根据正则表达式匹配该模式的请求地址并将其声明为可拦截的 API 资源,再设定限流、熔断规则 [^5]。 示例代码如下,用于自定义 API 拦截规则: ```java import com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants; import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition; import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem; import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem; import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager; import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule; import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager; import com.alibaba.csp.sentinel.slots.block.RuleConstant; import java.util.HashSet; import java.util.Set; public class CustomApiRuleExample { public static void main(String[] args) { Set<ApiDefinition> definitions = new HashSet<>(); ApiDefinition placeApi = new ApiDefinition("place_flow") .setPredicateItems(new HashSet<ApiPredicateItem>() {{ add(new ApiPathPredicateItem().setPattern("/lh9999/service/content/place/**") .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX)); }}); definitions.add(placeApi); GatewayApiDefinitionManager.loadApiDefinitions(definitions); Set<GatewayFlowRule> rules = new HashSet<>(); rules.add(new GatewayFlowRule("place_flow") .setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME) .setCount(3) .setGrade(RuleConstant.FLOW_GRADE_QPS) .setIntervalSec(1)); GatewayRuleManager.loadRules(rules); } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值