Search in sources :

Example 6 with MatchString

use of com.tencent.polaris.client.pb.ModelProto.MatchString in project polaris-java by polarismesh.

the class RuleBasedRouter method matchValueByValueType.

private boolean matchValueByValueType(boolean isMatchSource, boolean isRegex, String ruleMetaKey, MatchString ruleMetaValue, String destMetaValue, Map<String, String> multiEnvRouterParamMap) {
    boolean allMetaMatched = true;
    switch(ruleMetaValue.getValueType()) {
        case PARAMETER:
            // 通过参数传入
            if (isMatchSource) {
                // 当匹配的是source,记录请求的 K V
                multiEnvRouterParamMap.put(ruleMetaKey, destMetaValue);
            } else {
                // 当匹配的是dest, 判断value
                if (!multiEnvRouterParamMap.containsKey(ruleMetaKey)) {
                    allMetaMatched = false;
                } else {
                    String ruleValue = multiEnvRouterParamMap.get(ruleMetaKey);
                    // contains key
                    allMetaMatched = matchValue(isRegex, destMetaValue, ruleValue);
                }
            }
            break;
        case VARIABLE:
            if (globalVariablesConfig.containsKey(ruleMetaKey)) {
                // 1.先从配置获取
                String ruleValue = (String) globalVariablesConfig.get(ruleMetaKey);
                allMetaMatched = matchValue(isRegex, destMetaValue, ruleValue);
            } else {
                // 2.从环境变量中获取  key从规则中获取
                String key = ruleMetaValue.getValue().getValue();
                if (!System.getenv().containsKey(key)) {
                    allMetaMatched = false;
                } else {
                    String value = System.getenv(key);
                    allMetaMatched = matchValue(isRegex, destMetaValue, value);
                }
                if (!System.getenv().containsKey(key) || !System.getenv(key).equals(destMetaValue)) {
                    allMetaMatched = false;
                }
            }
            break;
        default:
            allMetaMatched = matchValue(isRegex, destMetaValue, ruleMetaValue.getValue().getValue());
    }
    return allMetaMatched;
}
Also used : MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString)

Example 7 with MatchString

use of com.tencent.polaris.client.pb.ModelProto.MatchString in project polaris-java by polarismesh.

the class RuleBasedRouter method matchMetadata.

// 匹配metadata
private boolean matchMetadata(Map<String, MatchString> ruleMeta, Map<String, String> destMeta, boolean isMatchSource, Map<String, String> multiEnvRouterParamMap) {
    // 如果规则metadata为空, 返回成功
    if (MapUtils.isEmpty(ruleMeta)) {
        return true;
    }
    if (ruleMeta.containsKey(RuleUtils.MATCH_ALL)) {
        return true;
    }
    // 如果规则metadata不为空, 待匹配规则为空, 直接返回失败
    if (MapUtils.isEmpty(destMeta)) {
        return false;
    }
    // metadata是否全部匹配
    boolean allMetaMatched = true;
    // dest中找到的metadata个数, 用于辅助判断是否能匹配成功
    int matchNum = 0;
    for (Map.Entry<String, MatchString> entry : ruleMeta.entrySet()) {
        String ruleMetaKey = entry.getKey();
        MatchString ruleMetaValue = entry.getValue();
        if (RuleUtils.isMatchAllValue(ruleMetaValue)) {
            matchNum++;
            continue;
        }
        if (destMeta.containsKey(ruleMetaKey)) {
            matchNum++;
            if (!ruleMetaValue.hasValue() && ruleMetaValue.getValueType() != MatchString.ValueType.PARAMETER) {
                continue;
            }
            String destMetaValue = destMeta.get(ruleMetaKey);
            allMetaMatched = isAllMetaMatched(isMatchSource, true, ruleMetaKey, ruleMetaValue, destMetaValue, multiEnvRouterParamMap);
        }
        if (!allMetaMatched) {
            break;
        }
    }
    // 如果一个metadata未找到, 匹配失败
    if (matchNum == 0) {
        allMetaMatched = false;
    }
    return allMetaMatched;
}
Also used : MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

MatchString (com.tencent.polaris.client.pb.ModelProto.MatchString)7 HashMap (java.util.HashMap)4 Map (java.util.Map)3 ServiceRule (com.tencent.polaris.api.pojo.ServiceRule)2 Rule (com.tencent.polaris.client.pb.RateLimitProto.Rule)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Pattern (java.util.regex.Pattern)2 FlowCache (com.tencent.polaris.api.plugin.cache.FlowCache)1 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 DestinationSet (com.tencent.polaris.client.pb.CircuitBreakerProto.DestinationSet)1 MatchStringType (com.tencent.polaris.client.pb.ModelProto.MatchString.MatchStringType)1 RateLimit (com.tencent.polaris.client.pb.RateLimitProto.RateLimit)1 Routing (com.tencent.polaris.client.pb.RoutingProto.Routing)1 InstanceParameter (com.tencent.polaris.test.mock.discovery.NamingService.InstanceParameter)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1