Search in sources :

Example 1 with NamingEvent

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();
            }
        });
    });
}
Also used : NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 2 with NamingEvent

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);
}
Also used : Arrays(java.util.Arrays) INTERFACE_KEY(org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY) NacosInstanceManageUtil(org.apache.dubbo.registry.nacos.util.NacosInstanceManageUtil) URLBuilder(org.apache.dubbo.common.URLBuilder) URL(org.apache.dubbo.common.URL) EMPTY_PROTOCOL(org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL) Instance(com.alibaba.nacos.api.naming.pojo.Instance) Map(java.util.Map) PROVIDERS_CATEGORY(org.apache.dubbo.common.constants.RegistryConstants.PROVIDERS_CATEGORY) FailbackRegistry(org.apache.dubbo.registry.support.FailbackRegistry) ROUTERS_CATEGORY(org.apache.dubbo.common.constants.RegistryConstants.ROUTERS_CATEGORY) ListView(com.alibaba.nacos.api.naming.pojo.ListView) Collection(java.util.Collection) DEFAULT_CATEGORY(org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_CATEGORY) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) NacosServiceName.valueOf(org.apache.dubbo.registry.nacos.NacosServiceName.valueOf) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) List(java.util.List) Constants(com.alibaba.nacos.api.common.Constants) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) NAME_SEPARATOR(org.apache.dubbo.registry.nacos.NacosServiceName.NAME_SEPARATOR) Registry(org.apache.dubbo.registry.Registry) UrlUtils(org.apache.dubbo.common.utils.UrlUtils) HashMap(java.util.HashMap) NotifyListener(org.apache.dubbo.registry.NotifyListener) StringUtils(org.apache.dubbo.common.utils.StringUtils) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Lists(com.google.common.collect.Lists) CONFIGURATORS_CATEGORY(org.apache.dubbo.common.constants.RegistryConstants.CONFIGURATORS_CATEGORY) NacosException(com.alibaba.nacos.api.exception.NacosException) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ADMIN_PROTOCOL(org.apache.dubbo.registry.Constants.ADMIN_PROTOCOL) PROTOCOL_KEY(org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) LinkedList(java.util.LinkedList) LinkedHashSet(java.util.LinkedHashSet) PATH_KEY(org.apache.dubbo.common.constants.CommonConstants.PATH_KEY) CONSUMERS_CATEGORY(org.apache.dubbo.common.constants.RegistryConstants.CONSUMERS_CATEGORY) NacosNamingServiceUtils(org.apache.dubbo.registry.nacos.util.NacosNamingServiceUtils) CATEGORY_KEY(org.apache.dubbo.common.constants.RegistryConstants.CATEGORY_KEY) TimeUnit(java.util.concurrent.TimeUnit) GROUP_KEY(org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY) ANY_VALUE(org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE) VERSION_KEY(org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY) Collections(java.util.Collections) NamingService(com.alibaba.nacos.api.naming.NamingService) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) NotifyListener(org.apache.dubbo.registry.NotifyListener)

Aggregations

NacosException (com.alibaba.nacos.api.exception.NacosException)2 NamingEvent (com.alibaba.nacos.api.naming.listener.NamingEvent)2 Constants (com.alibaba.nacos.api.common.Constants)1 NamingService (com.alibaba.nacos.api.naming.NamingService)1 EventListener (com.alibaba.nacos.api.naming.listener.EventListener)1 Instance (com.alibaba.nacos.api.naming.pojo.Instance)1 ListView (com.alibaba.nacos.api.naming.pojo.ListView)1 Lists (com.google.common.collect.Lists)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1