Contents

apache shenyu重构之AbstractShenyuPlugin

重构AbstractShenyuPlugin.execute()方法

原来的执行流程



initMatchCacheConfig();
String pluginName = named();
PluginData pluginData = BaseDataCache.getInstance().obtainPluginData(pluginName);
if (Objects.nonNull(pluginData) && pluginData.getEnabled()) {
    final String path = exchange.getRequest().getURI().getPath();
    SelectorData selectorData = obtainSelectorDataCacheIfEnabled(exchange);
    if (Objects.isNull(selectorData)) {
        List<SelectorData> selectors = BaseDataCache.getInstance().obtainSelectorData(pluginName);
        if (CollectionUtils.isEmpty(selectors)) {
            return handleSelectorIfNull(pluginName, exchange, chain);
        }
        Pair<Boolean, SelectorData> matchSelectorData = matchSelector(exchange, selectors);
        selectorData = matchSelectorData.getRight();
        if (Objects.isNull(selectorData)) {
            if (matchCacheConfig.getSelectorEnabled() && matchSelectorData.getLeft()) {
                selectorData = new SelectorData();
                selectorData.setPluginName(named());
                cacheSelectorData(path, selectorData);
            }
            return handleSelectorIfNull(pluginName, exchange, chain);
        } else {
            if (matchCacheConfig.getSelectorEnabled() && matchSelectorData.getLeft()) {
                cacheSelectorData(path, selectorData);
            }
        }
    } else {
        if (StringUtils.isBlank(selectorData.getId())) {
            return handleSelectorIfNull(pluginName, exchange, chain);
        }
    }
    selectorLog(selectorData, pluginName);
    if (Objects.nonNull(selectorData.getContinued()) && !selectorData.getContinued()) {
        // if continued, not match rules
        return doExecute(exchange, chain, selectorData, defaultRuleData(selectorData));
    }
    // handle rule
    RuleData ruleData = obtainRuleDataCacheIfEnabled(exchange);
    if (Objects.isNull(ruleData)) {
        // execute no cache
        List<RuleData> rules = BaseDataCache.getInstance().obtainRuleData(selectorData.getId());
        if (CollectionUtils.isEmpty(rules)) {
            return handleRuleIfNull(pluginName, exchange, chain);
        }
        if (selectorData.getType() == SelectorTypeEnum.FULL_FLOW.getCode()) {
            //get last
            RuleData rule = rules.get(rules.size() - 1);
            ruleLog(rule, pluginName);
            return doExecute(exchange, chain, selectorData, rule);
        } else {
            Pair<Boolean, RuleData> matchRuleData = matchRule(exchange, rules);
            ruleData = matchRuleData.getRight();
            if (Objects.isNull(ruleData)) {
                this.cacheRuleData(pluginName, path, matchRuleData);
                return handleRuleIfNull(pluginName, exchange, chain);
            } else {
                // if match success, cache rule.
                if (matchCacheConfig.getRuleEnabled() && matchRuleData.getLeft()) {
                    cacheRuleData(path, ruleData);
                }
            }
        }
    } else {
        if (Objects.isNull(ruleData.getId())) {
            // handle rule not match
            return handleRuleIfNull(pluginName, exchange, chain);
        }
    }
    ruleLog(ruleData, pluginName);
    return doExecute(exchange, chain, selectorData, ruleData);
}
return chain.execute(exchange);

重构流程

  • 获取变量数据
    • plugin
    • selector
    • rule
  • 卫语句提前结束
  • processSelector
    • 处理select为空的情况,卫语句提前执行
    • 记录日志,不为空记录日志
    • 已知selecor对象
      • 缓存获取
      • 不需要缓存对象
    • 执行select选中逻辑
  • processRule
    • 处理rule为空的情况,卫语句提前执行
    • 记录日志,不为空记录日志
    • 已知rule对象
      • 缓存获取
      • 不需要缓存对象
    • 执行select选中逻辑

重构后的代码



initMatchCacheConfig();
final String pluginName = named();
final String path = exchange.getRequest().getURI().getPath();
PluginData pluginData = BaseDataCache.getInstance().obtainPluginData(pluginName);
List<SelectorData> selectors = BaseDataCache.getInstance().obtainSelectorData(pluginName);
SelectorData selectorData = obtainSelectorDataCacheIfEnabled(path);
// early exit
if (Objects.isNull(pluginData) || !pluginData.getEnabled()) {
    return chain.execute(exchange);
}
// handle Selector
if (Objects.nonNull(selectorData) && StringUtils.isBlank(selectorData.getId())) {
    return handleSelectorIfNull(pluginName, exchange, chain);
}
if (Objects.isNull(selectorData)) {
    if (CollectionUtils.isEmpty(selectors)) {
        return handleSelectorIfNull(pluginName, exchange, chain);
    }
    Pair<Boolean, SelectorData> matchSelectorData = matchSelector(exchange, selectors);
    selectorData = matchSelectorData.getRight();
    if (Objects.isNull(selectorData)) {
        if (matchCacheConfig.getSelectorEnabled() && matchSelectorData.getLeft()) {
            selectorData = new SelectorData();
            selectorData.setPluginName(pluginName);
            cacheSelectorData(path, selectorData);
        }
        return handleSelectorIfNull(pluginName, exchange, chain);
    } else {
        if (matchCacheConfig.getSelectorEnabled() && matchSelectorData.getLeft()) {
            cacheSelectorData(path, selectorData);
        }
    }
}
printLog(selectorData, pluginName);
if (Objects.nonNull(selectorData.getContinued()) && !selectorData.getContinued()) {
    // if continued, not match rules
    return doExecute(exchange, chain, selectorData, defaultRuleData(selectorData));
}
// handle Rule
RuleData ruleData = obtainRuleDataCacheIfEnabled(path);
List<RuleData> rules = BaseDataCache.getInstance().obtainRuleData(selectorData.getId());
if (Objects.nonNull(ruleData) && Objects.isNull(ruleData.getId())) {
    return handleRuleIfNull(pluginName, exchange, chain);
}
if (CollectionUtils.isEmpty(rules)) {
    return handleRuleIfNull(pluginName, exchange, chain);
}
if (selectorData.getType() == SelectorTypeEnum.FULL_FLOW.getCode()) {
    //get last
    RuleData rule = rules.get(rules.size() - 1);
    printLog(rule, pluginName);
    return doExecute(exchange, chain, selectorData, rule);
} else {
    Pair<Boolean, RuleData> matchRuleData = matchRule(exchange, rules);
    ruleData = matchRuleData.getRight();
    if (Objects.isNull(ruleData)) {
        this.cacheRuleData(pluginName, path, matchRuleData);
        return handleRuleIfNull(pluginName, exchange, chain);
    } else {
        // if match success, cache rule.
        if (matchCacheConfig.getRuleEnabled() && matchRuleData.getLeft()) {
            cacheRuleData(path, ruleData);
        }
    }
}
printLog(ruleData, pluginName);
return doExecute(exchange, chain, selectorData, ruleData);