Search in sources :

Example 6 with Rule

use of com.huawei.route.common.gray.label.entity.Rule in project Sermant by huaweicloud.

the class GrayDynamicConfigListener method process.

@Override
public void process(DynamicConfigEvent event) {
    if (event.getKey() == null || !event.getKey().equals(key)) {
        return;
    }
    GrayConfiguration grayConfiguration = LabelCache.getLabel(labelName);
    Map<String, List<Rule>> routeRule = new LinkedHashMap<String, List<Rule>>();
    if (event.getEventType() == DynamicConfigEventType.DELETE) {
        resetGrayConfiguration(grayConfiguration);
        return;
    }
    Yaml yaml = new Yaml();
    Map<String, Map<String, Map<String, String>>> load = yaml.load(event.getContent());
    if (isInValidMap(load, grayConfiguration)) {
        resetGrayConfiguration(grayConfiguration);
        return;
    }
    Map<String, Map<String, String>> servicecomb = load.get(GrayConstant.GRAY_CONFIG_SERVICECOMB_KEY);
    if (isInValidMap(servicecomb, grayConfiguration)) {
        return;
    }
    Map<String, String> routeRuleMap = servicecomb.get(GrayConstant.GRAY_CONFIG_ROUTE_RULE_KEY);
    if (isInValidMap(routeRuleMap, grayConfiguration)) {
        return;
    }
    Yaml routeRuleYaml = new Yaml();
    for (Entry<String, String> entry : routeRuleMap.entrySet()) {
        List<Map<String, String>> routeRuleList = routeRuleYaml.load(entry.getValue());
        if (CollectionUtils.isEmpty(routeRuleList)) {
            continue;
        }
        Map<String, String> versionFromMap = routeRuleList.get(0);
        String versionFrom = versionFromMap.get(GrayConstant.GRAY_CONFIG_VERSION_FROM_KEY);
        if (StringUtils.isBlank(versionFrom)) {
            grayConfiguration.setVersionFrom(VersionFrom.REGISTER_MSG);
        } else {
            grayConfiguration.setVersionFrom(VersionFrom.valueOf(versionFrom.toUpperCase(Locale.ROOT)));
            routeRuleList.remove(0);
        }
        List<Rule> list = JSONArray.parseArray(JSONObject.toJSONString(routeRuleList), Rule.class);
        if (CollectionUtils.isEmpty(list)) {
            continue;
        }
        routeRule.put(entry.getKey(), list);
    }
    grayConfiguration.setRouteRule(routeRule);
    grayConfiguration.setOn(!CollectionUtils.isEmpty(routeRule));
    grayConfiguration.setValid(!CollectionUtils.isEmpty(routeRule));
    LOGGER.info(String.format(Locale.ROOT, "Config [%s] has been %s ", event.getKey(), event.getEventType()));
}
Also used : GrayConfiguration(com.huawei.route.common.gray.label.entity.GrayConfiguration) Yaml(org.yaml.snakeyaml.Yaml) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) Rule(com.huawei.route.common.gray.label.entity.Rule) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 7 with Rule

use of com.huawei.route.common.gray.label.entity.Rule in project Sermant by huaweicloud.

the class RegistryDirectoryServiceImpl method selectInvokers.

@Override
public Object selectInvokers(Object[] arguments, Object result) {
    if (arguments != null && arguments.length > 0 && arguments[0] instanceof Invocation) {
        GrayConfiguration grayConfiguration = LabelCache.getLabel(DubboCache.getLabelName());
        if (GrayConfiguration.isInValid(grayConfiguration)) {
            return result;
        }
        Invocation invocation = (Invocation) arguments[0];
        URL requestUrl = invocation.getInvoker().getUrl();
        if (!RouterUtil.isConsumer(requestUrl)) {
            return result;
        }
        String targetService = RouterUtil.getTargetService(requestUrl);
        String interfaceName = requestUrl.getServiceInterface() + "." + invocation.getMethodName();
        List<Rule> rules = RouterUtil.getValidRules(grayConfiguration, targetService, interfaceName);
        List<Route> routes = RouterUtil.getRoutes(rules, invocation.getArguments());
        RuleStrategyEnum ruleStrategyEnum = CollectionUtils.isEmpty(routes) ? RuleStrategyEnum.UPSTREAM : RuleStrategyEnum.WEIGHT;
        return ruleStrategyEnum.getTargetInvoker(routes, invocation, (List<Invoker<?>>) result, grayConfiguration.getVersionFrom());
    }
    return result;
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) Invoker(org.apache.dubbo.rpc.Invoker) GrayConfiguration(com.huawei.route.common.gray.label.entity.GrayConfiguration) RuleStrategyEnum(com.huawei.gray.dubbo.strategy.RuleStrategyEnum) Rule(com.huawei.route.common.gray.label.entity.Rule) URL(org.apache.dubbo.common.URL) Route(com.huawei.route.common.gray.label.entity.Route)

Aggregations

Rule (com.huawei.route.common.gray.label.entity.Rule)7 GrayConfiguration (com.huawei.route.common.gray.label.entity.GrayConfiguration)4 Route (com.huawei.route.common.gray.label.entity.Route)4 List (java.util.List)4 MatchRule (com.huawei.route.common.gray.label.entity.MatchRule)3 ArrayList (java.util.ArrayList)3 RuleStrategyEnum (com.huawei.gray.dubbo.strategy.RuleStrategyEnum)1 FeignResolvedUrl (com.huawei.gray.feign.context.FeignResolvedUrl)1 RuleType (com.huawei.gray.feign.rule.RuleType)1 Instances (com.huawei.route.common.gray.addr.entity.Instances)1 GrayConfig (com.huawei.route.common.gray.config.GrayConfig)1 Match (com.huawei.route.common.gray.label.entity.Match)1 ValueMatch (com.huawei.route.common.gray.label.entity.ValueMatch)1 Request (feign.Request)1 URL (java.net.URL)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 URL (org.apache.dubbo.common.URL)1 Invocation (org.apache.dubbo.rpc.Invocation)1 Invoker (org.apache.dubbo.rpc.Invoker)1