Search in sources :

Example 11 with RpcException

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

the class EtcdRegistry method doSubscribe.

@Override
public void doSubscribe(URL url, NotifyListener listener) {
    try {
        if (ANY_VALUE.equals(url.getServiceInterface())) {
            String root = toRootPath();
            /*
                 *  if we are interested in all interfaces,
                 *  find out the current container or create one for the url, put or get only once.
                 */
            ConcurrentMap<NotifyListener, ChildListener> listeners = Optional.ofNullable(etcdListeners.get(url)).orElseGet(() -> {
                ConcurrentMap<NotifyListener, ChildListener> container, prev;
                prev = etcdListeners.putIfAbsent(url, container = new ConcurrentHashMap<>());
                return prev != null ? prev : container;
            });
            /*
                 *  if we have no interface watcher listener,
                 *  find the current listener or create one for the current root, put or get only once.
                 */
            ChildListener interfaceListener = Optional.ofNullable(listeners.get(listener)).orElseGet(() -> {
                ChildListener childListener, prev;
                prev = listeners.putIfAbsent(listener, childListener = (parentPath, currentChildren) -> {
                    /*
                                         *  because etcd3 does not support direct children watch events,
                                         *  we should filter not interface events. if we watch /dubbo
                                         *  and /dubbo/interface, when we put a key-value pair {/dubbo/interface/hello hello},
                                         *  we will got events in watching path /dubbo.
                                         */
                    for (String child : currentChildren) {
                        child = URL.decode(child);
                        if (!anyServices.contains(child)) {
                            anyServices.add(child);
                            /*
                                                 *  if new interface event arrived, we watch their direct children,
                                                 *  eg: /dubbo/interface, /dubbo/interface and so on.
                                                 */
                            subscribe(url.setPath(child).addParameters(INTERFACE_KEY, child, CHECK_KEY, String.valueOf(false)), listener);
                        }
                    }
                });
                return prev != null ? prev : childListener;
            });
            etcdClient.create(root);
            /*
                 * at the first time, we want to pull already interface and then watch their direct children,
                 *  eg: /dubbo/interface, /dubbo/interface and so on.
                 */
            List<String> services = etcdClient.addChildListener(root, interfaceListener);
            for (String service : services) {
                service = URL.decode(service);
                anyServices.add(service);
                subscribe(url.setPath(service).addParameters(INTERFACE_KEY, service, CHECK_KEY, String.valueOf(false)), listener);
            }
        } else {
            List<URL> urls = new ArrayList<>();
            for (String path : toCategoriesPath(url)) {
                /*
                     *  if we are interested in special categories (providers, consumers, routers and so on),
                     *  we find out the current container or create one for the url, put or get only once.
                     */
                ConcurrentMap<NotifyListener, ChildListener> listeners = Optional.ofNullable(etcdListeners.get(url)).orElseGet(() -> {
                    ConcurrentMap<NotifyListener, ChildListener> container, prev;
                    prev = etcdListeners.putIfAbsent(url, container = new ConcurrentHashMap<>());
                    return prev != null ? prev : container;
                });
                /*
                     *  if we have no category watcher listener,
                     *  we find out the current listener or create one for the current category, put or get only once.
                     */
                ChildListener childListener = Optional.ofNullable(listeners.get(listener)).orElseGet(() -> {
                    ChildListener watchListener, prev;
                    prev = listeners.putIfAbsent(listener, watchListener = (parentPath, currentChildren) -> EtcdRegistry.this.notify(url, listener, toUrlsWithEmpty(url, parentPath, currentChildren)));
                    return prev != null ? prev : watchListener;
                });
                etcdClient.create(path);
                /*
                     * at the first time, we want to pull already category and then watch their direct children,
                     *  eg: /dubbo/interface/providers, /dubbo/interface/consumers and so on.
                     */
                List<String> children = etcdClient.addChildListener(path, childListener);
                if (children != null) {
                    urls.addAll(toUrlsWithEmpty(url, path, children));
                }
            }
            notify(url, listener, urls);
        }
    } catch (Throwable e) {
        throw new RpcException("Failed to subscribe " + url + " to etcd " + getUrl() + ", cause: " + (OptionUtil.isProtocolError(e) ? "etcd3 registry may not be supported yet or etcd3 registry is not available." : e.getMessage()), e);
    }
}
Also used : ChildListener(org.apache.dubbo.remoting.etcd.ChildListener) RpcException(org.apache.dubbo.rpc.RpcException) ArrayList(java.util.ArrayList) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener)

Example 12 with RpcException

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

the class EtcdRegistry method doUnregister.

@Override
public void doUnregister(URL url) {
    try {
        String path = toUrlPath(url);
        etcdClient.delete(path);
    } catch (Throwable e) {
        throw new RpcException("Failed to unregister " + url + " to etcd " + getUrl() + ", cause: " + e.getMessage(), e);
    }
}
Also used : RpcException(org.apache.dubbo.rpc.RpcException)

Example 13 with RpcException

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

the class ConsulRegistry method lookup.

@Override
public List<URL> lookup(URL url) {
    if (url == null) {
        throw new IllegalArgumentException("lookup url == null");
    }
    try {
        String service = url.getServiceKey();
        Response<List<HealthService>> result = getHealthServices(service, DEFAULT_INDEX, buildWatchTimeout(url));
        if (result == null || result.getValue() == null || result.getValue().isEmpty()) {
            return new ArrayList<>();
        } else {
            return convert(result.getValue(), url);
        }
    } catch (Throwable e) {
        throw new RpcException("Failed to lookup " + url + " from consul " + getUrl() + ", cause: " + e.getMessage(), e);
    }
}
Also used : RpcException(org.apache.dubbo.rpc.RpcException) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 14 with RpcException

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

the class EtcdServiceDiscovery method doUpdate.

@Override
public void doUpdate(ServiceInstance serviceInstance) {
    try {
        String path = toPath(serviceInstance);
        etcdClient.putEphemeral(path, new Gson().toJson(serviceInstance));
        services.add(serviceInstance.getServiceName());
    } catch (Throwable e) {
        throw new RpcException("Failed to register " + serviceInstance + " to etcd " + etcdClient.getUrl() + ", cause: " + (OptionUtil.isProtocolError(e) ? "etcd3 registry may not be supported yet or etcd3 registry is not available." : e.getMessage()), e);
    }
}
Also used : RpcException(org.apache.dubbo.rpc.RpcException) Gson(com.google.gson.Gson)

Example 15 with RpcException

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

the class EtcdServiceDiscovery method unregister.

@Override
public void unregister(ServiceInstance serviceInstance) throws RuntimeException {
    try {
        String path = toPath(serviceInstance);
        etcdClient.delete(path);
        services.remove(serviceInstance.getServiceName());
        this.serviceInstance = null;
    } catch (Throwable e) {
        throw new RpcException("Failed to unregister " + serviceInstance + " to etcd " + etcdClient.getUrl() + ", cause: " + e.getMessage(), e);
    }
}
Also used : RpcException(org.apache.dubbo.rpc.RpcException)

Aggregations

RpcException (org.apache.dubbo.rpc.RpcException)102 URL (org.apache.dubbo.common.URL)37 Test (org.junit.jupiter.api.Test)29 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)28 Result (org.apache.dubbo.rpc.Result)21 Invocation (org.apache.dubbo.rpc.Invocation)17 ArrayList (java.util.ArrayList)15 AppResponse (org.apache.dubbo.rpc.AppResponse)13 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)13 Invoker (org.apache.dubbo.rpc.Invoker)13 IOException (java.io.IOException)9 List (java.util.List)9 Method (java.lang.reflect.Method)8 Gson (com.google.gson.Gson)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 HashMap (java.util.HashMap)5 RemotingException (org.apache.dubbo.remoting.RemotingException)5 TException (org.apache.thrift.TException)5 SocketTimeoutException (java.net.SocketTimeoutException)4 CountDownLatch (java.util.concurrent.CountDownLatch)4