Search in sources :

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

Example 2 with Route

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

the class AbstractDirectoryServiceImpl method selectInvokers.

/**
 * 筛选灰度invoker
 *
 * @param obj RegistryDirectory
 * @param arguments 参数
 * @param result invokers
 * @return invokers
 * @see com.alibaba.dubbo.registry.integration.RegistryDirectory
 * @see org.apache.dubbo.registry.integration.RegistryDirectory
 * @see com.alibaba.dubbo.rpc.Invoker
 * @see org.apache.dubbo.rpc.Invoker
 */
@Override
public Object selectInvokers(Object obj, Object[] arguments, Object result) {
    if (arguments != null && arguments.length > 0) {
        GrayConfiguration grayConfiguration = LabelCache.getLabel(GrayConstant.GRAY_LABEL_CACHE_NAME);
        if (GrayConfiguration.isInValid(grayConfiguration)) {
            return result;
        }
        Map<String, String> queryMap = ReflectUtils.getQueryMap(obj);
        if (CollectionUtils.isEmpty(queryMap)) {
            return result;
        }
        if (!CONSUMER_VALUE.equals(queryMap.get(CONSUMER_KEY))) {
            return result;
        }
        String serviceInterface = queryMap.get(INTERFACE_KEY);
        Object invocation = arguments[0];
        String interfaceName = serviceInterface + "." + ReflectUtils.getMethodName(invocation);
        String targetService = DubboCache.INSTANCE.getApplication(serviceInterface);
        List<Rule> rules = RouterUtil.getValidRules(grayConfiguration, targetService, interfaceName);
        List<Route> routes = RouterUtil.getRoutes(rules, ReflectUtils.getArguments(invocation));
        if (CollectionUtils.isEmpty(routes)) {
            return result;
        }
        return RuleStrategyHandler.WEIGHT.getTargetInvoker(routes, (List<Object>) result, grayConfiguration.getVersionFrom());
    }
    return result;
}
Also used : GrayConfiguration(com.huawei.route.common.gray.label.entity.GrayConfiguration) Rule(com.huawei.route.common.gray.label.entity.Rule) Route(com.huawei.route.common.gray.label.entity.Route)

Example 3 with Route

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

the class RouterUtil method getValidRules.

/**
 * 获取有效规则
 *
 * @param grayConfiguration 灰度配置
 * @param targetService 目标服务
 * @param path 请求路径
 * @return 有效规则
 */
public static List<Rule> getValidRules(GrayConfiguration grayConfiguration, String targetService, String path) {
    if (GrayConfiguration.isInValid(grayConfiguration)) {
        return Collections.emptyList();
    }
    Map<String, List<Rule>> routeRule = grayConfiguration.getRouteRule();
    if (CollectionUtils.isEmpty(routeRule) || CollectionUtils.isEmpty(routeRule.get(targetService))) {
        return Collections.emptyList();
    }
    List<Rule> list = new ArrayList<Rule>();
    for (Rule rule : routeRule.get(targetService)) {
        if (!isValidRule(rule, CurrentInstance.getInstance().getAppName(), path)) {
            continue;
        }
        // 去掉无效的规则
        Map<String, List<MatchRule>> headerRules = rule.getMatch().getHeaders();
        if (headerRules != null) {
            removeInValidRules(headerRules);
            rule.getMatch().setHeaders(headerRules);
        }
        Map<String, List<MatchRule>> paramRules = rule.getMatch().getParameters();
        if (paramRules != null) {
            removeInValidRules(paramRules);
            rule.getMatch().setParameters(paramRules);
        }
        Map<String, List<MatchRule>> cookieRules = rule.getMatch().getCookie();
        if (cookieRules != null) {
            removeInValidRules(cookieRules);
            rule.getMatch().setCookie(cookieRules);
        }
        // 去掉无效的路由
        Iterator<Route> routeIterator = rule.getRoute().iterator();
        while (routeIterator.hasNext()) {
            if (isInValidRoute(routeIterator.next())) {
                routeIterator.remove();
            }
        }
        list.add(rule);
    }
    Collections.sort(list, new Comparator<Rule>() {

        @Override
        public int compare(Rule o1, Rule o2) {
            return o1.getPrecedence() - o2.getPrecedence();
        }
    });
    return list;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MatchRule(com.huawei.route.common.gray.label.entity.MatchRule) Rule(com.huawei.route.common.gray.label.entity.Rule) Route(com.huawei.route.common.gray.label.entity.Route)

Example 4 with Route

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

the class WeightRuleStrategy method getTargetServiceInstance.

@Override
public Optional<Instances> getTargetServiceInstance(List<Route> list, String targetService, Map<String, Collection<String>> headers) {
    Map<String, List<Instances>> map = AddrCache.getAddr(targetService, null);
    if (CollectionUtils.isEmpty(map)) {
        return Optional.empty();
    }
    // 剔除不合法的路由规则,和不在地址列表中的版本应用地址
    Iterator<Route> iterator = list.iterator();
    while (iterator.hasNext()) {
        Route route = iterator.next();
        if (route.getTags() == null || !map.containsKey(route.getTags().getVersion())) {
            iterator.remove();
        }
    }
    if (CollectionUtils.isEmpty(map)) {
        return Optional.empty();
    }
    if (list.get(0).getWeight() == null) {
        // 规定第一个规则不为空,则设置为100
        list.get(0).setWeight(ONO_HUNDRED);
    }
    int begin = 1;
    int num = new Random().nextInt(ONO_HUNDRED) + 1;
    for (Route route : list) {
        Integer weight = route.getWeight();
        if (weight == null) {
            continue;
        }
        String tagVersion = route.getTags().getVersion();
        if (num >= begin && num <= begin + weight - 1) {
            return Optional.of(map.get(tagVersion).get(0));
        }
        begin += weight;
    }
    return Optional.empty();
}
Also used : Random(java.util.Random) List(java.util.List) Route(com.huawei.route.common.gray.label.entity.Route)

Example 5 with Route

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

the class DefaultHttpClientServiceImpl method before.

@Override
public void before(Object obj, Method method, Object[] arguments, BeforeResult beforeResult) throws Exception {
    GrayConfig grayConfig = PluginConfigManager.getPluginConfig(GrayConfig.class);
    Request request = (Request) arguments[0];
    String targetAppName = HostContext.get();
    // 根据灰度规则重构请求地址
    GrayConfiguration grayConfiguration = LabelCache.getLabel(grayConfig.getSpringCloudKey());
    if (GrayConfiguration.isInValid(grayConfiguration)) {
        return;
    }
    // 获得url路径参数解析前的原始path
    URL url = new URL(request.url());
    String path = url.getPath();
    FeignResolvedUrl feignResolvedUrl = PathVarServiceImpl.URL_CONTEXT.get();
    if (feignResolvedUrl != null) {
        try {
            path = path.replace(feignResolvedUrl.getUrl().split("[?]")[0], feignResolvedUrl.getOriginUrl()).split("[?]")[0];
        } finally {
            PathVarServiceImpl.URL_CONTEXT.remove();
        }
    }
    // 获取匹配规则并替换url
    List<Rule> rules = RouterUtil.getValidRules(grayConfiguration, targetAppName, path);
    List<Route> routes = RouterUtil.getRoutes(rules, request);
    RuleType ruleType = CollectionUtils.isEmpty(routes) ? RuleType.UPSTREAM : RuleType.WEIGHT;
    Instances instance = ruleType.getTargetServiceInstance(routes, targetAppName, request.headers());
    if (instance != null) {
        String targetServiceHost = RouterUtil.getTargetHost(instance).orElse(null);
        String version = instance.getCurrentTag().getVersion();
        request = RouterUtil.rebuildUrl(targetServiceHost, version, request);
        arguments[0] = request;
    }
}
Also used : Instances(com.huawei.route.common.gray.addr.entity.Instances) GrayConfig(com.huawei.route.common.gray.config.GrayConfig) Request(feign.Request) GrayConfiguration(com.huawei.route.common.gray.label.entity.GrayConfiguration) FeignResolvedUrl(com.huawei.gray.feign.context.FeignResolvedUrl) RuleType(com.huawei.gray.feign.rule.RuleType) Rule(com.huawei.route.common.gray.label.entity.Rule) URL(java.net.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