use of com.alibaba.nacos.api.naming.listener.NamingEvent in project dubbo by alibaba.
the class NacosServiceDiscovery method addServiceInstancesChangedListener.
@Override
public void addServiceInstancesChangedListener(ServiceInstancesChangedListener listener) throws NullPointerException, IllegalArgumentException {
execute(namingService, service -> {
listener.getServiceNames().forEach(serviceName -> {
try {
service.subscribe(serviceName, e -> {
// Register Nacos EventListener
if (e instanceof NamingEvent) {
NamingEvent event = (NamingEvent) e;
handleEvent(event, listener);
}
});
} catch (NacosException e) {
e.printStackTrace();
}
});
});
}
use of com.alibaba.nacos.api.naming.listener.NamingEvent in project dubbo by alibaba.
the class NacosRegistry method subscribeEventListener.
private void subscribeEventListener(String serviceName, final URL url, final NotifyListener listener) throws NacosException {
ConcurrentMap<NotifyListener, EventListener> listeners = nacosListeners.computeIfAbsent(url, k -> new ConcurrentHashMap<>());
EventListener nacosListener = listeners.computeIfAbsent(listener, k -> {
EventListener eventListener = event -> {
if (event instanceof NamingEvent) {
NamingEvent e = (NamingEvent) event;
List<Instance> instances = e.getInstances();
if (isServiceNamesWithCompatibleMode(url)) {
// Get all instances with corresponding serviceNames to avoid instance overwrite and but with empty instance mentioned
// in https://github.com/apache/dubbo/issues/5885 and https://github.com/apache/dubbo/issues/5899
NacosInstanceManageUtil.initOrRefreshServiceInstanceList(serviceName, instances);
instances = NacosInstanceManageUtil.getAllCorrespondingServiceInstanceList(serviceName);
}
notifySubscriber(url, listener, instances);
}
};
return eventListener;
});
namingService.subscribe(serviceName, getUrl().getParameter(GROUP_KEY, Constants.DEFAULT_GROUP), nacosListener);
}
Aggregations