You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2022/11/28 09:25:18 UTC
[dubbo] branch master updated: Fix dubbo service name bug (#11038)
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 1a9eda5822 Fix dubbo service name bug (#11038)
1a9eda5822 is described below
commit 1a9eda58220ea1f57004b2fb8165d920d2277695
Author: 胡俊 <51...@qq.com>
AuthorDate: Mon Nov 28 17:25:06 2022 +0800
Fix dubbo service name bug (#11038)
---
.../registry/nacos/NacosAggregateListener.java | 70 --------------------
.../apache/dubbo/registry/nacos/NacosRegistry.java | 74 ++++++++++------------
2 files changed, 33 insertions(+), 111 deletions(-)
diff --git a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosAggregateListener.java b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosAggregateListener.java
deleted file mode 100644
index 5593acd915..0000000000
--- a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosAggregateListener.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.dubbo.registry.nacos;
-
-import org.apache.dubbo.common.utils.ConcurrentHashSet;
-import org.apache.dubbo.registry.NotifyListener;
-
-import com.alibaba.nacos.api.naming.pojo.Instance;
-
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.stream.Collectors;
-
-public class NacosAggregateListener {
- private final NotifyListener notifyListener;
- private final Set<String> serviceNames = new ConcurrentHashSet<>();
- private final Map<String, List<Instance>> serviceInstances = new ConcurrentHashMap<>();
-
- public NacosAggregateListener(NotifyListener notifyListener) {
- this.notifyListener = notifyListener;
- }
-
- public List<Instance> saveAndAggregateAllInstances(String serviceName, List<Instance> instances) {
- serviceNames.add(serviceName);
- serviceInstances.put(serviceName, instances);
- return serviceInstances.values().stream().flatMap(List::stream).collect(Collectors.toList());
- }
-
- public NotifyListener getNotifyListener() {
- return notifyListener;
- }
-
- public Set<String> getServiceNames() {
- return serviceNames;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (o == null || getClass() != o.getClass()) {
- return false;
- }
- NacosAggregateListener that = (NacosAggregateListener) o;
- return Objects.equals(notifyListener, that.notifyListener) && Objects.equals(serviceNames, that.serviceNames) && Objects.equals(serviceInstances, that.serviceInstances);
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(notifyListener, serviceNames, serviceInstances);
- }
-}
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 060bf2013a..af356373ee 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,9 +123,7 @@ public class NacosRegistry extends FailbackRegistry {
private final NacosNamingServiceWrapper namingService;
- private final ConcurrentMap<URL, ConcurrentMap<NotifyListener, NacosAggregateListener>> originToAggregateListener = new ConcurrentHashMap<>();
-
- private final ConcurrentMap<URL, ConcurrentMap<NacosAggregateListener, EventListener>> nacosListeners = new ConcurrentHashMap<>();
+ private final ConcurrentMap<URL, ConcurrentMap<NotifyListener, EventListener>> nacosListeners = new ConcurrentHashMap<>();
public NacosRegistry(URL url, NacosNamingServiceWrapper namingService) {
super(url);
@@ -180,10 +178,7 @@ public class NacosRegistry extends FailbackRegistry {
@Override
public void doSubscribe(final URL url, final NotifyListener listener) {
- NacosAggregateListener nacosAggregateListener = new NacosAggregateListener(listener);
- originToAggregateListener.computeIfAbsent(url, k -> new ConcurrentHashMap<>()).put(listener, nacosAggregateListener);
-
- Set<String> serviceNames = getServiceNames(url, nacosAggregateListener);
+ Set<String> serviceNames = getServiceNames(url, listener);
//Set corresponding serviceNames for easy search later
if (isServiceNamesWithCompatibleMode(url)) {
@@ -191,12 +186,14 @@ public class NacosRegistry extends FailbackRegistry {
NacosInstanceManageUtil.setCorrespondingServiceNames(serviceName, serviceNames);
}
}
- doSubscribe(url, nacosAggregateListener, serviceNames);
+
+ doSubscribe(url, listener, serviceNames);
}
- private void doSubscribe(final URL url, final NacosAggregateListener listener, final Set<String> serviceNames) {
+ private void doSubscribe(final URL url, final NotifyListener listener, final Set<String> serviceNames) {
execute(namingService -> {
if (isServiceNamesWithCompatibleMode(url)) {
+ List<Instance> allCorrespondingInstanceList = new ArrayList<>();
/**
* Get all instances with serviceNames to avoid instance overwrite and but with empty instance mentioned
@@ -211,8 +208,9 @@ public class NacosRegistry extends FailbackRegistry {
List<Instance> instances = namingService.getAllInstances(serviceName,
getUrl().getParameter(GROUP_KEY, Constants.DEFAULT_GROUP));
NacosInstanceManageUtil.initOrRefreshServiceInstanceList(serviceName, instances);
- notifySubscriber(url, serviceName, listener, instances);
+ allCorrespondingInstanceList.addAll(instances);
}
+ notifySubscriber(url, listener, allCorrespondingInstanceList);
for (String serviceName : serviceNames) {
subscribeEventListener(serviceName, url, listener);
}
@@ -221,7 +219,7 @@ public class NacosRegistry extends FailbackRegistry {
for (String serviceName : serviceNames) {
instances.addAll(namingService.getAllInstances(serviceName
, getUrl().getParameter(GROUP_KEY, Constants.DEFAULT_GROUP)));
- notifySubscriber(url, serviceName, listener, instances);
+ notifySubscriber(url, listener, instances);
subscribeEventListener(serviceName, url, listener);
}
}
@@ -244,24 +242,18 @@ public class NacosRegistry extends FailbackRegistry {
public void doUnsubscribe(URL url, NotifyListener listener) {
if (isAdminProtocol(url)) {
shutdownServiceNamesLookup();
- } else {
- Map<NotifyListener, NacosAggregateListener> listenerMap = originToAggregateListener.get(url);
- NacosAggregateListener nacosAggregateListener = listenerMap.remove(listener);
- if (nacosAggregateListener != null) {
- Set<String> serviceNames = getServiceNames(url, nacosAggregateListener);
+ }
+ else {
+ Set<String> serviceNames = getServiceNames(url, listener);
- doUnsubscribe(url, nacosAggregateListener, serviceNames);
- }
- if (listenerMap.isEmpty()) {
- originToAggregateListener.remove(url);
- }
+ doUnsubscribe(url, listener, serviceNames);
}
}
- private void doUnsubscribe(final URL url, final NacosAggregateListener nacosAggregateListener, final Set<String> serviceNames) {
+ private void doUnsubscribe(final URL url, final NotifyListener listener, final Set<String> serviceNames) {
execute(namingService -> {
for (String serviceName : serviceNames) {
- unsubscribeEventListener(serviceName, url, nacosAggregateListener);
+ unsubscribeEventListener(serviceName, url, listener);
}
});
}
@@ -279,7 +271,7 @@ public class NacosRegistry extends FailbackRegistry {
* @param listener {@link NotifyListener}
* @return non-null
*/
- private Set<String> getServiceNames(URL url, NacosAggregateListener listener) {
+ private Set<String> getServiceNames(URL url, NotifyListener listener) {
if (isAdminProtocol(url)) {
scheduleServiceNamesLookup(url, listener);
return getServiceNamesForOps(url);
@@ -313,7 +305,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)
@@ -364,7 +356,7 @@ public class NacosRegistry extends FailbackRegistry {
return ADMIN_PROTOCOL.equals(url.getProtocol());
}
- private void scheduleServiceNamesLookup(final URL url, final NacosAggregateListener listener) {
+ private void scheduleServiceNamesLookup(final URL url, final NotifyListener listener) {
if (scheduledExecutorService == null) {
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleAtFixedRate(() -> {
@@ -517,9 +509,9 @@ public class NacosRegistry extends FailbackRegistry {
return urls;
}
- private void subscribeEventListener(String serviceName, final URL url, final NacosAggregateListener listener)
+ private void subscribeEventListener(String serviceName, final URL url, final NotifyListener listener)
throws NacosException {
- ConcurrentMap<NacosAggregateListener, EventListener> listeners = nacosListeners.computeIfAbsent(url, k -> new ConcurrentHashMap<>());
+ ConcurrentMap<NotifyListener, EventListener> listeners = nacosListeners.computeIfAbsent(url, k -> new ConcurrentHashMap<>());
EventListener nacosListener = listeners.computeIfAbsent(listener, k -> {
EventListener eventListener = event -> {
if (event instanceof NamingEvent) {
@@ -531,11 +523,11 @@ public class NacosRegistry extends FailbackRegistry {
// 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);
+ NacosInstanceManageUtil.initOrRefreshServiceInstanceList(e.getServiceName(), instances);
+ instances = NacosInstanceManageUtil.getAllCorrespondingServiceInstanceList(e.getServiceName());
}
- notifySubscriber(url, serviceName, listener, instances);
+ notifySubscriber(url, listener, instances);
}
};
return eventListener;
@@ -545,14 +537,14 @@ public class NacosRegistry extends FailbackRegistry {
nacosListener);
}
- private void unsubscribeEventListener(String serviceName, final URL url, final NacosAggregateListener listener)
+ private void unsubscribeEventListener(String serviceName, final URL url, final NotifyListener listener)
throws NacosException {
- ConcurrentMap<NacosAggregateListener, EventListener> notifyListenerEventListenerConcurrentMap = nacosListeners.get(url);
- if (notifyListenerEventListenerConcurrentMap == null) {
+ ConcurrentMap<NotifyListener, EventListener> notifyListenerEventListenerConcurrentMap = nacosListeners.get(url);
+ if(notifyListenerEventListenerConcurrentMap == null){
return;
}
EventListener nacosListener = notifyListenerEventListenerConcurrentMap.get(listener);
- if (nacosListener == null) {
+ if(nacosListener == null){
return;
}
namingService.unsubscribe(serviceName,
@@ -567,14 +559,14 @@ public class NacosRegistry extends FailbackRegistry {
* @param listener {@link NotifyListener}
* @param instances all {@link Instance instances}
*/
- private void notifySubscriber(URL url, String serviceName, NacosAggregateListener listener, Collection<Instance> instances) {
+ private void notifySubscriber(URL url, NotifyListener listener, Collection<Instance> instances) {
List<Instance> enabledInstances = new LinkedList<>(instances);
if (enabledInstances.size() > 0) {
// Instances
filterEnabledInstances(enabledInstances);
}
- List<URL> aggregatedUrls = toUrlWithEmpty(url, listener.saveAndAggregateAllInstances(serviceName, enabledInstances));
- NacosRegistry.this.notify(url, listener.getNotifyListener(), aggregatedUrls);
+ List<URL> urls = toUrlWithEmpty(url, enabledInstances);
+ NacosRegistry.this.notify(url, listener, urls);
}
/**