Search in sources :

Example 56 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class RegistryDirectoryTest method test_Notified_acceptProtocol0.

// mock protocol
// Test the matching of protocol and select only the matched protocol for refer
@Test
public void test_Notified_acceptProtocol0() {
    URL errorPathUrl = URL.valueOf("notsupport:/xxx?refer=" + URL.encode("interface=" + service));
    RegistryDirectory registryDirectory = getRegistryDirectory(errorPathUrl);
    List<URL> serviceUrls = new ArrayList<URL>();
    URL dubbo1URL = URL.valueOf("dubbo://127.0.0.1:9098?lazy=true&methods=getXXX");
    URL dubbo2URL = URL.valueOf("injvm://127.0.0.1:9099?lazy=true&methods=getXXX");
    serviceUrls.add(dubbo1URL);
    serviceUrls.add(dubbo2URL);
    registryDirectory.notify(serviceUrls);
    invocation = new RpcInvocation();
    List<Invoker<DemoService>> invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());
}
Also used : RegistryDirectory(com.alibaba.dubbo.registry.integration.RegistryDirectory) RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Invoker(com.alibaba.dubbo.rpc.Invoker) ArrayList(java.util.ArrayList) URL(com.alibaba.dubbo.common.URL) Test(org.junit.Test)

Example 57 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class RegistryDirectory method toInvokers.

/**
 * Turn urls into invokers, and if url has been refer, will not re-reference.
 *
 * @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;
        }
        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);
        // The parameter urls are sorted
        String key = url.toFullString();
        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
        // local reference
        Map<String, Invoker<T>> localUrlInvokerMap = this.urlInvokerMap;
        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;
}
Also used : HashMap(java.util.HashMap) URL(com.alibaba.dubbo.common.URL) Invoker(com.alibaba.dubbo.rpc.Invoker) HashSet(java.util.HashSet)

Example 58 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class RegistryProtocol method doRefer.

private <T> Invoker<T> doRefer(Cluster cluster, Registry registry, Class<T> type, URL url) {
    RegistryDirectory<T> directory = new RegistryDirectory<T>(type, url);
    directory.setRegistry(registry);
    directory.setProtocol(protocol);
    // all attributes of REFER_KEY
    Map<String, String> parameters = new HashMap<String, String>(directory.getUrl().getParameters());
    URL subscribeUrl = new URL(Constants.CONSUMER_PROTOCOL, parameters.remove(Constants.REGISTER_IP_KEY), 0, type.getName(), parameters);
    if (!Constants.ANY_VALUE.equals(url.getServiceInterface()) && url.getParameter(Constants.REGISTER_KEY, true)) {
        registry.register(subscribeUrl.addParameters(Constants.CATEGORY_KEY, Constants.CONSUMERS_CATEGORY, Constants.CHECK_KEY, String.valueOf(false)));
    }
    directory.subscribe(subscribeUrl.addParameter(Constants.CATEGORY_KEY, Constants.PROVIDERS_CATEGORY + "," + Constants.CONFIGURATORS_CATEGORY + "," + Constants.ROUTERS_CATEGORY));
    Invoker invoker = cluster.join(directory);
    ProviderConsumerRegTable.registerConsumer(invoker, url, subscribeUrl, directory);
    return invoker;
}
Also used : QOS_PORT(com.alibaba.dubbo.common.Constants.QOS_PORT) Invoker(com.alibaba.dubbo.rpc.Invoker) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(com.alibaba.dubbo.common.URL)

Example 59 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class ProviderConsumerRegTable method getProviderWrapper.

public static ProviderInvokerWrapper getProviderWrapper(Invoker invoker) {
    URL providerUrl = invoker.getUrl();
    if (Constants.REGISTRY_PROTOCOL.equals(providerUrl.getProtocol())) {
        providerUrl = URL.valueOf(providerUrl.getParameterAndDecoded(Constants.EXPORT_KEY));
    }
    String serviceUniqueName = providerUrl.getServiceKey();
    Set<ProviderInvokerWrapper> invokers = providerInvokers.get(serviceUniqueName);
    if (invokers == null) {
        return null;
    }
    for (ProviderInvokerWrapper providerWrapper : invokers) {
        Invoker providerInvoker = providerWrapper.getInvoker();
        if (providerInvoker == invoker) {
            return providerWrapper;
        }
    }
    return null;
}
Also used : Invoker(com.alibaba.dubbo.rpc.Invoker) URL(com.alibaba.dubbo.common.URL)

Example 60 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class RegistryDirectoryTest method testNotifyoverrideUrls_Nouse.

/**
 * Test whether the override rule have a high priority
 * Scene: the rules of the push are the same as the parameters of the provider
 * Expectation: no need to be re-referenced
 */
@Test
public void testNotifyoverrideUrls_Nouse() {
    RegistryDirectory registryDirectory = getRegistryDirectory();
    invocation = new RpcInvocation();
    List<URL> durls = new ArrayList<URL>();
    // One is the same, one is different
    durls.add(SERVICEURL.addParameter("timeout", "1"));
    durls.add(SERVICEURL2.addParameter("timeout", "1").addParameter("connections", "5"));
    registryDirectory.notify(durls);
    List<Invoker<?>> invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());
    Invoker<?> a1Invoker = invokers.get(0);
    Invoker<?> b1Invoker = invokers.get(1);
    durls = new ArrayList<URL>();
    durls.add(URL.valueOf("override://0.0.0.0?timeout=1&connections=5"));
    registryDirectory.notify(durls);
    Assert.assertEquals(true, registryDirectory.isAvailable());
    invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());
    Invoker<?> a2Invoker = invokers.get(0);
    Invoker<?> b2Invoker = invokers.get(1);
    // The parameters are different and must be rereferenced.
    Assert.assertFalse("object not same", a1Invoker == a2Invoker);
    // The parameters can not be rereferenced
    Assert.assertTrue("object same", b1Invoker == b2Invoker);
}
Also used : RegistryDirectory(com.alibaba.dubbo.registry.integration.RegistryDirectory) RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Invoker(com.alibaba.dubbo.rpc.Invoker) ArrayList(java.util.ArrayList) URL(com.alibaba.dubbo.common.URL) Test(org.junit.Test)

Aggregations

Invoker (com.alibaba.dubbo.rpc.Invoker)67 Test (org.junit.Test)50 ArrayList (java.util.ArrayList)39 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)38 URL (com.alibaba.dubbo.common.URL)34 RegistryDirectory (com.alibaba.dubbo.registry.integration.RegistryDirectory)19 Router (com.alibaba.dubbo.rpc.cluster.Router)14 MockInvoker (com.alibaba.dubbo.rpc.cluster.router.MockInvoker)13 Result (com.alibaba.dubbo.rpc.Result)11 Invocation (com.alibaba.dubbo.rpc.Invocation)10 RpcException (com.alibaba.dubbo.rpc.RpcException)9 RpcResult (com.alibaba.dubbo.rpc.RpcResult)9 LoadBalance (com.alibaba.dubbo.rpc.cluster.LoadBalance)8 DemoService (com.alibaba.dubbo.rpc.support.DemoService)7 LeastActiveLoadBalance (com.alibaba.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance)5 RandomLoadBalance (com.alibaba.dubbo.rpc.cluster.loadbalance.RandomLoadBalance)5 RoundRobinLoadBalance (com.alibaba.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 List (java.util.List)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4