Search in sources :

Example 1 with StaticDirectory

use of com.alibaba.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) {
    URL tmpUrl = new URL("temp", "localhost", 0, map);
    final boolean isJvmRefer;
    if (isInjvm() == null) {
        if (url != null && url.length() > 0) {
            // if a url is specified, don't do local reference
            isJvmRefer = false;
        } else if (InjvmProtocol.getInjvmProtocol().isInjvmRefer(tmpUrl)) {
            // by default, reference local service if there is
            isJvmRefer = true;
        } else {
            isJvmRefer = false;
        }
    } else {
        isJvmRefer = isInjvm().booleanValue();
    }
    if (isJvmRefer) {
        URL url = new URL(Constants.LOCAL_PROTOCOL, NetUtils.LOCALHOST, 0, interfaceClass.getName()).addParameters(map);
        invoker = refprotocol.refer(interfaceClass, url);
        if (logger.isInfoEnabled()) {
            logger.info("Using injvm service " + interfaceClass.getName());
        }
    } else {
        if (url != null && url.length() > 0) {
            // user specified URL, could be peer-to-peer address, or register center's address.
            String[] us = Constants.SEMICOLON_SPLIT_PATTERN.split(url);
            if (us != null && us.length > 0) {
                for (String u : us) {
                    URL url = URL.valueOf(u);
                    if (url.getPath() == null || url.getPath().length() == 0) {
                        url = url.setPath(interfaceName);
                    }
                    if (Constants.REGISTRY_PROTOCOL.equals(url.getProtocol())) {
                        urls.add(url.addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map)));
                    } else {
                        urls.add(ClusterUtils.mergeUrl(url, map));
                    }
                }
            }
        } else {
            // assemble URL from register center's configuration
            List<URL> us = loadRegistries(false);
            if (us != null && !us.isEmpty()) {
                for (URL u : us) {
                    URL monitorUrl = loadMonitor(u);
                    if (monitorUrl != null) {
                        map.put(Constants.MONITOR_KEY, URL.encode(monitorUrl.toFullString()));
                    }
                    urls.add(u.addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map)));
                }
            }
            if (urls == null || 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 = refprotocol.refer(interfaceClass, urls.get(0));
        } else {
            List<Invoker<?>> invokers = new ArrayList<Invoker<?>>();
            URL registryURL = null;
            for (URL url : urls) {
                invokers.add(refprotocol.refer(interfaceClass, url));
                if (Constants.REGISTRY_PROTOCOL.equals(url.getProtocol())) {
                    // use last registry url
                    registryURL = url;
                }
            }
            if (registryURL != null) {
                // registry url is available
                // use AvailableCluster only when register's cluster is available
                URL u = registryURL.addParameter(Constants.CLUSTER_KEY, AvailableCluster.NAME);
                invoker = cluster.join(new StaticDirectory(u, invokers));
            } else {
                // not a registry url
                invoker = cluster.join(new StaticDirectory(invokers));
            }
        }
    }
    Boolean c = check;
    if (c == null && consumer != null) {
        c = consumer.isCheck();
    }
    if (c == null) {
        // default true
        c = true;
    }
    if (c && !invoker.isAvailable()) {
        throw new IllegalStateException("Failed to check the status of the service " + interfaceName + ". No provider available for the service " + (group == null ? "" : group + "/") + interfaceName + (version == null ? "" : ":" + version) + " from the url " + invoker.getUrl() + " to the consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion());
    }
    if (logger.isInfoEnabled()) {
        logger.info("Refer dubbo service " + interfaceClass.getName() + " from url " + invoker.getUrl());
    }
    // create service proxy
    return (T) proxyFactory.getProxy(invoker);
}
Also used : StaticDirectory(com.alibaba.dubbo.rpc.cluster.directory.StaticDirectory) Invoker(com.alibaba.dubbo.rpc.Invoker) ArrayList(java.util.ArrayList) URL(com.alibaba.dubbo.common.URL)

Example 2 with StaticDirectory

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

the class MockClusterInvokerTest method getClusterInvoker.

@SuppressWarnings({ "unchecked", "rawtypes" })
private Invoker<IHelloService> getClusterInvoker(URL url) {
    // 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);
    Directory<IHelloService> dic = new StaticDirectory<IHelloService>(durl, invokers, null);
    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 : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Invocation(com.alibaba.dubbo.rpc.Invocation) ProxyFactory(com.alibaba.dubbo.rpc.ProxyFactory) AbstractClusterInvoker(com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker) URL(com.alibaba.dubbo.common.URL) StaticDirectory(com.alibaba.dubbo.rpc.cluster.directory.StaticDirectory) Invoker(com.alibaba.dubbo.rpc.Invoker) AbstractClusterInvoker(com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker) LoadBalance(com.alibaba.dubbo.rpc.cluster.LoadBalance) RpcException(com.alibaba.dubbo.rpc.RpcException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with StaticDirectory

use of com.alibaba.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>() {

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

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

        public boolean isAvailable() {
            return false;
        }

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

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

Aggregations

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