Java Code Examples for org.apache.curator.x.discovery.ServiceDiscovery#registerService()

The following examples show how to use org.apache.curator.x.discovery.ServiceDiscovery#registerService() . 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: RemoteServiceLocatorConfig.java    From springboot-plus with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void addServiceURI(String path, String url) {
	try{
		ServiceInstanceBuilder<Map> service = ServiceInstance.builder();
		service.address("");
		service.port(0);
		service.name(path);
		Map config = new HashMap();
		config.put("url", url);
		service.payload(config);

		ServiceInstance<Map> instance = service.build();

		ServiceDiscovery<Map> serviceDiscovery = ServiceDiscoveryBuilder.builder(Map.class).client(client)
				.serializer(new JsonInstanceSerializer<Map>(Map.class)).basePath("/service").build();
		// 服务注册
		serviceDiscovery.registerService(instance);
		serviceDiscovery.start();
	}catch(Exception ex){
		log.warn(ex.getMessage(), ex);
		throw new PlatformException("服务注册中心 出错",ex);
	}
	
	
}
 
Example 2
Source File: TestServiceDiscovery.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void testCrashedServerMultiInstances() throws Exception
{
    List<Closeable> closeables = Lists.newArrayList();
    try
    {
        Timing timing = new Timing();
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
        closeables.add(client);
        client.start();

        final Semaphore semaphore = new Semaphore(0);
        ServiceInstance<String> instance1 = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
        ServiceInstance<String> instance2 = ServiceInstance.<String>builder().payload("thing").name("test").port(10065).build();
        ServiceDiscovery<String> discovery = new ServiceDiscoveryImpl<String>(client, "/test", new JsonInstanceSerializer<String>(String.class),new FastjsonServiceDefinitionSerializer<>(String.class), instance1, false)
        {
            @Override
            protected void internalRegisterService(ServiceInstance<String> service) throws Exception
            {
                super.internalRegisterService(service);
                semaphore.release();
            }
        };
        closeables.add(discovery);
        discovery.start();
        discovery.registerService(instance2);

        timing.acquireSemaphore(semaphore, 2);
        Assert.assertEquals(discovery.queryForInstances("test").size(), 2);

        KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString());
        server.stop();

        server.restart();
        closeables.add(server);

        timing.acquireSemaphore(semaphore, 2);
        Assert.assertEquals(discovery.queryForInstances("test").size(), 2);
    }
    finally
    {
        for ( Closeable c : closeables )
        {
            CloseableUtils.closeQuietly(c);
        }
    }
}
 
Example 3
Source File: TestServiceDiscovery.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultipleInstances() throws Exception
{
    final String SERVICE_ONE = "one";
    final String SERVICE_TWO = "two";

    List<Closeable> closeables = Lists.newArrayList();
    try
    {
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        closeables.add(client);
        client.start();

        ServiceInstance<Void> s1_i1 = ServiceInstance.<Void>builder().name(SERVICE_ONE).build();
        ServiceInstance<Void> s1_i2 = ServiceInstance.<Void>builder().name(SERVICE_ONE).build();
        ServiceInstance<Void> s2_i1 = ServiceInstance.<Void>builder().name(SERVICE_TWO).build();
        ServiceInstance<Void> s2_i2 = ServiceInstance.<Void>builder().name(SERVICE_TWO).build();

        ServiceDiscovery<Void> discovery = ServiceDiscoveryBuilder.builder(Void.class).client(client).basePath("/test").build();
        closeables.add(discovery);
        discovery.start();

        discovery.registerService(s1_i1);
        discovery.registerService(s1_i2);
        discovery.registerService(s2_i1);
        discovery.registerService(s2_i2);

        Assert.assertEquals(Sets.newHashSet(discovery.queryForNames()), Sets.newHashSet(SERVICE_ONE, SERVICE_TWO));

        List<ServiceInstance<Void>> list = Lists.newArrayList();
        list.add(s1_i1);
        list.add(s1_i2);
        Collections.sort(list, comparator);
        List<ServiceInstance<Void>> queriedInstances = Lists.newArrayList(discovery.queryForInstances(SERVICE_ONE));
        Collections.sort(queriedInstances, comparator);
        Assert.assertEquals(queriedInstances, list, String.format("Not equal l: %s - d: %s", list, queriedInstances));

        list.clear();

        list.add(s2_i1);
        list.add(s2_i2);
        Collections.sort(list, comparator);
        queriedInstances = Lists.newArrayList(discovery.queryForInstances(SERVICE_TWO));
        Collections.sort(queriedInstances, comparator);
        Assert.assertEquals(queriedInstances, list, String.format("Not equal 2: %s - d: %s", list, queriedInstances));
    }
    finally
    {
        Collections.reverse(closeables);
        for ( Closeable c : closeables )
        {
            CloseableUtils.closeQuietly(c);
        }
    }
}
 
Example 4
Source File: TestServiceDiscovery.java    From curator with Apache License 2.0 4 votes vote down vote up
@Test
public void testCrashedServerMultiInstances() throws Exception
{
    CuratorFramework client = null;
    ServiceDiscovery<String> discovery = null;
    try
    {
        Timing timing = new Timing();
        client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
        client.start();

        final Semaphore semaphore = new Semaphore(0);
        ServiceInstance<String> instance1 = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
        ServiceInstance<String> instance2 = ServiceInstance.<String>builder().payload("thing").name("test").port(10065).build();
        discovery = new ServiceDiscoveryImpl<String>(client, "/test", new JsonInstanceSerializer<String>(String.class), instance1, false)
        {
            @Override
            protected void internalRegisterService(ServiceInstance<String> service) throws Exception
            {
                super.internalRegisterService(service);
                semaphore.release();
            }
        };
        discovery.start();
        discovery.registerService(instance2);

        timing.acquireSemaphore(semaphore, 2);
        Assert.assertEquals(discovery.queryForInstances("test").size(), 2);

        client.getZookeeperClient().getZooKeeper().getTestable().injectSessionExpiration();
        server.stop();

        server.restart();

        timing.acquireSemaphore(semaphore, 2);
        Assert.assertEquals(discovery.queryForInstances("test").size(), 2);
    }
    finally
    {
        CloseableUtils.closeQuietly(discovery);
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 5
Source File: TestServiceDiscovery.java    From curator with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultipleInstances() throws Exception
{
    final String SERVICE_ONE = "one";
    final String SERVICE_TWO = "two";

    CuratorFramework client = null;
    ServiceDiscovery<Void> discovery = null;
    try
    {
        client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        client.start();

        ServiceInstance<Void> s1_i1 = ServiceInstance.<Void>builder().name(SERVICE_ONE).build();
        ServiceInstance<Void> s1_i2 = ServiceInstance.<Void>builder().name(SERVICE_ONE).build();
        ServiceInstance<Void> s2_i1 = ServiceInstance.<Void>builder().name(SERVICE_TWO).build();
        ServiceInstance<Void> s2_i2 = ServiceInstance.<Void>builder().name(SERVICE_TWO).build();

        discovery = ServiceDiscoveryBuilder.builder(Void.class).client(client).basePath("/test").build();
        discovery.start();

        discovery.registerService(s1_i1);
        discovery.registerService(s1_i2);
        discovery.registerService(s2_i1);
        discovery.registerService(s2_i2);

        Assert.assertEquals(Sets.newHashSet(discovery.queryForNames()), Sets.newHashSet(SERVICE_ONE, SERVICE_TWO));

        List<ServiceInstance<Void>> list = Lists.newArrayList();
        list.add(s1_i1);
        list.add(s1_i2);
        Collections.sort(list, comparator);
        List<ServiceInstance<Void>> queriedInstances = Lists.newArrayList(discovery.queryForInstances(SERVICE_ONE));
        Collections.sort(queriedInstances, comparator);
        Assert.assertEquals(queriedInstances, list, String.format("Not equal l: %s - d: %s", list, queriedInstances));

        list.clear();

        list.add(s2_i1);
        list.add(s2_i2);
        Collections.sort(list, comparator);
        queriedInstances = Lists.newArrayList(discovery.queryForInstances(SERVICE_TWO));
        Collections.sort(queriedInstances, comparator);
        Assert.assertEquals(queriedInstances, list, String.format("Not equal 2: %s - d: %s", list, queriedInstances));
    }
    finally
    {
        CloseableUtils.closeQuietly(discovery);
        CloseableUtils.closeQuietly(client);
    }
}