Search in sources :

Example 56 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class AbstractLoadBalanceTest method testGetRegistryWeight.

@Test
public void testGetRegistryWeight() {
    RpcInvocation invocation = new RpcInvocation();
    invocation.setMethodName("say");
    Invoker invoker1 = mock(Invoker.class, Mockito.withSettings().stubOnly());
    URL url1 = new URL("", "", 0, "DemoService", new HashMap<>());
    url1 = url1.addParameter(REGISTRY_KEY + "." + WEIGHT_KEY, 10);
    given(invoker1.getUrl()).willReturn(url1);
    Invoker invoker2 = mock(Invoker.class, Mockito.withSettings().stubOnly());
    URL url2 = new URL("", "", 0, "org.apache.dubbo.registry.RegistryService", new HashMap<>());
    url2 = url2.addParameter(REGISTRY_KEY + "." + WEIGHT_KEY, 20);
    given(invoker2.getUrl()).willReturn(url2);
    Assertions.assertEquals(100, balance.getWeight(invoker1, invocation));
    Assertions.assertEquals(20, balance.getWeight(invoker2, invocation));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 57 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class LeastActiveBalanceTest method testSelectByWeight.

@Test
public void testSelectByWeight() {
    int sumInvoker1 = 0;
    int sumInvoker2 = 0;
    int loop = 10000;
    LeastActiveLoadBalance lb = new LeastActiveLoadBalance();
    for (int i = 0; i < loop; i++) {
        Invoker selected = lb.select(weightInvokers, null, weightTestInvocation);
        if (selected.getUrl().getProtocol().equals("test1")) {
            sumInvoker1++;
        }
        if (selected.getUrl().getProtocol().equals("test2")) {
            sumInvoker2++;
        }
        // never select invoker3 because it's active is more than invoker1 and invoker2
        Assertions.assertTrue(!selected.getUrl().getProtocol().equals("test3"), "select is not the least active one");
    }
    // the sumInvoker1 : sumInvoker2 approximately equal to 1: 9
    System.out.println(sumInvoker1);
    System.out.println(sumInvoker2);
    Assertions.assertEquals(sumInvoker1 + sumInvoker2, loop, "select failed!");
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Test(org.junit.jupiter.api.Test)

Example 58 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class RandomLoadBalanceTest method testRandomLoadBalanceSelect.

@Test
public void testRandomLoadBalanceSelect() {
    int runs = 1000;
    Map<Invoker, AtomicLong> counter = getInvokeCounter(runs, RandomLoadBalance.NAME);
    for (Map.Entry<Invoker, AtomicLong> entry : counter.entrySet()) {
        Long count = entry.getValue().get();
        Assertions.assertTrue(Math.abs(count - runs / (0f + invokers.size())) < runs / (0f + invokers.size()), "abs diff should < avg");
    }
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j <= i; j++) {
            RpcStatus.beginCount(invokers.get(i).getUrl(), invocation.getMethodName());
        }
    }
    counter = getInvokeCounter(runs, LeastActiveLoadBalance.NAME);
    for (Map.Entry<Invoker, AtomicLong> entry : counter.entrySet()) {
        Long count = entry.getValue().get();
    }
    Assertions.assertEquals(runs, counter.get(invoker1).intValue());
    Assertions.assertEquals(0, counter.get(invoker2).intValue());
    Assertions.assertEquals(0, counter.get(invoker3).intValue());
    Assertions.assertEquals(0, counter.get(invoker4).intValue());
    Assertions.assertEquals(0, counter.get(invoker5).intValue());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Invoker(org.apache.dubbo.rpc.Invoker) AtomicLong(java.util.concurrent.atomic.AtomicLong) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 59 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class ReferenceConfig method createProxy.

@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
private T createProxy(Map<String, String> map) {
    if (shouldJvmRefer(map)) {
        URL url = new URL(LOCAL_PROTOCOL, LOCALHOST_VALUE, 0, interfaceClass.getName()).addParameters(map);
        invoker = REF_PROTOCOL.refer(interfaceClass, url);
        if (logger.isInfoEnabled()) {
            logger.info("Using injvm service " + interfaceClass.getName());
        }
    } else {
        urls.clear();
        if (url != null && url.length() > 0) {
            // user specified URL, could be peer-to-peer address, or register center's address.
            String[] us = SEMICOLON_SPLIT_PATTERN.split(url);
            if (us != null && us.length > 0) {
                for (String u : us) {
                    URL url = URL.valueOf(u);
                    if (StringUtils.isEmpty(url.getPath())) {
                        url = url.setPath(interfaceName);
                    }
                    if (UrlUtils.isRegistry(url)) {
                        urls.add(url.addParameterAndEncoded(REFER_KEY, StringUtils.toQueryString(map)));
                    } else {
                        urls.add(ClusterUtils.mergeUrl(url, map));
                    }
                }
            }
        } else {
            // if protocols not injvm checkRegistry
            if (!LOCAL_PROTOCOL.equalsIgnoreCase(getProtocol())) {
                checkRegistry();
                List<URL> us = ConfigValidationUtils.loadRegistries(this, false);
                if (CollectionUtils.isNotEmpty(us)) {
                    for (URL u : us) {
                        URL monitorUrl = ConfigValidationUtils.loadMonitor(this, u);
                        if (monitorUrl != null) {
                            map.put(MONITOR_KEY, URL.encode(monitorUrl.toFullString()));
                        }
                        urls.add(u.addParameterAndEncoded(REFER_KEY, StringUtils.toQueryString(map)));
                    }
                }
                if (urls.isEmpty()) {
                    throw new IllegalStateException("No such any registry to reference " + interfaceName + " on the consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", please config <dubbo:registry address=\"...\" /> to your spring config.");
                }
            }
        }
        if (urls.size() == 1) {
            invoker = REF_PROTOCOL.refer(interfaceClass, urls.get(0));
        } else {
            List<Invoker<?>> invokers = new ArrayList<Invoker<?>>();
            URL registryURL = null;
            for (URL url : urls) {
                // For multi-registry scenarios, it is not checked whether each referInvoker is available.
                // Because this invoker may become available later.
                invokers.add(REF_PROTOCOL.refer(interfaceClass, url));
                if (UrlUtils.isRegistry(url)) {
                    // use last registry url
                    registryURL = url;
                }
            }
            if (registryURL != null) {
                // registry url is available
                // for multi-subscription scenario, use 'zone-aware' policy by default
                String cluster = registryURL.getParameter(CLUSTER_KEY, ZoneAwareCluster.NAME);
                // The invoker wrap sequence would be: ZoneAwareClusterInvoker(StaticDirectory) -> FailoverClusterInvoker(RegistryDirectory, routing happens here) -> Invoker
                invoker = Cluster.getCluster(cluster, false).join(new StaticDirectory(registryURL, invokers));
            } else {
                // not a registry url, must be direct invoke.
                String cluster = CollectionUtils.isNotEmpty(invokers) ? (invokers.get(0).getUrl() != null ? invokers.get(0).getUrl().getParameter(CLUSTER_KEY, ZoneAwareCluster.NAME) : Cluster.DEFAULT) : Cluster.DEFAULT;
                invoker = Cluster.getCluster(cluster).join(new StaticDirectory(invokers));
            }
        }
    }
    if (logger.isInfoEnabled()) {
        logger.info("Refer dubbo service " + interfaceClass.getName() + " from url " + invoker.getUrl());
    }
    URL consumerURL = new URL(CONSUMER_PROTOCOL, map.remove(REGISTER_IP_KEY), 0, map.get(INTERFACE_KEY), map);
    MetadataUtils.publishServiceDefinition(consumerURL);
    // create service proxy
    return (T) PROXY_FACTORY.getProxy(invoker, ProtocolUtils.isGeneric(generic));
}
Also used : StaticDirectory(org.apache.dubbo.rpc.cluster.directory.StaticDirectory) Invoker(org.apache.dubbo.rpc.Invoker) ArrayList(java.util.ArrayList) URL(org.apache.dubbo.common.URL)

Example 60 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class GenericServiceTest method testGenericCompatible.

@Test
public void testGenericCompatible() {
    DemoService server = new DemoServiceImpl();
    ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
    Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
    URL url = URL.valueOf("dubbo://127.0.0.1:5342/" + DemoService.class.getName() + "?version=1.0.0&generic=true$timeout=3000");
    Exporter<DemoService> exporter = protocol.export(proxyFactory.getInvoker(server, DemoService.class, url));
    // simulate normal invoke
    ReferenceConfig<com.alibaba.dubbo.rpc.service.GenericService> oldReferenceConfig = new ReferenceConfig<>();
    oldReferenceConfig.setGeneric(true);
    oldReferenceConfig.setInterface(DemoService.class.getName());
    oldReferenceConfig.checkAndUpdateSubConfigs();
    Invoker invoker = protocol.refer(oldReferenceConfig.getInterfaceClass(), url);
    com.alibaba.dubbo.rpc.service.GenericService client = (com.alibaba.dubbo.rpc.service.GenericService) proxyFactory.getProxy(invoker, true);
    Object result = client.$invoke("sayHello", new String[] { "java.lang.String" }, new Object[] { "haha" });
    Assertions.assertEquals("hello haha", result);
    invoker.destroy();
    exporter.unexport();
}
Also used : GenericService(org.apache.dubbo.rpc.service.GenericService) ProxyFactory(org.apache.dubbo.rpc.ProxyFactory) DemoService(org.apache.dubbo.service.DemoService) URL(org.apache.dubbo.common.URL) Invoker(org.apache.dubbo.rpc.Invoker) ReferenceConfig(com.alibaba.dubbo.config.ReferenceConfig) ComplexObject(org.apache.dubbo.service.ComplexObject) Protocol(org.apache.dubbo.rpc.Protocol) DemoServiceImpl(org.apache.dubbo.service.DemoServiceImpl) Test(org.junit.jupiter.api.Test)

Aggregations

Invoker (org.apache.dubbo.rpc.Invoker)128 Test (org.junit.jupiter.api.Test)104 URL (org.apache.dubbo.common.URL)74 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)66 ArrayList (java.util.ArrayList)48 Invocation (org.apache.dubbo.rpc.Invocation)42 Result (org.apache.dubbo.rpc.Result)26 AppResponse (org.apache.dubbo.rpc.AppResponse)21 MockClusterInvoker (org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker)21 RegistryDirectory (org.apache.dubbo.registry.integration.RegistryDirectory)20 Router (org.apache.dubbo.rpc.cluster.Router)18 MockInvoker (org.apache.dubbo.rpc.cluster.router.MockInvoker)18 HashMap (java.util.HashMap)13 LoadBalance (org.apache.dubbo.rpc.cluster.LoadBalance)13 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)12 RpcException (org.apache.dubbo.rpc.RpcException)12 LeastActiveLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance)8 RandomLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance)8 RoundRobinLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8