Search in sources :

Example 1 with TAG_KEY

use of org.apache.dubbo.common.constants.CommonConstants.TAG_KEY in project dubbo by alibaba.

the class TagRouter method route.

@Override
public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
    if (CollectionUtils.isEmpty(invokers)) {
        return invokers;
    }
    // since the rule can be changed by config center, we should copy one to use.
    final TagRouterRule tagRouterRuleCopy = tagRouterRule;
    if (tagRouterRuleCopy == null || !tagRouterRuleCopy.isValid() || !tagRouterRuleCopy.isEnabled()) {
        return filterUsingStaticTag(invokers, url, invocation);
    }
    List<Invoker<T>> result = invokers;
    String tag = StringUtils.isEmpty(invocation.getAttachment(TAG_KEY)) ? url.getParameter(TAG_KEY) : invocation.getAttachment(TAG_KEY);
    // if we are requesting for a Provider with a specific tag
    if (StringUtils.isNotEmpty(tag)) {
        List<String> addresses = tagRouterRuleCopy.getTagnameToAddresses().get(tag);
        // filter by dynamic tag group first
        if (CollectionUtils.isNotEmpty(addresses)) {
            result = filterInvoker(invokers, invoker -> addressMatches(invoker.getUrl(), addresses));
            // if result is not null OR it's null but force=true, return result directly
            if (CollectionUtils.isNotEmpty(result) || tagRouterRuleCopy.isForce()) {
                return result;
            }
        } else {
            // dynamic tag group doesn't have any item about the requested app OR it's null after filtered by
            // dynamic tag group but force=false. check static tag
            result = filterInvoker(invokers, invoker -> tag.equals(invoker.getUrl().getParameter(TAG_KEY)));
        }
        // to false, which means it will invoke any providers without a tag unless it's explicitly disallowed.
        if (CollectionUtils.isNotEmpty(result) || isForceUseTag(invocation)) {
            return result;
        } else // FAILOVER: return all Providers without any tags.
        {
            List<Invoker<T>> tmp = filterInvoker(invokers, invoker -> addressNotMatches(invoker.getUrl(), tagRouterRuleCopy.getAddresses()));
            return filterInvoker(tmp, invoker -> StringUtils.isEmpty(invoker.getUrl().getParameter(TAG_KEY)));
        }
    } else {
        // List<String> addresses = tagRouterRule.filter(providerApp);
        // return all addresses in dynamic tag group.
        List<String> addresses = tagRouterRuleCopy.getAddresses();
        if (CollectionUtils.isNotEmpty(addresses)) {
            result = filterInvoker(invokers, invoker -> addressNotMatches(invoker.getUrl(), addresses));
            // 1. all addresses are in dynamic tag group, return empty list.
            if (CollectionUtils.isEmpty(result)) {
                return result;
            }
        // 2. if there are some addresses that are not in any dynamic tag group, continue to filter using the
        // static tag group.
        }
        return filterInvoker(result, invoker -> {
            String localTag = invoker.getUrl().getParameter(TAG_KEY);
            return StringUtils.isEmpty(localTag) || !tagRouterRuleCopy.getTagNames().contains(localTag);
        });
    }
}
Also used : ConfigurationListener(org.apache.dubbo.common.config.configcenter.ConfigurationListener) LoggerFactory(org.apache.dubbo.common.logger.LoggerFactory) Logger(org.apache.dubbo.common.logger.Logger) Predicate(java.util.function.Predicate) CollectionUtils(org.apache.dubbo.common.utils.CollectionUtils) Invocation(org.apache.dubbo.rpc.Invocation) Invoker(org.apache.dubbo.rpc.Invoker) RpcException(org.apache.dubbo.rpc.RpcException) NetUtils(org.apache.dubbo.common.utils.NetUtils) TagRouterRule(org.apache.dubbo.rpc.cluster.router.tag.model.TagRouterRule) Collectors(java.util.stream.Collectors) CommonConstants(org.apache.dubbo.common.constants.CommonConstants) StringUtils(org.apache.dubbo.common.utils.StringUtils) ConfigChangedEvent(org.apache.dubbo.common.config.configcenter.ConfigChangedEvent) URL(org.apache.dubbo.common.URL) List(java.util.List) FORCE_USE_TAG(org.apache.dubbo.rpc.Constants.FORCE_USE_TAG) ANYHOST_VALUE(org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE) ConfigChangeType(org.apache.dubbo.common.config.configcenter.ConfigChangeType) AbstractRouter(org.apache.dubbo.rpc.cluster.router.AbstractRouter) DynamicConfiguration(org.apache.dubbo.common.config.configcenter.DynamicConfiguration) TAG_KEY(org.apache.dubbo.common.constants.CommonConstants.TAG_KEY) TagRuleParser(org.apache.dubbo.rpc.cluster.router.tag.model.TagRuleParser) Invoker(org.apache.dubbo.rpc.Invoker) TagRouterRule(org.apache.dubbo.rpc.cluster.router.tag.model.TagRouterRule)

Aggregations

List (java.util.List)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 URL (org.apache.dubbo.common.URL)1 ConfigChangeType (org.apache.dubbo.common.config.configcenter.ConfigChangeType)1 ConfigChangedEvent (org.apache.dubbo.common.config.configcenter.ConfigChangedEvent)1 ConfigurationListener (org.apache.dubbo.common.config.configcenter.ConfigurationListener)1 DynamicConfiguration (org.apache.dubbo.common.config.configcenter.DynamicConfiguration)1 CommonConstants (org.apache.dubbo.common.constants.CommonConstants)1 ANYHOST_VALUE (org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE)1 TAG_KEY (org.apache.dubbo.common.constants.CommonConstants.TAG_KEY)1 Logger (org.apache.dubbo.common.logger.Logger)1 LoggerFactory (org.apache.dubbo.common.logger.LoggerFactory)1 CollectionUtils (org.apache.dubbo.common.utils.CollectionUtils)1 NetUtils (org.apache.dubbo.common.utils.NetUtils)1 StringUtils (org.apache.dubbo.common.utils.StringUtils)1 FORCE_USE_TAG (org.apache.dubbo.rpc.Constants.FORCE_USE_TAG)1 Invocation (org.apache.dubbo.rpc.Invocation)1 Invoker (org.apache.dubbo.rpc.Invoker)1 RpcException (org.apache.dubbo.rpc.RpcException)1