use of com.huawei.gray.dubbo.strategy.InvokerStrategy in project Sermant by huaweicloud.
the class WeightRuleStrategy method getTargetInvoker.
@Override
public List<Invoker<?>> getTargetInvoker(List<Route> routes, Invocation invocation, List<Invoker<?>> invokers, VersionFrom versionFrom) {
if (routes.get(0).getWeight() == null) {
// 规定第一个规则的流量为空,则设置为100
routes.get(0).setWeight(ONO_HUNDRED);
}
String targetVersion = null;
Set<String> notMatchVersions = new HashSet<String>();
int begin = 1;
int num = new Random().nextInt(ONO_HUNDRED) + 1;
for (Route route : routes) {
@SuppressWarnings("checkstyle:RegexpSingleline") 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<Invoker<?>> resultList = new ArrayList<Invoker<?>>();
for (Invoker<?> invoker : invokers) {
if (invokerStrategy.isMatch(invoker, targetVersion, notMatchVersions, versionStrategy)) {
resultList.add(invoker);
}
}
return CollectionUtils.isEmpty(resultList) ? invokers : resultList;
}
use of com.huawei.gray.dubbo.strategy.InvokerStrategy 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;
}
Aggregations