Search in sources :

Example 6 with ServiceInstance

use of org.apache.dubbo.registry.client.ServiceInstance in project dubbo by alibaba.

the class ConsulServiceDiscoveryTest method testRegistration.

@Test
public void testRegistration() throws InterruptedException {
    DefaultServiceInstance serviceInstance = createServiceInstance(SERVICE_NAME, LOCALHOST, NetUtils.getAvailablePort());
    consulServiceDiscovery.register(serviceInstance);
    Thread.sleep(5000);
    List<ServiceInstance> serviceInstances = consulServiceDiscovery.getInstances(SERVICE_NAME);
    assertEquals(serviceInstances.size(), 1);
    assertEquals(serviceInstances.get(0).getId(), Integer.toHexString(serviceInstance.hashCode()));
    assertEquals(serviceInstances.get(0).getHost(), serviceInstance.getHost());
    assertEquals(serviceInstances.get(0).getServiceName(), serviceInstance.getServiceName());
    assertEquals(serviceInstances.get(0).getPort(), serviceInstance.getPort());
    consulServiceDiscovery.unregister(serviceInstance);
    Thread.sleep(5000);
    serviceInstances = consulServiceDiscovery.getInstances(SERVICE_NAME);
    System.out.println(serviceInstances.size());
    assertTrue(serviceInstances.isEmpty());
}
Also used : DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) Test(org.junit.jupiter.api.Test)

Example 7 with ServiceInstance

use of org.apache.dubbo.registry.client.ServiceInstance in project dubbo by alibaba.

the class ZookeeperServiceDiscovery method getInstances.

@Override
public Page<ServiceInstance> getInstances(String serviceName, int offset, int pageSize, boolean healthyOnly) {
    String path = buildServicePath(serviceName);
    return execute(path, p -> {
        List<ServiceInstance> serviceInstances = new LinkedList<>();
        int totalSize = 0;
        try {
            List<String> serviceIds = new LinkedList<>(curatorFramework.getChildren().forPath(p));
            totalSize = serviceIds.size();
            Iterator<String> iterator = serviceIds.iterator();
            for (int i = 0; i < offset; i++) {
                if (iterator.hasNext()) {
                    // remove the elements from 0 to offset
                    iterator.next();
                    iterator.remove();
                }
            }
            for (int i = 0; i < pageSize; i++) {
                if (iterator.hasNext()) {
                    String serviceId = iterator.next();
                    ServiceInstance serviceInstance = build(serviceDiscovery.queryForInstance(serviceName, serviceId));
                    serviceInstances.add(serviceInstance);
                }
            }
            if (healthyOnly) {
                Iterator<ServiceInstance> instanceIterator = serviceInstances.iterator();
                while (instanceIterator.hasNext()) {
                    ServiceInstance instance = instanceIterator.next();
                    if (!instance.isHealthy()) {
                        instanceIterator.remove();
                    }
                }
            }
        } catch (KeeperException.NoNodeException e) {
            logger.warn(p + " path not exist.", e);
        }
        return new DefaultPage<>(offset, pageSize, serviceInstances, totalSize);
    });
}
Also used : ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) DefaultPage(org.apache.dubbo.common.utils.DefaultPage) LinkedList(java.util.LinkedList) KeeperException(org.apache.zookeeper.KeeperException)

Example 8 with ServiceInstance

use of org.apache.dubbo.registry.client.ServiceInstance in project dubbo by alibaba.

the class ZookeeperServiceDiscoveryTest method testGetInstances.

@Test
public void testGetInstances() throws InterruptedException {
    List<ServiceInstance> instances = asList(createServiceInstance(SERVICE_NAME, LOCALHOST, 8080), createServiceInstance(SERVICE_NAME, LOCALHOST, 8081), createServiceInstance(SERVICE_NAME, LOCALHOST, 8082));
    instances.forEach(discovery::register);
    List<ServiceInstance> serviceInstances = new LinkedList<>();
    CountDownLatch latch = new CountDownLatch(1);
    // Add Listener
    discovery.addServiceInstancesChangedListener(new ServiceInstancesChangedListener(Sets.newSet(SERVICE_NAME), discovery) {

        @Override
        public void onEvent(ServiceInstancesChangedEvent event) {
            serviceInstances.addAll(event.getServiceInstances());
            latch.countDown();
        }
    });
    discovery.register(createServiceInstance(SERVICE_NAME, LOCALHOST, 8082));
    discovery.update(createServiceInstance(SERVICE_NAME, LOCALHOST, 8082));
    latch.await();
    assertFalse(serviceInstances.isEmpty());
    // offset starts 0
    int offset = 0;
    // requestSize > total elements
    int requestSize = 5;
    Page<ServiceInstance> page = discovery.getInstances(SERVICE_NAME, offset, requestSize);
    assertEquals(0, page.getOffset());
    assertEquals(5, page.getPageSize());
    assertEquals(3, page.getTotalSize());
    assertEquals(3, page.getData().size());
    assertTrue(page.hasData());
    for (ServiceInstance instance : page.getData()) {
        assertTrue(instances.contains(instance));
    }
    // requestSize < total elements
    requestSize = 2;
    page = discovery.getInstances(SERVICE_NAME, offset, requestSize);
    assertEquals(0, page.getOffset());
    assertEquals(2, page.getPageSize());
    assertEquals(3, page.getTotalSize());
    assertEquals(2, page.getData().size());
    assertTrue(page.hasData());
    for (ServiceInstance instance : page.getData()) {
        assertTrue(instances.contains(instance));
    }
    offset = 1;
    page = discovery.getInstances(SERVICE_NAME, offset, requestSize);
    assertEquals(1, page.getOffset());
    assertEquals(2, page.getPageSize());
    assertEquals(3, page.getTotalSize());
    assertEquals(2, page.getData().size());
    assertTrue(page.hasData());
    for (ServiceInstance instance : page.getData()) {
        assertTrue(instances.contains(instance));
    }
    offset = 2;
    page = discovery.getInstances(SERVICE_NAME, offset, requestSize);
    assertEquals(2, page.getOffset());
    assertEquals(2, page.getPageSize());
    assertEquals(3, page.getTotalSize());
    assertEquals(1, page.getData().size());
    assertTrue(page.hasData());
    offset = 3;
    page = discovery.getInstances(SERVICE_NAME, offset, requestSize);
    assertEquals(3, page.getOffset());
    assertEquals(2, page.getPageSize());
    assertEquals(3, page.getTotalSize());
    assertEquals(0, page.getData().size());
    assertFalse(page.hasData());
    offset = 5;
    page = discovery.getInstances(SERVICE_NAME, offset, requestSize);
    assertEquals(5, page.getOffset());
    assertEquals(2, page.getPageSize());
    assertEquals(3, page.getTotalSize());
    assertEquals(0, page.getData().size());
    assertFalse(page.hasData());
}
Also used : ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) ServiceInstancesChangedEvent(org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent) CountDownLatch(java.util.concurrent.CountDownLatch) ServiceInstancesChangedListener(org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

Example 9 with ServiceInstance

use of org.apache.dubbo.registry.client.ServiceInstance in project dubbo by alibaba.

the class ZookeeperServiceDiscoveryTest method testRegistration.

@Test
public void testRegistration() throws InterruptedException {
    DefaultServiceInstance serviceInstance = createServiceInstance(SERVICE_NAME, LOCALHOST, NetUtils.getAvailablePort());
    CountDownLatch latch = new CountDownLatch(1);
    // Add Listener
    discovery.addServiceInstancesChangedListener(new ServiceInstancesChangedListener(Sets.newSet(SERVICE_NAME), discovery) {

        @Override
        public void onEvent(ServiceInstancesChangedEvent event) {
            latch.countDown();
        }
    });
    discovery.register(serviceInstance);
    latch.await();
    List<ServiceInstance> serviceInstances = discovery.getInstances(SERVICE_NAME);
    assertTrue(serviceInstances.contains(serviceInstance));
    assertEquals(asList(serviceInstance), serviceInstances);
    Map<String, String> metadata = new HashMap<>();
    // metadata.put("message", "Hello,World");
    serviceInstance.setMetadata(metadata);
    discovery.update(serviceInstance);
    serviceInstances = discovery.getInstances(SERVICE_NAME);
    assertEquals(serviceInstance, serviceInstances.get(0));
    discovery.unregister(serviceInstance);
    serviceInstances = discovery.getInstances(SERVICE_NAME);
    assertTrue(serviceInstances.isEmpty());
}
Also used : DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) HashMap(java.util.HashMap) ServiceInstancesChangedEvent(org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent) ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) CountDownLatch(java.util.concurrent.CountDownLatch) ServiceInstancesChangedListener(org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener) Test(org.junit.jupiter.api.Test)

Example 10 with ServiceInstance

use of org.apache.dubbo.registry.client.ServiceInstance in project dubbo by alibaba.

the class NacosServiceDiscovery method doRegister.

@Override
public void doRegister(ServiceInstance serviceInstance) {
    execute(namingService, service -> {
        Instance instance = toInstance(serviceInstance);
        appendPreservedParam(instance);
        service.registerInstance(instance.getServiceName(), group, instance);
    });
}
Also used : NacosNamingServiceUtils.toInstance(org.apache.dubbo.registry.nacos.util.NacosNamingServiceUtils.toInstance) ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) Instance(com.alibaba.nacos.api.naming.pojo.Instance)

Aggregations

ServiceInstance (org.apache.dubbo.registry.client.ServiceInstance)16 DefaultServiceInstance (org.apache.dubbo.registry.client.DefaultServiceInstance)11 Test (org.junit.jupiter.api.Test)5 ArrayList (java.util.ArrayList)4 ServiceInstancesChangedEvent (org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent)4 Instance (com.alibaba.nacos.api.naming.pojo.Instance)3 LinkedList (java.util.LinkedList)3 ServiceInstancesChangedListener (org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener)3 HashMap (java.util.HashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 URL (org.apache.dubbo.common.URL)2 MetadataInfo (org.apache.dubbo.metadata.MetadataInfo)2 NacosNamingServiceUtils.toInstance (org.apache.dubbo.registry.nacos.util.NacosNamingServiceUtils.toInstance)2 Gson (com.google.gson.Gson)1 InstanceInfo (com.netflix.appinfo.InstanceInfo)1 Application (com.netflix.discovery.shared.Application)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1