Java Code Examples for com.alibaba.dubbo.common.URL#hasParameter()

The following examples show how to use com.alibaba.dubbo.common.URL#hasParameter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnregisterPageHandler.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Page handle(URL url) {
    String provider = url.getParameterAndDecoded("provider");
    if (provider == null || provider.length() == 0) {
        throw new IllegalArgumentException("Please input provider parameter.");
    }
    URL providerUrl = URL.valueOf(provider);
    RegistryContainer.getInstance().getRegistry().unregister(providerUrl);
    String parameter;
    if (url.hasParameter("service")) {
        parameter = "service=" + url.getParameter("service");
    } else if (url.hasParameter("host")) {
        parameter = "host=" + url.getParameter("host");
    } else if (url.hasParameter("application")) {
        parameter = "application=" + url.getParameter("application");
    } else {
        parameter = "service=" + providerUrl.getServiceInterface();
    }
    return new Page("<script type=\"text/javascript\">window.location.href=\"providers.html?" + parameter + "\";</script>");
}
 
Example 2
Source File: UnsubscribePageHandler.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Page handle(URL url) {
    String consumer = url.getParameterAndDecoded("consumer");
    if (consumer == null || consumer.length() == 0) {
        throw new IllegalArgumentException("Please input consumer parameter.");
    }
    URL consumerUrl = URL.valueOf(consumer);
    RegistryContainer.getInstance().getRegistry().unsubscribe(consumerUrl, NotifyListenerAdapter.NOTIFY_LISTENER);
    String parameter;
    if (url.hasParameter("service")) {
        parameter = "service=" + url.getParameter("service");
    } else if (url.hasParameter("host")) {
        parameter = "host=" + url.getParameter("host");
    } else if (url.hasParameter("application")) {
        parameter = "application=" + url.getParameter("application");
    } else {
        parameter = "service=" + consumerUrl.getServiceInterface();
    }
    return new Page("<script type=\"text/javascript\">window.location.href=\"consumers.html?" + parameter + "\";</script>");
}
 
Example 3
Source File: RestfulInvoker.java    From dubbo-plus with Apache License 2.0 6 votes vote down vote up
public RestfulInvoker(URL url, Class<?> serviceType) {
    StringBuffer reqUrl = new StringBuffer("");
    baseUrl=url.toIdentityString();
    if(url.hasParameter(Constants.VERSION_KEY)){
        reqUrl.append("/").append(url.getParameter(Constants.VERSION_KEY));
    }else{
        reqUrl.append("/").append(RestfulConstants.ALL);
    }
    if(url.hasParameter(Constants.GROUP_KEY)){
        reqUrl.append("/").append(url.getParameter(Constants.GROUP_KEY));
    }else{
        reqUrl.append("/").append(RestfulConstants.ALL);
    }
    versionGroupFragment = reqUrl.toString();
    this.serviceType = serviceType;
    this.url = url;
}
 
Example 4
Source File: UnsubscribePageHandler.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Page handle(URL url) {
    String consumer = url.getParameterAndDecoded("consumer");
    if (consumer == null || consumer.length() == 0) {
        throw new IllegalArgumentException("Please input consumer parameter.");
    }
    URL consumerUrl = URL.valueOf(consumer);
    RegistryContainer.getInstance().getRegistry().unsubscribe(consumerUrl, NotifyListenerAdapter.NOTIFY_LISTENER);
    String parameter;
    if (url.hasParameter("service")) {
        parameter = "service=" + url.getParameter("service");
    } else if (url.hasParameter("host")) {
        parameter = "host=" + url.getParameter("host");
    } else if (url.hasParameter("application")) {
        parameter = "application=" + url.getParameter("application");
    } else {
        parameter = "service=" + consumerUrl.getServiceInterface();
    }
    return new Page("<script type=\"text/javascript\">window.location.href=\"consumers.html?" + parameter + "\";</script>");
}
 
Example 5
Source File: UnregisterPageHandler.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public Page handle(URL url) {
    String provider = url.getParameterAndDecoded("provider");
    if (provider == null || provider.length() == 0) {
        throw new IllegalArgumentException("Please input provider parameter.");
    }
    URL providerUrl = URL.valueOf(provider);
    RegistryContainer.getInstance().getRegistry().unregister(providerUrl);
    String parameter;
    if (url.hasParameter("service")) {
        parameter = "service=" + url.getParameter("service");
    } else if (url.hasParameter("host")) {
        parameter = "host=" + url.getParameter("host");
    } else if (url.hasParameter("application")) {
        parameter = "application=" + url.getParameter("application");
    } else {
        parameter = "service=" + providerUrl.getServiceInterface();
    }
    return new Page("<script type=\"text/javascript\">window.location.href=\"providers.html?" + parameter + "\";</script>");
}
 
Example 6
Source File: UnsubscribePageHandler.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public Page handle(URL url) {
    String consumer = url.getParameterAndDecoded("consumer");
    if (consumer == null || consumer.length() == 0) {
        throw new IllegalArgumentException("Please input consumer parameter.");
    }
    URL consumerUrl = URL.valueOf(consumer);
    RegistryContainer.getInstance().getRegistry().unsubscribe(consumerUrl, NotifyListenerAdapter.NOTIFY_LISTENER);
    String parameter;
    if (url.hasParameter("service")) {
        parameter = "service=" + url.getParameter("service");
    } else if (url.hasParameter("host")) {
        parameter = "host=" + url.getParameter("host");
    } else if (url.hasParameter("application")) {
        parameter = "application=" + url.getParameter("application");
    } else {
        parameter = "service=" + consumerUrl.getServiceInterface();
    }
    return new Page("<script type=\"text/javascript\">window.location.href=\"consumers.html?" + parameter + "\";</script>");
}
 
Example 7
Source File: UnsubscribePageHandler.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Page handle(URL url) {
    String consumer = url.getParameterAndDecoded("consumer");
    if (consumer == null || consumer.length() == 0) {
        throw new IllegalArgumentException("Please input consumer parameter.");
    }
    URL consumerUrl = URL.valueOf(consumer);
    RegistryContainer.getInstance().getRegistry().unsubscribe(consumerUrl, NotifyListenerAdapter.NOTIFY_LISTENER);
    String parameter;
    if (url.hasParameter("service")) {
        parameter = "service=" + url.getParameter("service");
    } else if (url.hasParameter("host")) {
        parameter = "host=" + url.getParameter("host");
    } else if (url.hasParameter("application")) {
        parameter = "application=" + url.getParameter("application");
    } else {
        parameter = "service=" + consumerUrl.getServiceInterface();
    }
    return new Page("<script type=\"text/javascript\">window.location.href=\"consumers.html?" + parameter + "\";</script>");
}
 
Example 8
Source File: UnregisterPageHandler.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
public Page handle(URL url) {
    String provider = url.getParameterAndDecoded("provider");
    if (provider == null || provider.length() == 0) {
        throw new IllegalArgumentException("Please input provider parameter.");
    }
    URL providerUrl = URL.valueOf(provider);
    RegistryContainer.getInstance().getRegistry().unregister(providerUrl);
    String parameter;
    if (url.hasParameter("service")) {
        parameter = "service=" + url.getParameter("service");
    } else if (url.hasParameter("host")) {
        parameter = "host=" + url.getParameter("host");
    } else if (url.hasParameter("application")) {
        parameter = "application=" + url.getParameter("application");
    } else {
        parameter = "service=" + providerUrl.getServiceInterface();
    }
    return new Page("<script type=\"text/javascript\">window.location.href=\"providers.html?" + parameter + "\";</script>");
}
 
Example 9
Source File: UnsubscribePageHandler.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
public Page handle(URL url) {
    String consumer = url.getParameterAndDecoded("consumer");
    if (consumer == null || consumer.length() == 0) {
        throw new IllegalArgumentException("Please input consumer parameter.");
    }
    URL consumerUrl = URL.valueOf(consumer);
    RegistryContainer.getInstance().getRegistry().unsubscribe(consumerUrl, NotifyListenerAdapter.NOTIFY_LISTENER);
    String parameter;
    if (url.hasParameter("service")) {
        parameter = "service=" + url.getParameter("service");
    } else if (url.hasParameter("host")) {
        parameter = "host=" + url.getParameter("host");
    } else if (url.hasParameter("application")) {
        parameter = "application=" + url.getParameter("application");
    } else {
        parameter = "service=" + consumerUrl.getServiceInterface();
    }
    return new Page("<script type=\"text/javascript\">window.location.href=\"consumers.html?" + parameter + "\";</script>");
}
 
Example 10
Source File: UnregisterPageHandler.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Page handle(URL url) {
    String provider = url.getParameterAndDecoded("provider");
    if (provider == null || provider.length() == 0) {
        throw new IllegalArgumentException("Please input provider parameter.");
    }
    URL providerUrl = URL.valueOf(provider);
    RegistryContainer.getInstance().getRegistry().unregister(providerUrl);
    String parameter;
    if (url.hasParameter("service")) {
        parameter = "service=" + url.getParameter("service");
    } else if (url.hasParameter("host")) {
        parameter = "host=" + url.getParameter("host");
    } else if (url.hasParameter("application")) {
        parameter = "application=" + url.getParameter("application");
    } else {
        parameter = "service=" + providerUrl.getServiceInterface();
    }
    return new Page("<script type=\"text/javascript\">window.location.href=\"providers.html?" + parameter + "\";</script>");
}
 
Example 11
Source File: RpcInvocation.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public RpcInvocation(Invocation invocation, Invoker<?> invoker) {
    this(invocation.getMethodName(), invocation.getParameterTypes(), 
            invocation.getArguments(), new HashMap<String, String>(invocation.getAttachments()),
            invocation.getInvoker());
    if (invoker != null) {
        URL url = invoker.getUrl();
        setAttachment(Constants.PATH_KEY, url.getPath());
        if (url.hasParameter(Constants.INTERFACE_KEY)) {
            setAttachment(Constants.INTERFACE_KEY, url.getParameter(Constants.INTERFACE_KEY));
        }
        if (url.hasParameter(Constants.GROUP_KEY)) {
            setAttachment(Constants.GROUP_KEY, url.getParameter(Constants.GROUP_KEY));
        }
        if (url.hasParameter(Constants.VERSION_KEY)) {
            setAttachment(Constants.VERSION_KEY, url.getParameter(Constants.VERSION_KEY, "0.0.0"));
        }
        if (url.hasParameter(Constants.TIMEOUT_KEY)) {
            setAttachment(Constants.TIMEOUT_KEY, url.getParameter(Constants.TIMEOUT_KEY));
        }
        if (url.hasParameter(Constants.TOKEN_KEY)) {
            setAttachment(Constants.TOKEN_KEY, url.getParameter(Constants.TOKEN_KEY));
        }
        if (url.hasParameter(Constants.APPLICATION_KEY)) {
            setAttachment(Constants.APPLICATION_KEY, url.getParameter(Constants.APPLICATION_KEY));
        }
    }
}
 
Example 12
Source File: RpcInvocation.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
public RpcInvocation(Invocation invocation, Invoker<?> invoker) {
    this(invocation.getMethodName(), invocation.getParameterTypes(),
            invocation.getArguments(), new HashMap<String, String>(invocation.getAttachments()),
            invocation.getInvoker());
    if (invoker != null) {
        URL url = invoker.getUrl();
        setAttachment(Constants.PATH_KEY, url.getPath());
        if (url.hasParameter(Constants.INTERFACE_KEY)) {
            setAttachment(Constants.INTERFACE_KEY, url.getParameter(Constants.INTERFACE_KEY));
        }
        if (url.hasParameter(Constants.GROUP_KEY)) {
            setAttachment(Constants.GROUP_KEY, url.getParameter(Constants.GROUP_KEY));
        }
        if (url.hasParameter(Constants.VERSION_KEY)) {
            setAttachment(Constants.VERSION_KEY, url.getParameter(Constants.VERSION_KEY, "0.0.0"));
        }
        if (url.hasParameter(Constants.TIMEOUT_KEY)) {
            setAttachment(Constants.TIMEOUT_KEY, url.getParameter(Constants.TIMEOUT_KEY));
        }
        if (url.hasParameter(Constants.TOKEN_KEY)) {
            setAttachment(Constants.TOKEN_KEY, url.getParameter(Constants.TOKEN_KEY));
        }
        if (url.hasParameter(Constants.APPLICATION_KEY)) {
            setAttachment(Constants.APPLICATION_KEY, url.getParameter(Constants.APPLICATION_KEY));
        }
    }
}
 
Example 13
Source File: RpcInvocation.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public RpcInvocation(Invocation invocation, Invoker<?> invoker) {
    this(invocation.getMethodName(), invocation.getParameterTypes(), 
            invocation.getArguments(), new HashMap<String, String>(invocation.getAttachments()),
            invocation.getInvoker());
    if (invoker != null) {
        URL url = invoker.getUrl();
        setAttachment(Constants.PATH_KEY, url.getPath());
        if (url.hasParameter(Constants.INTERFACE_KEY)) {
            setAttachment(Constants.INTERFACE_KEY, url.getParameter(Constants.INTERFACE_KEY));
        }
        if (url.hasParameter(Constants.GROUP_KEY)) {
            setAttachment(Constants.GROUP_KEY, url.getParameter(Constants.GROUP_KEY));
        }
        if (url.hasParameter(Constants.VERSION_KEY)) {
            setAttachment(Constants.VERSION_KEY, url.getParameter(Constants.VERSION_KEY, "0.0.0"));
        }
        if (url.hasParameter(Constants.TIMEOUT_KEY)) {
            setAttachment(Constants.TIMEOUT_KEY, url.getParameter(Constants.TIMEOUT_KEY));
        }
        if (url.hasParameter(Constants.TOKEN_KEY)) {
            setAttachment(Constants.TOKEN_KEY, url.getParameter(Constants.TOKEN_KEY));
        }
        if (url.hasParameter(Constants.APPLICATION_KEY)) {
            setAttachment(Constants.APPLICATION_KEY, url.getParameter(Constants.APPLICATION_KEY));
        }
    }
}
 
Example 14
Source File: RpcInvocation.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
public RpcInvocation(Invocation invocation, Invoker<?> invoker) {
    this(invocation.getMethodName(), invocation.getParameterTypes(),
            invocation.getArguments(), new HashMap<>(invocation.getAttachments()),
            invocation.getInvoker());
    if (invoker != null) {
        URL url = invoker.getUrl();
        setAttachment(Constants.PATH_KEY, url.getPath());
        if (url.hasParameter(Constants.INTERFACE_KEY)) {
            setAttachment(Constants.INTERFACE_KEY, url.getParameter(Constants.INTERFACE_KEY));
        }
        if (url.hasParameter(Constants.GROUP_KEY)) {
            setAttachment(Constants.GROUP_KEY, url.getParameter(Constants.GROUP_KEY));
        }
        if (url.hasParameter(Constants.VERSION_KEY)) {
            setAttachment(Constants.VERSION_KEY, url.getParameter(Constants.VERSION_KEY, "0.0.0"));
        }
        if (url.hasParameter(Constants.TIMEOUT_KEY)) {
            setAttachment(Constants.TIMEOUT_KEY, url.getParameter(Constants.TIMEOUT_KEY));
        }
        if (url.hasParameter(Constants.TOKEN_KEY)) {
            setAttachment(Constants.TOKEN_KEY, url.getParameter(Constants.TOKEN_KEY));
        }
        if (url.hasParameter(Constants.APPLICATION_KEY)) {
            setAttachment(Constants.APPLICATION_KEY, url.getParameter(Constants.APPLICATION_KEY));
        }
    }
}
 
Example 15
Source File: RpcInvocation.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public RpcInvocation(Invocation invocation, Invoker<?> invoker) {
    this(invocation.getMethodName(), invocation.getParameterTypes(), 
            invocation.getArguments(), new HashMap<String, String>(invocation.getAttachments()),
            invocation.getInvoker());
    if (invoker != null) {
        URL url = invoker.getUrl();
        setAttachment(Constants.PATH_KEY, url.getPath());
        if (url.hasParameter(Constants.INTERFACE_KEY)) {
            setAttachment(Constants.INTERFACE_KEY, url.getParameter(Constants.INTERFACE_KEY));
        }
        if (url.hasParameter(Constants.GROUP_KEY)) {
            setAttachment(Constants.GROUP_KEY, url.getParameter(Constants.GROUP_KEY));
        }
        if (url.hasParameter(Constants.VERSION_KEY)) {
            setAttachment(Constants.VERSION_KEY, url.getParameter(Constants.VERSION_KEY, "0.0.0"));
        }
        if (url.hasParameter(Constants.TIMEOUT_KEY)) {
            setAttachment(Constants.TIMEOUT_KEY, url.getParameter(Constants.TIMEOUT_KEY));
        }
        if (url.hasParameter(Constants.TOKEN_KEY)) {
            setAttachment(Constants.TOKEN_KEY, url.getParameter(Constants.TOKEN_KEY));
        }
        if (url.hasParameter(Constants.APPLICATION_KEY)) {
            setAttachment(Constants.APPLICATION_KEY, url.getParameter(Constants.APPLICATION_KEY));
        }
    }
}
 
Example 16
Source File: RegistryDirectory.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
/**
 * 将urls转成invokers,如果url已经被refer过,不再重新引用。
 *
 * @param urls url list
 * @return invokers
 */
private Map<String, Invoker<T>> toInvokers(List<URL> urls) {
    Map<String, Invoker<T>> newUrlInvokerMap = new HashMap<>();
    if (urls == null || urls.size() == 0) {
        return newUrlInvokerMap;
    }
    Set<String> keys = new HashSet<>();
    String queryProtocols = this.queryMap.get(Constants.PROTOCOL_KEY);
    for (URL providerUrl : urls) {
        //如果reference端配置了protocol,则只选择匹配的protocol
        if (queryProtocols != null && queryProtocols.length() > 0) {
            boolean accept = false;
            String[] acceptProtocols = queryProtocols.split(",");
            for (String acceptProtocol : acceptProtocols) {
                if (providerUrl.getProtocol().equals(acceptProtocol)) {
                    accept = true;
                    break;
                }
            }
            if (!accept) {
                continue;
            }
        }
        if (Constants.EMPTY_PROTOCOL.equals(providerUrl.getProtocol())) {
            continue;
        }
        if (!ExtensionLoader.getExtensionLoader(Protocol.class).hasExtension(providerUrl.getProtocol())) {
            logger.error(new IllegalStateException("Unsupported protocol " + providerUrl.getProtocol() + " in notified url: " + providerUrl + " from registry " + getUrl().getAddress() + " to consumer " + NetUtils.getLocalHost()
                    + ", supported protocol: " + ExtensionLoader.getExtensionLoader(Protocol.class).getSupportedExtensions()));
            continue;
        }
        URL url = mergeUrl(providerUrl);

        String key = url.toFullString(); // URL参数是排序的
        if (keys.contains(key)) { // 重复URL
            continue;
        }
        keys.add(key);
        // 缓存key为没有合并消费端参数的URL,不管消费端如何合并参数,如果服务端URL发生变化,则重新refer
        Map<String, Invoker<T>> localUrlInvokerMap = this.urlInvokerMap; // local reference
        Invoker<T> invoker = localUrlInvokerMap == null ? null : localUrlInvokerMap.get(key);
        if (invoker == null) { // 缓存中没有,重新refer
            try {
                boolean enabled;
                if (url.hasParameter(Constants.DISABLED_KEY)) {
                    enabled = !url.getParameter(Constants.DISABLED_KEY, false);
                } else {
                    enabled = url.getParameter(Constants.ENABLED_KEY, true);
                }
                if (enabled) {
                    invoker = new InvokerDelegete<>(protocol.refer(serviceType, url), url, providerUrl);
                }
            } catch (Throwable t) {
                logger.error("Failed to refer invoker for interface:" + serviceType + ",url:(" + url + ")" + t.getMessage(), t);
            }
            if (invoker != null) { // 将新的引用放入缓存
                newUrlInvokerMap.put(key, invoker);
            }
        } else {
            newUrlInvokerMap.put(key, invoker);
        }
    }
    keys.clear();
    return newUrlInvokerMap;
}
 
Example 17
Source File: RegistryDirectory.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
/**
 * 将urls转成invokers,如果url已经被refer过,不再重新引用。
 * 
 * @param urls
 * @param overrides
 * @param query
 * @return invokers
 */
private Map<String, Invoker<T>> toInvokers(List<URL> urls) {
    Map<String, Invoker<T>> newUrlInvokerMap = new HashMap<String, Invoker<T>>();
    if(urls == null || urls.size() == 0){
        return newUrlInvokerMap;
    }
    Set<String> keys = new HashSet<String>();
    String queryProtocols = this.queryMap.get(Constants.PROTOCOL_KEY);
    for (URL providerUrl : urls) {
    	//如果reference端配置了protocol,则只选择匹配的protocol
    	if (queryProtocols != null && queryProtocols.length() >0) {
    		boolean accept = false;
    		String[] acceptProtocols = queryProtocols.split(",");
    		for (String acceptProtocol : acceptProtocols) {
    			if (providerUrl.getProtocol().equals(acceptProtocol)) {
    				accept = true;
    				break;
    			}
    		}
    		if (!accept) {
    			continue;
    		}
    	}
        if (Constants.EMPTY_PROTOCOL.equals(providerUrl.getProtocol())) {
            continue;
        }
        if (! ExtensionLoader.getExtensionLoader(Protocol.class).hasExtension(providerUrl.getProtocol())) {
            logger.error(new IllegalStateException("Unsupported protocol " + providerUrl.getProtocol() + " in notified url: " + providerUrl + " from registry " + getUrl().getAddress() + " to consumer " + NetUtils.getLocalHost() 
                    + ", supported protocol: "+ExtensionLoader.getExtensionLoader(Protocol.class).getSupportedExtensions()));
            continue;
        }
        URL url = mergeUrl(providerUrl);
        
        String key = url.toFullString(); // URL参数是排序的
        if (keys.contains(key)) { // 重复URL
            continue;
        }
        keys.add(key);
        // 缓存key为没有合并消费端参数的URL,不管消费端如何合并参数,如果服务端URL发生变化,则重新refer
        Map<String, Invoker<T>> localUrlInvokerMap = this.urlInvokerMap; // local reference
        Invoker<T> invoker = localUrlInvokerMap == null ? null : localUrlInvokerMap.get(key);
        if (invoker == null) { // 缓存中没有,重新refer
            try {
            	boolean enabled = true;
            	if (url.hasParameter(Constants.DISABLED_KEY)) {
            		enabled = ! url.getParameter(Constants.DISABLED_KEY, false);
            	} else {
            		enabled = url.getParameter(Constants.ENABLED_KEY, true);
            	}
            	if (enabled) {
            		invoker = new InvokerDelegete<T>(protocol.refer(serviceType, url), url, providerUrl);
            	}
            } catch (Throwable t) {
                logger.error("Failed to refer invoker for interface:"+serviceType+",url:("+url+")" + t.getMessage(), t);
            }
            if (invoker != null) { // 将新的引用放入缓存
                newUrlInvokerMap.put(key, invoker);
            }
        }else {
            newUrlInvokerMap.put(key, invoker);
        }
    }
    keys.clear();
    return newUrlInvokerMap;
}
 
Example 18
Source File: RegistryDirectory.java    From dubbox with Apache License 2.0 4 votes vote down vote up
/**
 * 将urls转成invokers,如果url已经被refer过,不再重新引用。
 * 
 * @param urls
 * @param overrides
 * @param query
 * @return invokers
 */
private Map<String, Invoker<T>> toInvokers(List<URL> urls) {
    Map<String, Invoker<T>> newUrlInvokerMap = new HashMap<String, Invoker<T>>();
    if(urls == null || urls.size() == 0){
        return newUrlInvokerMap;
    }
    Set<String> keys = new HashSet<String>();
    String queryProtocols = this.queryMap.get(Constants.PROTOCOL_KEY);
    for (URL providerUrl : urls) {
    	//如果reference端配置了protocol,则只选择匹配的protocol
    	if (queryProtocols != null && queryProtocols.length() >0) {
    		boolean accept = false;
    		String[] acceptProtocols = queryProtocols.split(",");
    		for (String acceptProtocol : acceptProtocols) {
    			if (providerUrl.getProtocol().equals(acceptProtocol)) {
    				accept = true;
    				break;
    			}
    		}
    		if (!accept) {
    			continue;
    		}
    	}
        if (Constants.EMPTY_PROTOCOL.equals(providerUrl.getProtocol())) {
            continue;
        }
        if (! ExtensionLoader.getExtensionLoader(Protocol.class).hasExtension(providerUrl.getProtocol())) {
            logger.error(new IllegalStateException("Unsupported protocol " + providerUrl.getProtocol() + " in notified url: " + providerUrl + " from registry " + getUrl().getAddress() + " to consumer " + NetUtils.getLocalHost() 
                    + ", supported protocol: "+ExtensionLoader.getExtensionLoader(Protocol.class).getSupportedExtensions()));
            continue;
        }
        URL url = mergeUrl(providerUrl);
        
        String key = url.toFullString(); // URL参数是排序的
        if (keys.contains(key)) { // 重复URL
            continue;
        }
        keys.add(key);
        // 缓存key为没有合并消费端参数的URL,不管消费端如何合并参数,如果服务端URL发生变化,则重新refer
        Map<String, Invoker<T>> localUrlInvokerMap = this.urlInvokerMap; // local reference
        Invoker<T> invoker = localUrlInvokerMap == null ? null : localUrlInvokerMap.get(key);
        if (invoker == null) { // 缓存中没有,重新refer
            try {
            	boolean enabled = true;
            	if (url.hasParameter(Constants.DISABLED_KEY)) {
            		enabled = ! url.getParameter(Constants.DISABLED_KEY, false);
            	} else {
            		enabled = url.getParameter(Constants.ENABLED_KEY, true);
            	}
            	if (enabled) {
            		invoker = new InvokerDelegete<T>(protocol.refer(serviceType, url), url, providerUrl);
            	}
            } catch (Throwable t) {
                logger.error("Failed to refer invoker for interface:"+serviceType+",url:("+url+")" + t.getMessage(), t);
            }
            if (invoker != null) { // 将新的引用放入缓存
                newUrlInvokerMap.put(key, invoker);
            }
        }else {
            newUrlInvokerMap.put(key, invoker);
        }
    }
    keys.clear();
    return newUrlInvokerMap;
}
 
Example 19
Source File: RegistryDirectory.java    From dubbox with Apache License 2.0 4 votes vote down vote up
/**
 * 将urls转成invokers,如果url已经被refer过,不再重新引用。
 * 
 * @param urls
 * @param overrides
 * @param query
 * @return invokers
 */
private Map<String, Invoker<T>> toInvokers(List<URL> urls) {
    Map<String, Invoker<T>> newUrlInvokerMap = new HashMap<String, Invoker<T>>();
    if(urls == null || urls.size() == 0){
        return newUrlInvokerMap;
    }
    Set<String> keys = new HashSet<String>();
    String queryProtocols = this.queryMap.get(Constants.PROTOCOL_KEY);
    for (URL providerUrl : urls) {
    	//如果reference端配置了protocol,则只选择匹配的protocol
    	if (queryProtocols != null && queryProtocols.length() >0) {
    		boolean accept = false;
    		String[] acceptProtocols = queryProtocols.split(",");
    		for (String acceptProtocol : acceptProtocols) {
    			if (providerUrl.getProtocol().equals(acceptProtocol)) {
    				accept = true;
    				break;
    			}
    		}
    		if (!accept) {
    			continue;
    		}
    	}
        if (Constants.EMPTY_PROTOCOL.equals(providerUrl.getProtocol())) {
            continue;
        }
        if (! ExtensionLoader.getExtensionLoader(Protocol.class).hasExtension(providerUrl.getProtocol())) {
            logger.error(new IllegalStateException("Unsupported protocol " + providerUrl.getProtocol() + " in notified url: " + providerUrl + " from registry " + getUrl().getAddress() + " to consumer " + NetUtils.getLocalHost() 
                    + ", supported protocol: "+ExtensionLoader.getExtensionLoader(Protocol.class).getSupportedExtensions()));
            continue;
        }
        URL url = mergeUrl(providerUrl);
        
        String key = url.toFullString(); // URL参数是排序的
        if (keys.contains(key)) { // 重复URL
            continue;
        }
        keys.add(key);
        // 缓存key为没有合并消费端参数的URL,不管消费端如何合并参数,如果服务端URL发生变化,则重新refer
        Map<String, Invoker<T>> localUrlInvokerMap = this.urlInvokerMap; // local reference
        Invoker<T> invoker = localUrlInvokerMap == null ? null : localUrlInvokerMap.get(key);
        if (invoker == null) { // 缓存中没有,重新refer
            try {
            	boolean enabled = true;
            	if (url.hasParameter(Constants.DISABLED_KEY)) {
            		enabled = ! url.getParameter(Constants.DISABLED_KEY, false);
            	} else {
            		enabled = url.getParameter(Constants.ENABLED_KEY, true);
            	}
            	if (enabled) {
            		invoker = new InvokerDelegete<T>(protocol.refer(serviceType, url), url, providerUrl);
            	}
            } catch (Throwable t) {
                logger.error("Failed to refer invoker for interface:"+serviceType+",url:("+url+")" + t.getMessage(), t);
            }
            if (invoker != null) { // 将新的引用放入缓存
                newUrlInvokerMap.put(key, invoker);
            }
        }else {
            newUrlInvokerMap.put(key, invoker);
        }
    }
    keys.clear();
    return newUrlInvokerMap;
}
 
Example 20
Source File: RegistryDirectory.java    From dubbo-2.6.5 with Apache License 2.0 4 votes vote down vote up
/**
     * Turn urls into invokers, and if url has been refer, will not re-reference. 将url转换为调用程序,如果url已被引用,则不会重新引用。
     *
     * @param urls
     * @return invokers
     */
    private Map<String, Invoker<T>> toInvokers(List<URL> urls) {
        Map<String, Invoker<T>> newUrlInvokerMap = new HashMap<String, Invoker<T>>();
        if (urls == null || urls.isEmpty()) {
            return newUrlInvokerMap;
        }
        Set<String> keys = new HashSet<String>();
        String queryProtocols = this.queryMap.get(Constants.PROTOCOL_KEY);
        for (URL providerUrl : urls) {
            // If protocol is configured at the reference side, only the matching protocol is selected 如果在引用端配置了协议,则只选择匹配的协议
            if (queryProtocols != null && queryProtocols.length() > 0) {
                boolean accept = false;
                String[] acceptProtocols = queryProtocols.split(",");
                for (String acceptProtocol : acceptProtocols) {
                    if (providerUrl.getProtocol().equals(acceptProtocol)) {
                        accept = true;
                        break;
                    }
                }
                if (!accept) {
                    continue;
                }
            }
            if (Constants.EMPTY_PROTOCOL.equals(providerUrl.getProtocol())) {
                continue;
            }
//            默认是dubbo协议
            if (!ExtensionLoader.getExtensionLoader(Protocol.class).hasExtension(providerUrl.getProtocol())) {
                logger.error(new IllegalStateException("Unsupported protocol " + providerUrl.getProtocol() + " in notified url: " + providerUrl + " from registry " + getUrl().getAddress() + " to consumer " + NetUtils.getLocalHost()
                        + ", supported protocol: " + ExtensionLoader.getExtensionLoader(Protocol.class).getSupportedExtensions()));
                continue;
            }
//            覆盖参数,默认是优先级系统变量配置、客户端、服务端
            URL url = mergeUrl(providerUrl);

            String key = url.toFullString(); // The parameter urls are sorted
            if (keys.contains(key)) { // Repeated url
                continue;
            }
            keys.add(key);
            // Cache key is url that does not merge with consumer side parameters, regardless of how the consumer combines parameters, if the server url changes, then refer again
            // 缓存键是不与使用者端参数合并的url,无论使用者如何组合参数,如果服务器url发生更改,则再次引用
            Map<String, Invoker<T>> localUrlInvokerMap = this.urlInvokerMap; // local reference
            Invoker<T> invoker = localUrlInvokerMap == null ? null : localUrlInvokerMap.get(key);
            if (invoker == null) { // Not in the cache, refer again
                try {
                    boolean enabled = true;
                    if (url.hasParameter(Constants.DISABLED_KEY)) {
                        enabled = !url.getParameter(Constants.DISABLED_KEY, false);
                    } else {
                        enabled = url.getParameter(Constants.ENABLED_KEY, true);
                    }
                    if (enabled) {
                        invoker = new InvokerDelegate<T>(protocol.refer(serviceType, url), url, providerUrl);
                    }
                } catch (Throwable t) {
                    logger.error("Failed to refer invoker for interface:" + serviceType + ",url:(" + url + ")" + t.getMessage(), t);
                }
                if (invoker != null) { // Put new invoker in cache
                    newUrlInvokerMap.put(key, invoker);
                }
            } else {
                newUrlInvokerMap.put(key, invoker);
            }
        }
        keys.clear();
        return newUrlInvokerMap;
    }