Search in sources :

Example 1 with StaticDirectory

use of org.apache.dubbo.rpc.cluster.directory.StaticDirectory 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 2 with StaticDirectory

use of org.apache.dubbo.rpc.cluster.directory.StaticDirectory in project dubbo by alibaba.

the class AbstractClusterInvokerTest method testTimeoutExceptionCode.

@Test()
public void testTimeoutExceptionCode() {
    List<Invoker<DemoService>> invokers = new ArrayList<Invoker<DemoService>>();
    invokers.add(new Invoker<DemoService>() {

        @Override
        public Class<DemoService> getInterface() {
            return DemoService.class;
        }

        public URL getUrl() {
            return URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/" + DemoService.class.getName());
        }

        @Override
        public boolean isAvailable() {
            return false;
        }

        @Override
        public Result invoke(Invocation invocation) throws RpcException {
            throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "test timeout");
        }

        @Override
        public void destroy() {
        }
    });
    Directory<DemoService> directory = new StaticDirectory<DemoService>(invokers);
    FailoverClusterInvoker<DemoService> failoverClusterInvoker = new FailoverClusterInvoker<DemoService>(directory);
    try {
        failoverClusterInvoker.invoke(new RpcInvocation("sayHello", DemoService.class.getName(), "", new Class<?>[0], new Object[0]));
        Assertions.fail();
    } catch (RpcException e) {
        Assertions.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
    }
    ForkingClusterInvoker<DemoService> forkingClusterInvoker = new ForkingClusterInvoker<DemoService>(directory);
    try {
        forkingClusterInvoker.invoke(new RpcInvocation("sayHello", DemoService.class.getName(), "", new Class<?>[0], new Object[0]));
        Assertions.fail();
    } catch (RpcException e) {
        Assertions.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
    }
    FailfastClusterInvoker<DemoService> failfastClusterInvoker = new FailfastClusterInvoker<DemoService>(directory);
    try {
        failfastClusterInvoker.invoke(new RpcInvocation("sayHello", DemoService.class.getName(), "", new Class<?>[0], new Object[0]));
        Assertions.fail();
    } catch (RpcException e) {
        Assertions.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
    }
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) ArrayList(java.util.ArrayList) DemoService(org.apache.dubbo.rpc.cluster.filter.DemoService) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) StaticDirectory(org.apache.dubbo.rpc.cluster.directory.StaticDirectory) Invoker(org.apache.dubbo.rpc.Invoker) RpcException(org.apache.dubbo.rpc.RpcException) Test(org.junit.jupiter.api.Test)

Example 3 with StaticDirectory

use of org.apache.dubbo.rpc.cluster.directory.StaticDirectory in project dubbo by alibaba.

the class MockClusterInvokerTest method getClusterInvokerMock.

private Invoker<IHelloService> getClusterInvokerMock(URL url, Invoker<IHelloService> mockInvoker) {
    // As `javassist` have a strict restriction of argument types, request will fail if Invocation do not contains complete parameter type information
    final URL durl = url.addParameter("proxy", "jdk");
    invokers.clear();
    ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getExtension("jdk");
    Invoker<IHelloService> invoker1 = proxy.getInvoker(new HelloService(), IHelloService.class, durl);
    invokers.add(invoker1);
    if (mockInvoker != null) {
        invokers.add(mockInvoker);
    }
    StaticDirectory<IHelloService> dic = new StaticDirectory<IHelloService>(durl, invokers, null);
    dic.buildRouterChain();
    AbstractClusterInvoker<IHelloService> cluster = new AbstractClusterInvoker(dic) {

        @Override
        protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance) throws RpcException {
            if (durl.getParameter("invoke_return_error", false)) {
                throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "test rpc exception");
            } else {
                return ((Invoker<?>) invokers.get(0)).invoke(invocation);
            }
        }
    };
    return new MockClusterInvoker<IHelloService>(dic, cluster);
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) ProxyFactory(org.apache.dubbo.rpc.ProxyFactory) AbstractClusterInvoker(org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker) URL(org.apache.dubbo.common.URL) StaticDirectory(org.apache.dubbo.rpc.cluster.directory.StaticDirectory) Invoker(org.apache.dubbo.rpc.Invoker) AbstractClusterInvoker(org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker) LoadBalance(org.apache.dubbo.rpc.cluster.LoadBalance) RpcException(org.apache.dubbo.rpc.RpcException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with StaticDirectory

use of org.apache.dubbo.rpc.cluster.directory.StaticDirectory in project dubbo by alibaba.

the class MockProviderRpcExceptionTest method getClusterInvokerMock.

private Invoker<IHelloRpcService> getClusterInvokerMock(URL url, Invoker<IHelloRpcService> mockInvoker) {
    // As `javassist` have a strict restriction of argument types, request will fail if Invocation do not contains complete parameter type information
    final URL durl = url.addParameter("proxy", "jdk");
    invokers.clear();
    ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getExtension("jdk");
    Invoker<IHelloRpcService> invoker1 = proxy.getInvoker(new HelloRpcService(), IHelloRpcService.class, durl);
    invokers.add(invoker1);
    if (mockInvoker != null) {
        invokers.add(mockInvoker);
    }
    StaticDirectory<IHelloRpcService> dic = new StaticDirectory<IHelloRpcService>(durl, invokers, null);
    dic.buildRouterChain();
    AbstractClusterInvoker<IHelloRpcService> cluster = new AbstractClusterInvoker(dic) {

        @Override
        protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance) throws RpcException {
            if (durl.getParameter("invoke_return_error", false)) {
                throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "test rpc exception ");
            } else {
                return ((Invoker<?>) invokers.get(0)).invoke(invocation);
            }
        }
    };
    return new MockClusterInvoker<IHelloRpcService>(dic, cluster);
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) ProxyFactory(org.apache.dubbo.rpc.ProxyFactory) AbstractClusterInvoker(org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker) URL(org.apache.dubbo.common.URL) StaticDirectory(org.apache.dubbo.rpc.cluster.directory.StaticDirectory) Invoker(org.apache.dubbo.rpc.Invoker) AbstractClusterInvoker(org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker) LoadBalance(org.apache.dubbo.rpc.cluster.LoadBalance) RpcException(org.apache.dubbo.rpc.RpcException) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ArrayList (java.util.ArrayList)4 URL (org.apache.dubbo.common.URL)4 Invoker (org.apache.dubbo.rpc.Invoker)4 StaticDirectory (org.apache.dubbo.rpc.cluster.directory.StaticDirectory)4 Invocation (org.apache.dubbo.rpc.Invocation)3 RpcException (org.apache.dubbo.rpc.RpcException)3 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)3 List (java.util.List)2 ProxyFactory (org.apache.dubbo.rpc.ProxyFactory)2 LoadBalance (org.apache.dubbo.rpc.cluster.LoadBalance)2 AbstractClusterInvoker (org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker)2 Result (org.apache.dubbo.rpc.Result)1 DemoService (org.apache.dubbo.rpc.cluster.filter.DemoService)1 Test (org.junit.jupiter.api.Test)1