Search in sources :

Example 16 with ServiceInstance

use of com.baidu.brpc.client.channel.ServiceInstance in project brpc-java by baidu.

the class DubboZookeeperNamingService method doSubscribe.

@Override
public void doSubscribe(SubscribeInfo subscribeInfo, final NotifyListener listener) throws Exception {
    final String path = buildParentNodePath(subscribeInfo.getGroup(), subscribeInfo.getInterfaceName(), subscribeInfo.getVersion());
    PathChildrenCache cache = new PathChildrenCache(client, path, true);
    cache.getListenable().addListener(new PathChildrenCacheListener() {

        @Override
        public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
            ChildData data = event.getData();
            // 子节点信息,将监听的父节点信息擦除
            String childNodePath = data.getPath().replace(path + "/", "");
            String providerUrlString = URLDecoder.decode(childNodePath, "UTF-8");
            BrpcURL url = new BrpcURL(providerUrlString);
            ServiceInstance instance = new ServiceInstance(url.getHostPorts());
            switch(event.getType()) {
                case CHILD_ADDED:
                    {
                        listener.notify(Collections.singletonList(instance), Collections.<ServiceInstance>emptyList());
                        break;
                    }
                case CHILD_REMOVED:
                    {
                        listener.notify(Collections.<ServiceInstance>emptyList(), Collections.singletonList(instance));
                        break;
                    }
                case CHILD_UPDATED:
                default:
                    break;
            }
        }
    });
    cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
    subscribeCacheMap.putIfAbsent(subscribeInfo, cache);
    log.info("dubbo subscribe success from {}", url);
}
Also used : BrpcURL(com.baidu.brpc.naming.BrpcURL) CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ChildData(org.apache.curator.framework.recipes.cache.ChildData) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) RpcException(com.baidu.brpc.exceptions.RpcException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 17 with ServiceInstance

use of com.baidu.brpc.client.channel.ServiceInstance in project brpc-java by baidu.

the class DubboZookeeperNamingService method lookup.

@Override
public List<ServiceInstance> lookup(SubscribeInfo subscribeInfo) {
    String path = buildParentNodePath(subscribeInfo.getGroup(), subscribeInfo.getInterfaceName(), subscribeInfo.getVersion());
    List<ServiceInstance> instances = new ArrayList<ServiceInstance>();
    try {
        List<String> childList = client.getChildren().forPath(path);
        for (String child : childList) {
            String providerUrlString = URLDecoder.decode(child, "UTF-8");
            BrpcURL url = new BrpcURL(providerUrlString);
            ServiceInstance instance = new ServiceInstance(url.getHostPorts());
            if (subscribeInfo != null && StringUtils.isNoneBlank(subscribeInfo.getServiceId())) {
                instance.setServiceName(subscribeInfo.getServiceId());
            }
            instances.add(instance);
        }
        log.info("lookup {} instances from {}", instances.size(), url);
    } catch (Exception ex) {
        log.warn("lookup service instance list failed from {}, msg={}", url, ex.getMessage());
        if (!subscribeInfo.isIgnoreFailOfNamingService()) {
            throw new RpcException("lookup service instance list failed from zookeeper", ex);
        }
    }
    return instances;
}
Also used : BrpcURL(com.baidu.brpc.naming.BrpcURL) RpcException(com.baidu.brpc.exceptions.RpcException) ArrayList(java.util.ArrayList) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) RpcException(com.baidu.brpc.exceptions.RpcException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 18 with ServiceInstance

use of com.baidu.brpc.client.channel.ServiceInstance in project brpc-java by baidu.

the class StargateZookeeperNamingService method doSubscribe.

@Override
public void doSubscribe(SubscribeInfo subscribeInfo, final NotifyListener listener) throws Exception {
    final String path = buildParentNodePath(resolveGroup(subscribeInfo), subscribeInfo.getInterfaceName(), resolveVersion(subscribeInfo));
    PathChildrenCache cache = new PathChildrenCache(client, path, true);
    cache.getListenable().addListener(new PathChildrenCacheListener() {

        @Override
        public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
            ChildData data = event.getData();
            // 子节点信息,将监听的父节点信息擦除
            String childNodePath = data.getPath().replace(path + "/", "");
            // 如果是客户端上线,不做处理
            if (StargateConstants.ZK_CONSUMER_DIR.equals(childNodePath)) {
                return;
            }
            switch(event.getType()) {
                case CHILD_ADDED:
                    {
                        ServiceInstance endPoint = new ServiceInstance(childNodePath);
                        listener.notify(Collections.singletonList(endPoint), Collections.<ServiceInstance>emptyList());
                        break;
                    }
                case CHILD_REMOVED:
                    {
                        ServiceInstance endPoint = new ServiceInstance(childNodePath);
                        listener.notify(Collections.<ServiceInstance>emptyList(), Collections.singletonList(endPoint));
                        break;
                    }
                case CHILD_UPDATED:
                default:
                    break;
            }
        }
    });
    cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
    subscribeCacheMap.putIfAbsent(subscribeInfo, cache);
    log.info("stargate subscribe success from {}", url);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ChildData(org.apache.curator.framework.recipes.cache.ChildData) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) RpcException(com.baidu.brpc.exceptions.RpcException)

Example 19 with ServiceInstance

use of com.baidu.brpc.client.channel.ServiceInstance in project brpc-java by baidu.

the class ZookeeperNamingService method lookup.

@Override
public List<ServiceInstance> lookup(SubscribeInfo subscribeInfo) {
    String path = getSubscribePath(subscribeInfo);
    List<ServiceInstance> instances = new ArrayList<ServiceInstance>();
    try {
        List<String> childList = client.getChildren().forPath(path);
        for (String child : childList) {
            String childPath = path + "/" + child;
            try {
                String childData = new String(client.getData().forPath(childPath));
                Endpoint endpoint = GsonUtils.fromJson(childData, Endpoint.class);
                ServiceInstance instance = new ServiceInstance(endpoint);
                if (subscribeInfo != null && StringUtils.isNoneBlank(subscribeInfo.getServiceId())) {
                    instance.setServiceName(subscribeInfo.getServiceId());
                }
                instances.add(instance);
            } catch (Exception getDataFailedException) {
                log.warn("get child data failed, path:{}, ex:", childPath, getDataFailedException);
            }
        }
        log.info("lookup {} instances from {}", instances.size(), url);
    } catch (Exception ex) {
        log.warn("lookup end point list failed from {}, msg={}", url, ex.getMessage());
        if (!subscribeInfo.isIgnoreFailOfNamingService()) {
            throw new RpcException("lookup end point list failed from zookeeper failed", ex);
        }
    }
    return instances;
}
Also used : Endpoint(com.baidu.brpc.client.channel.Endpoint) RpcException(com.baidu.brpc.exceptions.RpcException) ArrayList(java.util.ArrayList) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) RpcException(com.baidu.brpc.exceptions.RpcException)

Example 20 with ServiceInstance

use of com.baidu.brpc.client.channel.ServiceInstance in project brpc-java by baidu.

the class ZookeeperNamingService method doSubscribe.

@Override
public void doSubscribe(SubscribeInfo subscribeInfo, final NotifyListener listener) throws Exception {
    String path = getSubscribePath(subscribeInfo);
    PathChildrenCache cache = new PathChildrenCache(client, path, true);
    cache.getListenable().addListener(new PathChildrenCacheListener() {

        @Override
        public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
            ChildData data = event.getData();
            switch(event.getType()) {
                case CHILD_ADDED:
                    {
                        ServiceInstance instance = GsonUtils.fromJson(new String(data.getData()), ServiceInstance.class);
                        listener.notify(Collections.singletonList(instance), Collections.<ServiceInstance>emptyList());
                        break;
                    }
                case CHILD_REMOVED:
                    {
                        ServiceInstance instance = GsonUtils.fromJson(new String(data.getData()), ServiceInstance.class);
                        listener.notify(Collections.<ServiceInstance>emptyList(), Collections.singletonList(instance));
                        break;
                    }
                case CHILD_UPDATED:
                    break;
                default:
                    break;
            }
        }
    });
    cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
    subscribeCacheMap.putIfAbsent(subscribeInfo, cache);
    log.info("subscribe success from {}", url);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ChildData(org.apache.curator.framework.recipes.cache.ChildData) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) RpcException(com.baidu.brpc.exceptions.RpcException)

Aggregations

ServiceInstance (com.baidu.brpc.client.channel.ServiceInstance)24 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)8 RpcException (com.baidu.brpc.exceptions.RpcException)7 RegisterInfo (com.baidu.brpc.naming.RegisterInfo)6 SubscribeInfo (com.baidu.brpc.protocol.SubscribeInfo)6 BrpcURL (com.baidu.brpc.naming.BrpcURL)4 NotifyListener (com.baidu.brpc.naming.NotifyListener)3 BaseMockitoTest (com.baidu.brpc.test.BaseMockitoTest)3 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)3 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)3 CuratorFramework (org.apache.curator.framework.CuratorFramework)3 ChildData (org.apache.curator.framework.recipes.cache.ChildData)3 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)3 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)3 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)3 CommunicationClient (com.baidu.brpc.client.CommunicationClient)2 HttpRequest (com.baidu.brpc.protocol.HttpRequest)2 RpcServer (com.baidu.brpc.server.RpcServer)2 ByteBuf (io.netty.buffer.ByteBuf)2