Search in sources :

Example 1 with StargateURI

use of com.baidu.brpc.protocol.stargate.StargateURI in project brpc-java by baidu.

the class StargateZookeeperNamingService method lookup.

@Override
public List<ServiceInstance> lookup(SubscribeInfo subscribeInfo) {
    String path = buildParentNodePath(resolveGroup(subscribeInfo), subscribeInfo.getInterfaceName(), resolveVersion(subscribeInfo));
    List<ServiceInstance> instances = new ArrayList<ServiceInstance>();
    try {
        List<String> childList = client.getChildren().forPath(path);
        for (String child : childList) {
            // 跨过所有客户端节点
            if (StargateConstants.ZK_CONSUMER_DIR.equals(child)) {
                continue;
            }
            String childPath = path + "/" + child;
            try {
                String childData = new String(client.getData().forPath(childPath));
                StargateURI uri = new StargateURI.Builder(childData).build();
                ServiceInstance instance = new ServiceInstance(uri.getHost(), uri.getPort());
                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 service instance 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 : StargateURI(com.baidu.brpc.protocol.stargate.StargateURI) RpcException(com.baidu.brpc.exceptions.RpcException) ArrayList(java.util.ArrayList) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) RpcException(com.baidu.brpc.exceptions.RpcException)

Aggregations

ServiceInstance (com.baidu.brpc.client.channel.ServiceInstance)1 RpcException (com.baidu.brpc.exceptions.RpcException)1 StargateURI (com.baidu.brpc.protocol.stargate.StargateURI)1 ArrayList (java.util.ArrayList)1