This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 326fb7250c Fix Nacos ServiceName error (#11180)
326fb7250c is described below

commit 326fb7250c8b6e175785167da6ea1188a7582cd5
Author: Albumen Kevin <[email protected]>
AuthorDate: Wed Dec 21 23:38:49 2022 +0800

    Fix Nacos ServiceName error (#11180)
---
 .../apache/dubbo/registry/nacos/NacosRegistry.java | 60 +++++++++++-----------
 1 file changed, 30 insertions(+), 30 deletions(-)

diff --git 
a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java
 
b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java
index af356373ee..4aba61cf02 100644
--- 
a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java
+++ 
b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java
@@ -38,19 +38,19 @@ import com.alibaba.nacos.api.naming.pojo.ListView;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.ConcurrentHashMap;
 import java.util.stream.Collectors;
-import java.util.Collections;
 
 import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE;
 import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
@@ -123,7 +123,7 @@ public class NacosRegistry extends FailbackRegistry {
 
     private final NacosNamingServiceWrapper namingService;
 
-    private final ConcurrentMap<URL, ConcurrentMap<NotifyListener, 
EventListener>> nacosListeners = new ConcurrentHashMap<>();
+    private final ConcurrentMap<URL, Map<NotifyListener, Map<String, 
EventListener>>> nacosListeners = new ConcurrentHashMap<>();
 
     public NacosRegistry(URL url, NacosNamingServiceWrapper namingService) {
         super(url);
@@ -242,8 +242,7 @@ public class NacosRegistry extends FailbackRegistry {
     public void doUnsubscribe(URL url, NotifyListener listener) {
         if (isAdminProtocol(url)) {
             shutdownServiceNamesLookup();
-        }
-        else {
+        } else {
             Set<String> serviceNames = getServiceNames(url, listener);
 
             doUnsubscribe(url, listener, serviceNames);
@@ -305,7 +304,7 @@ public class NacosRegistry extends FailbackRegistry {
         Set<String> serviceNames = new LinkedHashSet<>();
 
         execute(namingService -> 
serviceNames.addAll(namingService.getServicesOfServer(1, Integer.MAX_VALUE,
-                getUrl().getParameter(GROUP_KEY, 
Constants.DEFAULT_GROUP)).getData()
+                        getUrl().getParameter(GROUP_KEY, 
Constants.DEFAULT_GROUP)).getData()
                 .stream()
                 .filter(this::isConformRules)
                 .map(NacosServiceName::new)
@@ -511,27 +510,24 @@ public class NacosRegistry extends FailbackRegistry {
 
     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)) {
+        Map<NotifyListener, Map<String, EventListener>> listeners = 
nacosListeners.computeIfAbsent(url, k -> new ConcurrentHashMap<>());
+        Map<String, EventListener> eventListenerMap = 
listeners.computeIfAbsent(listener, k -> new ConcurrentHashMap<>());
+        EventListener nacosListener = 
eventListenerMap.computeIfAbsent(serviceName,
+                name -> 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(name, instances);
+                            instances = 
NacosInstanceManageUtil.getAllCorrespondingServiceInstanceList(name);
+                        }
 
-                        // 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(e.getServiceName(), 
instances);
-                        instances = 
NacosInstanceManageUtil.getAllCorrespondingServiceInstanceList(e.getServiceName());
+                        notifySubscriber(url, listener, instances);
                     }
-
-                    notifySubscriber(url, listener, instances);
-                }
-            };
-            return eventListener;
-        });
+                });
         namingService.subscribe(serviceName,
                 getUrl().getParameter(GROUP_KEY, Constants.DEFAULT_GROUP),
                 nacosListener);
@@ -539,12 +535,16 @@ public class NacosRegistry extends FailbackRegistry {
 
     private void unsubscribeEventListener(String serviceName, final URL url, 
final NotifyListener listener)
             throws NacosException {
-        ConcurrentMap<NotifyListener, EventListener> 
notifyListenerEventListenerConcurrentMap = nacosListeners.get(url);
-        if(notifyListenerEventListenerConcurrentMap == null){
+        Map<NotifyListener, Map<String, EventListener>> 
notifyListenerEventListenerConcurrentMap = nacosListeners.get(url);
+        if (notifyListenerEventListenerConcurrentMap == null) {
+            return;
+        }
+        Map<String, EventListener> listenerMap = 
notifyListenerEventListenerConcurrentMap.get(listener);
+        if (listenerMap == null) {
             return;
         }
-        EventListener nacosListener = 
notifyListenerEventListenerConcurrentMap.get(listener);
-        if(nacosListener == null){
+        EventListener nacosListener = listenerMap.remove(serviceName);
+        if (nacosListener == null) {
             return;
         }
         namingService.unsubscribe(serviceName,

Reply via email to