use of org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent 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.ServiceInstancesChangedEvent in project dubbo by alibaba.
the class ZookeeperServiceDiscovery method registerServiceWatcher.
protected void registerServiceWatcher(String serviceName, ServiceInstancesChangedListener listener) {
String path = buildServicePath(serviceName);
try {
curatorFramework.create().creatingParentsIfNeeded().forPath(path);
} catch (KeeperException.NodeExistsException e) {
// ignored
if (logger.isDebugEnabled()) {
logger.debug(e);
}
} catch (Exception e) {
throw new IllegalStateException("registerServiceWatcher create path=" + path + " fail.", e);
}
CuratorWatcher prev = watcherCaches.get(path);
CuratorWatcher watcher = watcherCaches.computeIfAbsent(path, key -> new ZookeeperServiceDiscoveryChangeWatcher(this, serviceName, listener));
try {
List<String> addresses = curatorFramework.getChildren().usingWatcher(watcher).forPath(path);
// we need to merge pushed instances
if ((prev == null || watcher != prev) && CollectionUtils.isNotEmpty(addresses)) {
listener.onEvent(new ServiceInstancesChangedEvent(serviceName, this.getInstances(serviceName)));
}
} catch (KeeperException.NoNodeException e) {
// ignored
if (logger.isErrorEnabled()) {
logger.error(e.getMessage());
}
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
use of org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent 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.ServiceInstancesChangedEvent 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.ServiceInstancesChangedEvent in project dubbo by alibaba.
the class LoggingEventListenerTest method testOnEvent.
@Test
public void testOnEvent() throws Exception {
URL connectionURL = URL.valueOf("file:///Users/Home");
ServiceDiscovery serviceDiscovery = new FileSystemServiceDiscovery();
serviceDiscovery.initialize(connectionURL);
// ServiceDiscoveryStartingEvent
listener.onEvent(new ServiceDiscoveryInitializingEvent(serviceDiscovery, serviceDiscovery));
// ServiceDiscoveryStartedEvent
listener.onEvent(new ServiceDiscoveryInitializedEvent(serviceDiscovery, serviceDiscovery));
// ServiceInstancePreRegisteredEvent
listener.onEvent(new ServiceInstancePreRegisteredEvent(serviceDiscovery, createInstance()));
// ServiceInstanceRegisteredEvent
listener.onEvent(new ServiceInstanceRegisteredEvent(serviceDiscovery, createInstance()));
// ServiceInstancesChangedEvent
listener.onEvent(new ServiceInstancesChangedEvent("test", Arrays.asList(createInstance())));
// ServiceInstancePreUnregisteredEvent
listener.onEvent(new ServiceInstancePreUnregisteredEvent(serviceDiscovery, createInstance()));
// ServiceInstanceUnregisteredEvent
listener.onEvent(new ServiceInstanceUnregisteredEvent(serviceDiscovery, createInstance()));
// ServiceDiscoveryStoppingEvent
listener.onEvent(new ServiceDiscoveryDestroyingEvent(serviceDiscovery, serviceDiscovery));
// ServiceDiscoveryStoppedEvent
listener.onEvent(new ServiceDiscoveryDestroyedEvent(serviceDiscovery, serviceDiscovery));
}
Aggregations