use of com.huawei.route.common.gray.label.entity.Route in project Sermant by huaweicloud.
the class WeightRuleStrategy method getTargetInvoker.
@Override
public List<Object> getTargetInvoker(List<Route> routes, List<Object> invokers, VersionFrom versionFrom) {
if (routes.get(0).getWeight() == null) {
// 规定第一个规则的流量为空,则设置为100
routes.get(0).setWeight(ONO_HUNDRED);
}
String targetVersion = null;
Set<String> notMatchVersions = new HashSet<>();
int begin = 1;
int num = new Random().nextInt(ONO_HUNDRED) + 1;
for (Route route : routes) {
Integer weight = route.getWeight();
if (weight == null) {
continue;
}
String currentVersion = route.getTags().getVersion();
if (num >= begin && num <= begin + weight - 1) {
targetVersion = currentVersion;
break;
}
begin += weight;
notMatchVersions.add(currentVersion);
}
VersionStrategy versionStrategy = VersionChooser.INSTANCE.choose(versionFrom);
InvokerStrategy invokerStrategy = InvokerChooser.INSTANCE.choose(targetVersion);
List<Object> resultList = new ArrayList<>();
for (Object invoker : invokers) {
if (invokerStrategy.isMatch(invoker, targetVersion, notMatchVersions, versionStrategy)) {
resultList.add(invoker);
}
}
return CollectionUtils.isEmpty(resultList) ? invokers : resultList;
}
use of com.huawei.route.common.gray.label.entity.Route 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