<dubbo:service>和<dubbo:reference>存在一些相同的参数,例如:timeout,retries等,那么哪个配置的优先级高呢?

consumer合并url参数的顺序为:override > -D >Consumer > Provider

调用栈:

URL com.alibaba.dubbo.registry.integration.RegistryDirectory.mergeUrl(URL providerUrl)

private URL mergeUrl(URL providerUrl){
  // 合并消费端参数。queryMap存放consumer端的参数
    providerUrl = ClusterUtils.mergeUrl(providerUrl, queryMap); 
    // local reference
    List<Configurator> localConfigurators = this.configurators; 
    if (localConfigurators != null && localConfigurators.size() > 0) {
        for (Configurator configurator : localConfigurators) {
            providerUrl = configurator.configure(providerUrl);
        }
    }

    providerUrl = providerUrl.addParameter(Constants.CHECK_KEY, String.valueOf(false)); 
    
    //directoryUrl 与 override 合并是在notify的最后,这里不能够处理
    this.overrideDirectoryUrl = this.overrideDirectoryUrl.addParametersIfAbsent(providerUrl.getParameters()); 
    
    // 合并提供者参数        
    if ((providerUrl.getPath() == null || providerUrl.getPath().length() == 0)
            && "dubbo".equals(providerUrl.getProtocol())) { // 兼容1.0
        //fix by tony.chenl DUBBO-44
        String path = directoryUrl.getParameter(Constants.INTERFACE_KEY);
        if (path != null) {
            int i = path.indexOf('/');
            if (i >= 0) {
                path = path.substring(i + 1);
            }
            i = path.lastIndexOf(':');
            if (i >= 0) {
                path = path.substring(0, i);
            }
            providerUrl = providerUrl.setPath(path);
        }
    }
    return providerUrl;
}

具体的覆盖逻辑:

public static URL mergeUrl(URL remoteUrl, Map<String, String> localMap) {
    Map<String, String> map = new HashMap<String, String>();
    Map<String, String> remoteMap = remoteUrl.getParameters();
    
    
    if (remoteMap != null && remoteMap.size() > 0) {
        map.putAll(remoteMap);
        
        //线程池配置不使用提供者的
        map.remove(Constants.THREAD_NAME_KEY);
        map.remove(Constants.DEFAULT_KEY_PREFIX + Constants.THREAD_NAME_KEY);

        map.remove(Constants.THREADPOOL_KEY);
        map.remove(Constants.DEFAULT_KEY_PREFIX + Constants.THREADPOOL_KEY);

        map.remove(Constants.CORE_THREADS_KEY);
        map.remove(Constants.DEFAULT_KEY_PREFIX + Constants.CORE_THREADS_KEY);

        map.remove(Constants.THREADS_KEY);
        map.remove(Constants.DEFAULT_KEY_PREFIX + Constants.THREADS_KEY);

        map.remove(Constants.QUEUES_KEY);
        map.remove(Constants.DEFAULT_KEY_PREFIX + Constants.QUEUES_KEY);

        map.remove(Constants.ALIVE_KEY);
        map.remove(Constants.DEFAULT_KEY_PREFIX + Constants.ALIVE_KEY);
    }
    
    if (localMap != null && localMap.size() > 0) {
        map.putAll(localMap);
    }
    if (remoteMap != null && remoteMap.size() > 0) { 
        // 版本号使用提供者的
        String dubbo = remoteMap.get(Constants.DUBBO_VERSION_KEY);
        if (dubbo != null && dubbo.length() > 0) {
            map.put(Constants.DUBBO_VERSION_KEY, dubbo);
        }
        String version = remoteMap.get(Constants.VERSION_KEY);
        if (version != null && version.length() > 0) {
            map.put(Constants.VERSION_KEY, version);
        }
        String group = remoteMap.get(Constants.GROUP_KEY);
        if (group != null && group.length() > 0) {
            map.put(Constants.GROUP_KEY, group);
        }
        String methods = remoteMap.get(Constants.METHODS_KEY);
        if (methods != null && methods.length() > 0) {
            map.put(Constants.METHODS_KEY, methods);
        }
        // 合并filter和listener
        String remoteFilter = remoteMap.get(Constants.REFERENCE_FILTER_KEY);
        String localFilter = localMap.get(Constants.REFERENCE_FILTER_KEY);
        if (remoteFilter != null && remoteFilter.length() > 0
                && localFilter != null && localFilter.length() > 0) {
            localMap.put(Constants.REFERENCE_FILTER_KEY, remoteFilter + "," + localFilter);
        }
        String remoteListener = remoteMap.get(Constants.INVOKER_LISTENER_KEY);
        String localListener = localMap.get(Constants.INVOKER_LISTENER_KEY);
        if (remoteListener != null && remoteListener.length() > 0
                && localListener != null && localListener.length() > 0) {
            localMap.put(Constants.INVOKER_LISTENER_KEY, remoteListener + "," + localListener);
        }
    }

    return remoteUrl.clearParameters().addParameters(map);
}

 

posted on 2018-02-27 17:24  偶尔发呆  阅读(1288)  评论(0编辑  收藏  举报