@Test public void test() throws Exception { final String zkString = zkTestServer.getConnectString(); ServiceDiscovery<LinkedHashMap> serviceDiscovery = null; CuratorFramework curatorClient = null; try { String servicePath = KylinServerDiscovery.SERVICE_PATH; final KylinServerDiscovery.JsonInstanceSerializer<LinkedHashMap> serializer = new KylinServerDiscovery.JsonInstanceSerializer<>(LinkedHashMap.class); curatorClient = ZKUtil.newZookeeperClient(zkString, new ExponentialBackoffRetry(3000, 3)); serviceDiscovery = ServiceDiscoveryBuilder.builder(LinkedHashMap.class).client(curatorClient) .basePath(servicePath).serializer(serializer).build(); serviceDiscovery.start(); final ExampleServer server1 = new ExampleServer("localhost:1111"); final ExampleServer server2 = new ExampleServer("localhost:2222"); Collection<String> serviceNames = serviceDiscovery.queryForNames(); Assert.assertTrue(serviceNames.size() == 1); Assert.assertTrue(KylinServerDiscovery.SERVICE_NAME.equals(serviceNames.iterator().next())); Collection<ServiceInstance<LinkedHashMap>> instances = serviceDiscovery .queryForInstances(KylinServerDiscovery.SERVICE_NAME); Assert.assertTrue(instances.size() == 2); List<ServiceInstance<LinkedHashMap>> instancesList = Lists.newArrayList(instances); final List<String> instanceNodes = instancesList.stream() .map(input -> input.getAddress() + ":" + input.getPort() + ":" + input.getPayload().get(KylinServerDiscovery.SERVICE_PAYLOAD_DESCRIPTION)) .collect(Collectors.toList()); Assert.assertTrue(instanceNodes.contains(server1.getAddress() + ":query")); Assert.assertTrue(instanceNodes.contains(server2.getAddress() + ":query")); server1.close(); instances = serviceDiscovery.queryForInstances(KylinServerDiscovery.SERVICE_NAME); ServiceInstance<LinkedHashMap> existingInstance = instances.iterator().next(); Assert.assertTrue(instances.size() == 1); Assert.assertEquals(server2.getAddress() + ":query", existingInstance.getAddress() + ":" + existingInstance.getPort() + ":" + existingInstance.getPayload().get(KylinServerDiscovery.SERVICE_PAYLOAD_DESCRIPTION)); server2.close(); instances = serviceDiscovery.queryForInstances(KylinServerDiscovery.SERVICE_NAME); Assert.assertTrue(instances.size() == 0); } finally { CloseableUtils.closeQuietly(serviceDiscovery); CloseableUtils.closeQuietly(curatorClient); } }
@Override public void close() throws IOException { IOUtils.closeQuietly(serviceCache); IOUtils.closeQuietly(serviceDiscovery); }
KylinServerDiscovery implements Closeable { @Override public void close() throws IOException { IOUtils.closeQuietly(serviceCache); IOUtils.closeQuietly(serviceDiscovery); } }
KylinServerDiscovery implements Closeable { @Override public void close() throws IOException { IOUtils.closeQuietly(serviceCache); IOUtils.closeQuietly(serviceDiscovery); } private  KylinServerDiscovery(); @VisibleForTesting protected  KylinServerDiscovery(KylinConfig kylinConfig); }
KylinServerDiscovery implements Closeable { @Override public void close() throws IOException { IOUtils.closeQuietly(serviceCache); IOUtils.closeQuietly(serviceDiscovery); } private  KylinServerDiscovery(); @VisibleForTesting protected  KylinServerDiscovery(KylinConfig kylinConfig); static KylinServerDiscovery getInstance(); @Override void close(); }
KylinServerDiscovery implements Closeable { @Override public void close() throws IOException { IOUtils.closeQuietly(serviceCache); IOUtils.closeQuietly(serviceDiscovery); } private  KylinServerDiscovery(); @VisibleForTesting protected  KylinServerDiscovery(KylinConfig kylinConfig); static KylinServerDiscovery getInstance(); @Override void close(); static final String SERVICE_PATH; static final String SERVICE_NAME; static final String SERVICE_PAYLOAD_DESCRIPTION; }