Skip to content

Commit 326fb72

Browse files
authored
Fix Nacos ServiceName error (#11180)
1 parent d6341d9 commit 326fb72

File tree

1 file changed

+30
-30
lines changed
  • dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos

1 file changed

+30
-30
lines changed

dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java

+30-30
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@
3838
import java.util.ArrayList;
3939
import java.util.Arrays;
4040
import java.util.Collection;
41+
import java.util.Collections;
4142
import java.util.HashMap;
4243
import java.util.LinkedHashSet;
4344
import java.util.LinkedList;
4445
import java.util.List;
4546
import java.util.Map;
4647
import java.util.Set;
48+
import java.util.concurrent.ConcurrentHashMap;
49+
import java.util.concurrent.ConcurrentMap;
4750
import java.util.concurrent.Executors;
4851
import java.util.concurrent.ScheduledExecutorService;
4952
import java.util.concurrent.TimeUnit;
50-
import java.util.concurrent.ConcurrentMap;
51-
import java.util.concurrent.ConcurrentHashMap;
5253
import java.util.stream.Collectors;
53-
import java.util.Collections;
5454

5555
import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE;
5656
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
@@ -123,7 +123,7 @@ public class NacosRegistry extends FailbackRegistry {
123123

124124
private final NacosNamingServiceWrapper namingService;
125125

126-
private final ConcurrentMap<URL, ConcurrentMap<NotifyListener, EventListener>> nacosListeners = new ConcurrentHashMap<>();
126+
private final ConcurrentMap<URL, Map<NotifyListener, Map<String, EventListener>>> nacosListeners = new ConcurrentHashMap<>();
127127

128128
public NacosRegistry(URL url, NacosNamingServiceWrapper namingService) {
129129
super(url);
@@ -242,8 +242,7 @@ private boolean isServiceNamesWithCompatibleMode(final URL url) {
242242
public void doUnsubscribe(URL url, NotifyListener listener) {
243243
if (isAdminProtocol(url)) {
244244
shutdownServiceNamesLookup();
245-
}
246-
else {
245+
} else {
247246
Set<String> serviceNames = getServiceNames(url, listener);
248247

249248
doUnsubscribe(url, listener, serviceNames);
@@ -305,7 +304,7 @@ private Set<String> filterServiceNames(NacosServiceName serviceName) {
305304
Set<String> serviceNames = new LinkedHashSet<>();
306305

307306
execute(namingService -> serviceNames.addAll(namingService.getServicesOfServer(1, Integer.MAX_VALUE,
308-
getUrl().getParameter(GROUP_KEY, Constants.DEFAULT_GROUP)).getData()
307+
getUrl().getParameter(GROUP_KEY, Constants.DEFAULT_GROUP)).getData()
309308
.stream()
310309
.filter(this::isConformRules)
311310
.map(NacosServiceName::new)
@@ -511,40 +510,41 @@ private List<URL> buildURLs(URL consumerURL, Collection<Instance> instances) {
511510

512511
private void subscribeEventListener(String serviceName, final URL url, final NotifyListener listener)
513512
throws NacosException {
514-
ConcurrentMap<NotifyListener, EventListener> listeners = nacosListeners.computeIfAbsent(url, k -> new ConcurrentHashMap<>());
515-
EventListener nacosListener = listeners.computeIfAbsent(listener, k -> {
516-
EventListener eventListener = event -> {
517-
if (event instanceof NamingEvent) {
518-
NamingEvent e = (NamingEvent) event;
519-
List<Instance> instances = e.getInstances();
520-
521-
522-
if (isServiceNamesWithCompatibleMode(url)) {
513+
Map<NotifyListener, Map<String, EventListener>> listeners = nacosListeners.computeIfAbsent(url, k -> new ConcurrentHashMap<>());
514+
Map<String, EventListener> eventListenerMap = listeners.computeIfAbsent(listener, k -> new ConcurrentHashMap<>());
515+
EventListener nacosListener = eventListenerMap.computeIfAbsent(serviceName,
516+
name -> event -> {
517+
if (event instanceof NamingEvent) {
518+
NamingEvent e = (NamingEvent) event;
519+
List<Instance> instances = e.getInstances();
520+
if (isServiceNamesWithCompatibleMode(url)) {
521+
522+
// Get all instances with corresponding serviceNames to avoid instance overwrite and but with empty instance mentioned
523+
// in https://github.com/apache/dubbo/issues/5885 and https://github.com/apache/dubbo/issues/5899
524+
NacosInstanceManageUtil.initOrRefreshServiceInstanceList(name, instances);
525+
instances = NacosInstanceManageUtil.getAllCorrespondingServiceInstanceList(name);
526+
}
523527

524-
// Get all instances with corresponding serviceNames to avoid instance overwrite and but with empty instance mentioned
525-
// in https://github.com/apache/dubbo/issues/5885 and https://github.com/apache/dubbo/issues/5899
526-
NacosInstanceManageUtil.initOrRefreshServiceInstanceList(e.getServiceName(), instances);
527-
instances = NacosInstanceManageUtil.getAllCorrespondingServiceInstanceList(e.getServiceName());
528+
notifySubscriber(url, listener, instances);
528529
}
529-
530-
notifySubscriber(url, listener, instances);
531-
}
532-
};
533-
return eventListener;
534-
});
530+
});
535531
namingService.subscribe(serviceName,
536532
getUrl().getParameter(GROUP_KEY, Constants.DEFAULT_GROUP),
537533
nacosListener);
538534
}
539535

540536
private void unsubscribeEventListener(String serviceName, final URL url, final NotifyListener listener)
541537
throws NacosException {
542-
ConcurrentMap<NotifyListener, EventListener> notifyListenerEventListenerConcurrentMap = nacosListeners.get(url);
543-
if(notifyListenerEventListenerConcurrentMap == null){
538+
Map<NotifyListener, Map<String, EventListener>> notifyListenerEventListenerConcurrentMap = nacosListeners.get(url);
539+
if (notifyListenerEventListenerConcurrentMap == null) {
540+
return;
541+
}
542+
Map<String, EventListener> listenerMap = notifyListenerEventListenerConcurrentMap.get(listener);
543+
if (listenerMap == null) {
544544
return;
545545
}
546-
EventListener nacosListener = notifyListenerEventListenerConcurrentMap.get(listener);
547-
if(nacosListener == null){
546+
EventListener nacosListener = listenerMap.remove(serviceName);
547+
if (nacosListener == null) {
548548
return;
549549
}
550550
namingService.unsubscribe(serviceName,

0 commit comments

Comments
 (0)