Search in sources :

Example 6 with ServiceInstancesChangedEvent

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

the class SofaRegistryServiceDiscovery method handleRegistryData.

private List<ServiceInstance> handleRegistryData(String dataId, UserData userData, ServiceInstancesChangedListener listener, CountDownLatch latch) {
    try {
        List<String> datas = getUserData(dataId, userData);
        List<ServiceInstance> serviceInstances = new ArrayList<>(datas.size());
        for (String serviceData : datas) {
            SofaRegistryInstance sri = gson.fromJson(serviceData, SofaRegistryInstance.class);
            DefaultServiceInstance serviceInstance = new DefaultServiceInstance(sri.getId(), dataId, sri.getHost(), sri.getPort());
            serviceInstance.setMetadata(sri.getMetadata());
            serviceInstances.add(serviceInstance);
        }
        if (null != listener) {
            listener.onEvent(new ServiceInstancesChangedEvent(dataId, serviceInstances));
        }
        return serviceInstances;
    } finally {
        if (null != latch) {
            latch.countDown();
        }
    }
}
Also used : DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) ArrayList(java.util.ArrayList) ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) ServiceInstancesChangedEvent(org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent)

Example 7 with ServiceInstancesChangedEvent

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

Example 8 with ServiceInstancesChangedEvent

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

the class ZookeeperServiceDiscoveryChangeWatcher method process.

@Override
public void process(WatchedEvent event) throws Exception {
    Watcher.Event.EventType eventType = event.getType();
    if (NodeChildrenChanged.equals(eventType) || NodeDataChanged.equals(eventType)) {
        if (shouldKeepWatching()) {
            listener.onEvent(new ServiceInstancesChangedEvent(serviceName, zookeeperServiceDiscovery.getInstances(serviceName)));
            zookeeperServiceDiscovery.registerServiceWatcher(serviceName, listener);
        // only the current registry will be queried, which may be pushed empty.
        // zookeeperServiceDiscovery.dispatchServiceInstancesChangedEvent(serviceName);
        }
    }
}
Also used : ServiceInstancesChangedEvent(org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent) WatchedEvent(org.apache.zookeeper.WatchedEvent) ServiceInstancesChangedEvent(org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent)

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