sentinal源码4-demo-basic处理流程

2019-04-02  本文已影响0人  modou1618

一 黑白名单

1.1 规则AuthorityRule

1.2 初始化配置规则

private static void initWhiteRules() {
    AuthorityRule rule = new AuthorityRule();
    rule.setResource(RESOURCE_NAME);
    rule.setStrategy(RuleConstant.AUTHORITY_WHITE);
    rule.setLimitApp("appA,appE");
    AuthorityRuleManager.loadRules(Collections.singletonList(rule));
}

1.3 配置规则管理AuthorityRuleManager

1.3.1 初始化

static {
    currentProperty.addListener(LISTENER);
}

1.3.2 RulePropertyListener

public void configUpdate(List<AuthorityRule> conf) {
    Map<String, Set<AuthorityRule>> rules = loadAuthorityConf(conf);

    authorityRules.clear();
    if (rules != null) {
        authorityRules.putAll(rules);
    }
    RecordLog.info("[AuthorityRuleManager] Authority rules received: " + authorityRules);
}

public void configLoad(List<AuthorityRule> value) {
    Map<String, Set<AuthorityRule>> rules = loadAuthorityConf(value);

    authorityRules.clear();
    if (rules != null) {
        authorityRules.putAll(rules);
    }
    RecordLog.info("[AuthorityRuleManager] Load authority rules: " + authorityRules);
}
private Map<String, Set<AuthorityRule>> loadAuthorityConf(List<AuthorityRule> list) {
    Map<String, Set<AuthorityRule>> newRuleMap = new ConcurrentHashMap<>();

    if (list == null || list.isEmpty()) {
        return newRuleMap;
    }

    for (AuthorityRule rule : list) {
        if (!isValidRule(rule)) {
            RecordLog.warn("[AuthorityRuleManager] Ignoring invalid authority rule when loading new rules: " + rule);
            continue;
        }

        if (StringUtil.isBlank(rule.getLimitApp())) {
            rule.setLimitApp(RuleConstant.LIMIT_APP_DEFAULT);
        }

        String identity = rule.getResource();
        Set<AuthorityRule> ruleSet = newRuleMap.get(identity);
        // putIfAbsent
        if (ruleSet == null) {
            ruleSet = new HashSet<>();
            ruleSet.add(rule);
            newRuleMap.put(identity, ruleSet);
        } else {
            // One resource should only have at most one authority rule, so just ignore redundant rules.
            RecordLog.warn("[AuthorityRuleManager] Ignoring redundant rule: " + rule.toString());
        }
    }

    return newRuleMap;
}

1.4 配置规则校验AuthorityRuleChecker

static boolean passCheck(AuthorityRule rule, Context context) {
    String requester = context.getOrigin();//1
    if (StringUtil.isEmpty(requester) || StringUtil.isEmpty(rule.getLimitApp())) {//2
        return true;
    }

    int pos = rule.getLimitApp().indexOf(requester);//3
    boolean contain = pos > -1;

    if (contain) {//4
        boolean exactlyMatch = false;
        String[] appArray = rule.getLimitApp().split(",");
        for (String app : appArray) {//
            if (requester.equals(app)) {
                exactlyMatch = true;
                break;
            }
        }

        contain = exactlyMatch;
    }

    int strategy = rule.getStrategy();//5
    if (strategy == RuleConstant.AUTHORITY_BLACK && contain) {
        return false;
    }

    if (strategy == RuleConstant.AUTHORITY_WHITE && !contain) {
        return false;
    }

    return true;
}

1.5 规则应用AuthoritySlot

void checkBlackWhiteAuthority(ResourceWrapper resource, Context context) throws AuthorityException {
    Map<String, Set<AuthorityRule>> authorityRules = AuthorityRuleManager.getAuthorityRules();

    if (authorityRules == null) {
        return;
    }

    Set<AuthorityRule> rules = authorityRules.get(resource.getName());
    if (rules == null) {
        return;
    }

    for (AuthorityRule rule : rules) {
        if (!AuthorityRuleChecker.passCheck(rule, context)) {
            throw new AuthorityException(context.getOrigin(), rule);
        }
    }
}

二 系统

三 降级

四 流控

五 异步

上一篇 下一篇

猜你喜欢

热点阅读