Search in sources :

Example 6 with Route

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;
}
Also used : InvokerStrategy(com.huawei.gray.dubbo.strategy.InvokerStrategy) Random(java.util.Random) VersionStrategy(com.huawei.gray.dubbo.strategy.VersionStrategy) ArrayList(java.util.ArrayList) Route(com.huawei.route.common.gray.label.entity.Route) HashSet(java.util.HashSet)

Example 7 with Route

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;
}
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

Route (com.huawei.route.common.gray.label.entity.Route)7 Rule (com.huawei.route.common.gray.label.entity.Rule)4 GrayConfiguration (com.huawei.route.common.gray.label.entity.GrayConfiguration)3 ArrayList (java.util.ArrayList)3 Random (java.util.Random)3 InvokerStrategy (com.huawei.gray.dubbo.strategy.InvokerStrategy)2 VersionStrategy (com.huawei.gray.dubbo.strategy.VersionStrategy)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Invoker (org.apache.dubbo.rpc.Invoker)2 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 MatchRule (com.huawei.route.common.gray.label.entity.MatchRule)1 Request (feign.Request)1 URL (java.net.URL)1 URL (org.apache.dubbo.common.URL)1 Invocation (org.apache.dubbo.rpc.Invocation)1