@Test @Ignore public void testRegisterWhenZookeeperDownAndUp() throws Exception { namingUrl = new BrpcURL("zookeeper: namingService = new ZookeeperNamingService(namingUrl); RegisterInfo registerInfo = createRegisterInfo("127.0.0.1", 8015); namingService.register(registerInfo); SubscribeInfo subscribeInfo = createSubscribeInfo(false); List<ServiceInstance> instances = namingService.lookup(subscribeInfo); Assert.assertTrue(instances.size() == 1); Assert.assertTrue(instances.get(0).getIp().equals("127.0.0.1")); Assert.assertTrue(instances.get(0).getPort() == 8015); Thread.sleep(30 * 1000); instances = namingService.lookup(subscribeInfo); Assert.assertTrue(instances.size() == 1); System.out.println(instances.get(0)); Assert.assertTrue(instances.get(0).getIp().equals("127.0.0.1")); Assert.assertTrue(instances.get(0).getPort() == 8015); namingService.unregister(registerInfo); }
@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; }
ZookeeperNamingService extends FailbackNamingService implements NamingService { @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; } }
ZookeeperNamingService extends FailbackNamingService implements NamingService { @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; } ZookeeperNamingService(BrpcURL url); }
ZookeeperNamingService extends FailbackNamingService implements NamingService { @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; } ZookeeperNamingService(BrpcURL url); @Override List<ServiceInstance> lookup(SubscribeInfo subscribeInfo); @Override void doSubscribe(SubscribeInfo subscribeInfo, final NotifyListener listener); @Override void doUnsubscribe(SubscribeInfo subscribeInfo); @Override void doRegister(RegisterInfo registerInfo); @Override void doUnregister(RegisterInfo registerInfo); @Override void destroy(); String getSubscribePath(SubscribeInfo subscribeInfo); String getParentRegisterPath(RegisterInfo registerInfo); String getRegisterPath(RegisterInfo registerInfo); String getRegisterPathData(RegisterInfo registerInfo); }
ZookeeperNamingService extends FailbackNamingService implements NamingService { @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; } ZookeeperNamingService(BrpcURL url); @Override List<ServiceInstance> lookup(SubscribeInfo subscribeInfo); @Override void doSubscribe(SubscribeInfo subscribeInfo, final NotifyListener listener); @Override void doUnsubscribe(SubscribeInfo subscribeInfo); @Override void doRegister(RegisterInfo registerInfo); @Override void doUnregister(RegisterInfo registerInfo); @Override void destroy(); String getSubscribePath(SubscribeInfo subscribeInfo); String getParentRegisterPath(RegisterInfo registerInfo); String getRegisterPath(RegisterInfo registerInfo); String getRegisterPathData(RegisterInfo registerInfo);  }