Search in sources :

Example 1 with ServiceInstancesChangedEvent

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);
                }
            }
        }
    }
}
Also used : ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) ServiceInstancesChangedEvent(org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent) ServiceInstancesChangedListener(org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener)

Example 2 with ServiceInstancesChangedEvent

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);
    }
}
Also used : CuratorWatcher(org.apache.curator.framework.api.CuratorWatcher) ServiceInstancesChangedEvent(org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent) KeeperException(org.apache.zookeeper.KeeperException) KeeperException(org.apache.zookeeper.KeeperException)

Example 3 with ServiceInstancesChangedEvent

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());
}
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 4 with ServiceInstancesChangedEvent

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());
}
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 5 with ServiceInstancesChangedEvent

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));
}
Also used : ServiceDiscoveryInitializingEvent(org.apache.dubbo.registry.client.event.ServiceDiscoveryInitializingEvent) ServiceInstancePreUnregisteredEvent(org.apache.dubbo.registry.client.event.ServiceInstancePreUnregisteredEvent) ServiceDiscoveryInitializedEvent(org.apache.dubbo.registry.client.event.ServiceDiscoveryInitializedEvent) ServiceInstancePreRegisteredEvent(org.apache.dubbo.registry.client.event.ServiceInstancePreRegisteredEvent) ServiceDiscoveryDestroyingEvent(org.apache.dubbo.registry.client.event.ServiceDiscoveryDestroyingEvent) ServiceInstanceRegisteredEvent(org.apache.dubbo.registry.client.event.ServiceInstanceRegisteredEvent) ServiceInstancesChangedEvent(org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent) ServiceInstanceUnregisteredEvent(org.apache.dubbo.registry.client.event.ServiceInstanceUnregisteredEvent) ServiceDiscoveryDestroyedEvent(org.apache.dubbo.registry.client.event.ServiceDiscoveryDestroyedEvent) FileSystemServiceDiscovery(org.apache.dubbo.registry.client.FileSystemServiceDiscovery) URL(org.apache.dubbo.common.URL) FileSystemServiceDiscovery(org.apache.dubbo.registry.client.FileSystemServiceDiscovery) ServiceDiscovery(org.apache.dubbo.registry.client.ServiceDiscovery) Test(org.junit.jupiter.api.Test)

Aggregations

ServiceInstancesChangedEvent (org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent)8 ServiceInstance (org.apache.dubbo.registry.client.ServiceInstance)4 ServiceInstancesChangedListener (org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener)4 DefaultServiceInstance (org.apache.dubbo.registry.client.DefaultServiceInstance)3 Test (org.junit.jupiter.api.Test)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 CuratorWatcher (org.apache.curator.framework.api.CuratorWatcher)1 URL (org.apache.dubbo.common.URL)1 FileSystemServiceDiscovery (org.apache.dubbo.registry.client.FileSystemServiceDiscovery)1 ServiceDiscovery (org.apache.dubbo.registry.client.ServiceDiscovery)1 ServiceDiscoveryDestroyedEvent (org.apache.dubbo.registry.client.event.ServiceDiscoveryDestroyedEvent)1 ServiceDiscoveryDestroyingEvent (org.apache.dubbo.registry.client.event.ServiceDiscoveryDestroyingEvent)1 ServiceDiscoveryInitializedEvent (org.apache.dubbo.registry.client.event.ServiceDiscoveryInitializedEvent)1 ServiceDiscoveryInitializingEvent (org.apache.dubbo.registry.client.event.ServiceDiscoveryInitializingEvent)1 ServiceInstancePreRegisteredEvent (org.apache.dubbo.registry.client.event.ServiceInstancePreRegisteredEvent)1 ServiceInstancePreUnregisteredEvent (org.apache.dubbo.registry.client.event.ServiceInstancePreUnregisteredEvent)1 ServiceInstanceRegisteredEvent (org.apache.dubbo.registry.client.event.ServiceInstanceRegisteredEvent)1