sentinel源码分析-07GatewayFlowSlot网关限流

本文介绍了Sentinel的GatewayFlowSlot网关限流。Sentinel支持Spring Cloud Gateway等API网关限流,可针对不同route或自定义API分组限流。可通过API或dashboard设置分组,配置的规则会被客户端处理。底层会将网关流控规则转化为热点参数规则,接入规则通过SentinelGatewayFilter解析参数,最终进入限流处理逻辑。

GatewayFlowSlot网关限流

sentinel支持api网关限流策略,其中包括 Spring Cloud Gateway。对于api网关限流,sentinel支持针对不同 route 或自定义的 API 分组进行限流,但是默认不支持 URL 粒度。

自定义分组可以通过api实现或通过dashboard页面配置,用户可以通过自定义分组将一些需要限流的url添加进来,支持精确匹配、前缀匹配、正则匹配,然后针对不同分组设置对应的限流规则,即可进行限流

通过api设置分组

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

这里设置了两个分组,并设置了对应匹配分组的规则

或者我们可以通dashboard设置分组,接口在GatewayApiController类,我们着重看一下保存逻辑
在这里插入图片描述

@PostMapping("/save.json")
    public Result<ApiDefinitionEntity> updateApi(HttpServletRequest request, @RequestBody UpdateApiReqVo reqVo) {
   
   
        AuthService.AuthUser authUser = authService.getAuthUser(request);

        String app = reqVo.getApp();
        if (StringUtil.isBlank(app)) {
   
   
            return Result.ofFail(-1, "app can't be null or empty");
        }

        authUser.authTarget(app, AuthService.PrivilegeType.WRITE_RULE);

        Long id = reqVo.getId();
        if (id == null) {
   
   
            return Result.ofFail(-1, "id can't be null");
        }

        ApiDefinitionEntity entity = repository.findById(id);
        if (entity == null) {
   
   
            return Result.ofFail(-1, "api does not exist, id=" + id);
        }

        // 匹配规则列表
        List<ApiPredicateItemVo> predicateItems = reqVo.getPredicateItems();
        if (CollectionUtils.isEmpty(predicateItems)) {
   
   
            return Result.ofFail(-1, "predicateItems can't empty");
        }

        List<ApiPredicateItemEntity> predicateItemEntities = new ArrayList<>();
        for (ApiPredicateItemVo predicateItem : predicateItems) {
   
   
            ApiPredicateItemEntity predicateItemEntity = new ApiPredicateItemEntity();

            // 匹配模式
            int matchStrategy = predicateItem.getMatchStrategy();
            if (!Arrays.asList(URL_MATCH_STRATEGY_EXACT, URL_MATCH_STRATEGY_PREFIX, URL_MATCH_STRATEGY_REGEX).contains(matchStrategy)) {
   
   
                return Result.ofFail(-1, "Invalid matchStrategy: " + matchStrategy);
            }
            predicateItemEntity.setMatchStrategy(matchStrategy);

            // 匹配串
            String pattern = predicateItem.getPattern();
            if (StringUtil.isBlank(pattern)) {
   
   
                return Result.ofFail(-1, "pattern can't be null or empty");
            }
            predicateItemEntity.setPattern(pattern);

            predicateItemEntities.add(predicateItemEntity);
        }
        entity.setPredicateItems(new LinkedHashSet<>(predicateItemEntities));

        Date date = new Date();
        entity.setGmtModified(date);

        try {
   
   
            entity = repository.save(entity);
        } catch (Throwable throwable) {
   
   
            logger.error("update gateway api error:", throwable);
            return Result.ofThrowable(-1, throwable);
        }

        if (!publishApis(app, entity.getIp(), entity.getPort())) {
   
   
            logger.warn("publish gateway apis fail after update");
        }

        return Result.ofSuccess(entity);
    }

这里可以看到就是针对不同属性的处理,然后将配置的自定义分组推送到客户端

private boolean publishApis(String app, String ip, Integer port) {
   
   
    List<ApiDefinitionEntity> apis = repository.findAllByMachine(MachineInfo.of(app, ip, port));
    return sentinelApiClient.modifyApis(app, ip, port, apis);
}

 public boolean modifyApis(String app, String ip, int port, List<ApiDefinitionEntity> apis) {
   
   
    if (apis == null) {
   
   
        return true;
    }

    try {
   
   
        AssertUtil.notEmpty(app, "Bad app name");
        AssertUtil.notEmpty(ip, "Bad machine IP");
        AssertUtil.isTrue(port > 0, "Bad machine port");
        
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值