ZkRegister implements IRegister { @Override public List<Provider> discover(String serviceName, String version) { assert zkClient != null; String serverPath = root_path + "/" + serviceName + root_provider; List<String> children = null; try { children = zkClient.getChildren(serverPath); } catch (Exception ex) { if (ex.getMessage().contains("KeeperException$NoNodeException") && ex.getMessage().contains("KeeperErrorCode")) { throw new RuntimeException("提供者[" + serviceName + "]服务列表为空"); } else { ex.printStackTrace(); System.exit(1); } } if (children == null || children.size() == 0) { throw new RuntimeException("提供者列表为空"); } List<Provider> collect = children.parallelStream().map(this::toProvider).collect(Collectors.toList()); List<Provider> result = collect.stream().filter(provider -> provider.getVersion().equalsIgnoreCase(version)).collect(Collectors.toList()); if (result == null || result.size() == 0) { throw new RuntimeException("版本号:[" + version + "]的提供者列表为空"); } return result; } ZkRegister(String host);  ZkRegister(String host, Integer sessionOut, Integer connectionOut); @Override void registerService(List<Provider> providerList); @Override List<Provider> discover(String serviceName, String version); @Override void registerConsumer(Consumer consumer); @Override void subscribe(String service);  }
@Test public void discover() throws InterruptedException { List<Provider> discover = register.discover("top.huzhurong.fuck.UserService", "1.0.0"); discover.forEach(System.out::println); discover.forEach(provider -> { String path = ZkRegister.root_path + "/" + provider.getServiceName() + ZkRegister.root_provider; ((ZkRegister) register).getZkClient().subscribeChildChanges(path, (parent, children) -> { System.out.println(parent); children.forEach(System.out::println); System.out.println("============="); }); }); Thread.sleep(100000000000L); }