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