use of org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener in project incubator-dubbo-ops by apache.
the class AdminMappingListener method onEvent.
@Override
public void onEvent(MappingChangedEvent event) {
Set<String> apps = event.getApps();
if (CollectionUtils.isEmpty(apps)) {
return;
}
for (String serviceName : apps) {
ServiceInstancesChangedListener serviceInstancesChangedListener = serviceListeners.get(serviceName);
if (serviceInstancesChangedListener == null) {
synchronized (this) {
serviceInstancesChangedListener = serviceListeners.get(serviceName);
if (serviceInstancesChangedListener == null) {
AddressChangeListener addressChangeListener = new DefaultAddressChangeListener(serviceName, instanceRegistryCache);
serviceInstancesChangedListener = new AdminServiceInstancesChangedListener(Sets.newHashSet(serviceName), serviceDiscovery, addressChangeListener);
serviceInstancesChangedListener.setUrl(CONSUMER_URL);
List<ServiceInstance> serviceInstances = serviceDiscovery.getInstances(serviceName);
if (CollectionUtils.isNotEmpty(serviceInstances)) {
serviceInstancesChangedListener.onEvent(new ServiceInstancesChangedEvent(serviceName, serviceInstances));
}
serviceListeners.put(serviceName, serviceInstancesChangedListener);
serviceInstancesChangedListener.setUrl(CONSUMER_URL);
serviceDiscovery.addServiceInstancesChangedListener(serviceInstancesChangedListener);
}
}
}
}
}
use of org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener in project dubbo by alibaba.
the class ServiceDiscoveryRegistry method doUnsubscribe.
public void doUnsubscribe(URL url, NotifyListener listener) {
writableMetadataService.unsubscribeURL(url);
String protocolServiceKey = url.getServiceKey() + GROUP_CHAR_SEPARATOR + url.getParameter(PROTOCOL_KEY, DUBBO);
String serviceNamesKey = serviceToAppsMapping.remove(protocolServiceKey);
if (serviceNamesKey == null) {
return;
}
ServiceInstancesChangedListener instancesChangedListener = serviceListeners.get(serviceNamesKey);
instancesChangedListener.removeListener(protocolServiceKey);
}
use of org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener 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.event.listener.ServiceInstancesChangedListener 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.event.listener.ServiceInstancesChangedListener in project dubbo by alibaba.
the class ServiceDiscoveryRegistry method subscribeURLs.
protected void subscribeURLs(URL url, NotifyListener listener, Set<String> serviceNames) {
String serviceNamesKey = serviceNames.toString();
String protocolServiceKey = url.getServiceKey() + GROUP_CHAR_SEPARATOR + url.getParameter(PROTOCOL_KEY, DUBBO);
serviceToAppsMapping.put(protocolServiceKey, serviceNamesKey);
// register ServiceInstancesChangedListener
ServiceInstancesChangedListener serviceListener = serviceListeners.computeIfAbsent(serviceNamesKey, k -> new ServiceInstancesChangedListener(serviceNames, serviceDiscovery));
serviceListener.setUrl(url);
listener.addServiceListener(serviceListener);
serviceListener.addListener(protocolServiceKey, listener);
registerServiceInstancesChangedListener(url, serviceListener);
// FIXME: This will cause redundant duplicate notifications
serviceNames.forEach(serviceName -> {
List<ServiceInstance> serviceInstances = serviceDiscovery.getInstances(serviceName);
if (CollectionUtils.isNotEmpty(serviceInstances)) {
serviceListener.onEvent(new ServiceInstancesChangedEvent(serviceName, serviceInstances));
} else {
logger.info("getInstances by serviceName=" + serviceName + " is empty, waiting for serviceListener callback. url=" + url);
}
});
listener.notify(serviceListener.getUrls(protocolServiceKey));
}
Aggregations