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());
}
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);
});
}
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());
}
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());
}
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);
});
}
Aggregations