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);
}
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;
}
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);
}
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;
}
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);
}
Aggregations