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()));
}
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;
}
Aggregations