Java Code Examples for org.apache.curator.x.discovery.ServiceDiscovery#queryForNames()
The following examples show how to use
org.apache.curator.x.discovery.ServiceDiscovery#queryForNames() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DiscoveryExample.java From xian with Apache License 2.0 | 6 votes |
private static void listInstances(ServiceDiscovery<InstanceDetails> serviceDiscovery) throws Exception { // This shows how to query all the instances in group discovery try { Collection<String> serviceNames = serviceDiscovery.queryForNames(); System.out.println(serviceNames.size() + " type(s)"); for ( String serviceName : serviceNames ) { Collection<ServiceInstance<InstanceDetails>> instances = serviceDiscovery.queryForInstances(serviceName); System.out.println(serviceName); for ( ServiceInstance<InstanceDetails> instance : instances ) { outputInstance(instance); } } } finally { CloseableUtils.closeQuietly(serviceDiscovery); } }
Example 2
Source File: DiscoveryExample.java From curator with Apache License 2.0 | 6 votes |
private static void listInstances(ServiceDiscovery<InstanceDetails> serviceDiscovery) throws Exception { // This shows how to query all the instances in service discovery try { Collection<String> serviceNames = serviceDiscovery.queryForNames(); System.out.println(serviceNames.size() + " type(s)"); for ( String serviceName : serviceNames ) { Collection<ServiceInstance<InstanceDetails>> instances = serviceDiscovery.queryForInstances(serviceName); System.out.println(serviceName); for ( ServiceInstance<InstanceDetails> instance : instances ) { outputInstance(instance); } } } finally { CloseableUtils.closeQuietly(serviceDiscovery); } }
Example 3
Source File: DiscoveryExample.java From ZKRecipesByExample with Apache License 2.0 | 6 votes |
private static void listInstances(ServiceDiscovery<InstanceDetails> serviceDiscovery) throws Exception { // This shows how to query all the instances in service discovery try { Collection<String> serviceNames = serviceDiscovery.queryForNames(); System.out.println(serviceNames.size() + " type(s)"); for (String serviceName : serviceNames) { Collection<ServiceInstance<InstanceDetails>> instances = serviceDiscovery.queryForInstances(serviceName); System.out.println(serviceName); for (ServiceInstance<InstanceDetails> instance : instances) { outputInstance(instance); } } } finally { CloseableUtils.closeQuietly(serviceDiscovery); } }
Example 4
Source File: CuratorSchedulerTest.java From kylin-on-parquet-v2 with Apache License 2.0 | 4 votes |
@Test public void test() throws Exception { final String zkString = zkTestServer.getConnectString(); ServiceDiscovery<LinkedHashMap> serviceDiscovery = null; CuratorFramework curatorClient = null; try { final CuratorScheduler.JsonInstanceSerializer<LinkedHashMap> serializer = new CuratorScheduler.JsonInstanceSerializer<>( LinkedHashMap.class); String servicePath = CuratorScheduler.KYLIN_SERVICE_PATH; 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(CuratorScheduler.SERVICE_NAME.equals(serviceNames.iterator().next())); Collection<ServiceInstance<LinkedHashMap>> instances = serviceDiscovery .queryForInstances(CuratorScheduler.SERVICE_NAME); Assert.assertTrue(instances.size() == 2); List<ServiceInstance<LinkedHashMap>> instancesList = Lists.newArrayList(instances); final List<String> instanceNodes = Lists.transform(instancesList, new Function<ServiceInstance<LinkedHashMap>, String>() { @Nullable @Override public String apply(@Nullable ServiceInstance<LinkedHashMap> stringServiceInstance) { return (String) stringServiceInstance.getPayload() .get(CuratorScheduler.SERVICE_PAYLOAD_DESCRIPTION); } }); Assert.assertTrue(instanceNodes.contains(server1.getAddress() + ":query")); Assert.assertTrue(instanceNodes.contains(server2.getAddress() + ":query")); // stop one server server1.close(); instances = serviceDiscovery.queryForInstances(CuratorScheduler.SERVICE_NAME); Assert.assertTrue(instances.size() == 1); Assert.assertEquals(server2.getAddress() + ":query", instances.iterator().next().getPayload().get(CuratorScheduler.SERVICE_PAYLOAD_DESCRIPTION)); // all stop server2.close(); instances = serviceDiscovery.queryForInstances(CuratorScheduler.SERVICE_NAME); Assert.assertTrue(instances.size() == 0); } finally { CloseableUtils.closeQuietly(serviceDiscovery); CloseableUtils.closeQuietly(curatorClient); } }
Example 5
Source File: KylinServerDiscoveryTest.java From kylin with Apache License 2.0 | 4 votes |
@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")); // stop one server 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)); // all stop server2.close(); instances = serviceDiscovery.queryForInstances(KylinServerDiscovery.SERVICE_NAME); Assert.assertTrue(instances.size() == 0); } finally { CloseableUtils.closeQuietly(serviceDiscovery); CloseableUtils.closeQuietly(curatorClient); } }