Skip to content

Commit 3627fdb

Browse files
authored
get all nacos instances without subscription (#15126)
1 parent 652a78a commit 3627fdb

4 files changed

Lines changed: 25 additions & 17 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ public void unsubscribe(String serviceName, String group, EventListener eventLis
110110
}
111111
}
112112

113-
public List<Instance> getAllInstances(String serviceName, String group) throws NacosException {
114-
return apply(
115-
() -> nacosConnectionManager.getNamingService().getAllInstances(handleInnerSymbol(serviceName), group));
113+
public List<Instance> getAllInstancesWithoutSubscription(String serviceName, String group) throws NacosException {
114+
return apply(() -> nacosConnectionManager
115+
.getNamingService()
116+
.getAllInstances(handleInnerSymbol(serviceName), group, false));
116117
}
117118

118119
public void registerInstance(String serviceName, String group, Instance instance) throws NacosException {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ public List<URL> lookup(final URL url) {
158158
List<URL> urls = new LinkedList<>();
159159
Set<String> serviceNames = getServiceNames(url, null);
160160
for (String serviceName : serviceNames) {
161-
List<Instance> instances =
162-
namingService.getAllInstances(serviceName, getUrl().getGroup(Constants.DEFAULT_GROUP));
161+
List<Instance> instances = namingService.getAllInstancesWithoutSubscription(
162+
serviceName, getUrl().getGroup(Constants.DEFAULT_GROUP));
163163
urls.addAll(buildURLs(url, instances));
164164
}
165165
return urls;
@@ -267,8 +267,8 @@ private void doSubscribe(final URL url, final NacosAggregateListener listener, f
267267
* in https://github.com/apache/dubbo/issues/5978
268268
*/
269269
for (String serviceName : serviceNames) {
270-
List<Instance> instances =
271-
namingService.getAllInstances(serviceName, getUrl().getGroup(Constants.DEFAULT_GROUP));
270+
List<Instance> instances = namingService.getAllInstancesWithoutSubscription(
271+
serviceName, getUrl().getGroup(Constants.DEFAULT_GROUP));
272272
notifySubscriber(url, serviceName, listener, instances);
273273
}
274274
for (String serviceName : serviceNames) {
@@ -277,8 +277,8 @@ private void doSubscribe(final URL url, final NacosAggregateListener listener, f
277277
} else {
278278
for (String serviceName : serviceNames) {
279279
List<Instance> instances = new LinkedList<>();
280-
instances.addAll(
281-
namingService.getAllInstances(serviceName, getUrl().getGroup(Constants.DEFAULT_GROUP)));
280+
instances.addAll(namingService.getAllInstancesWithoutSubscription(
281+
serviceName, getUrl().getGroup(Constants.DEFAULT_GROUP)));
282282
String serviceInterface = serviceName;
283283
String[] segments = serviceName.split(SERVICE_NAME_SEPARATOR, -1);
284284
if (segments.length == 4) {

dubbo-registry/dubbo-registry-nacos/src/test/java/org/apache/dubbo/registry/nacos/MockNamingService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public List<Instance> getAllInstances(String serviceName, boolean subscribe) thr
8686
}
8787

8888
@Override
89-
public List<Instance> getAllInstances(String serviceName, String groupName, boolean subscribe) {
89+
public List<Instance> getAllInstances(String serviceName, String groupName, boolean subscribe)
90+
throws NacosException {
9091
return null;
9192
}
9293

dubbo-registry/dubbo-registry-nacos/src/test/java/org/apache/dubbo/registry/nacos/NacosNamingServiceWrapperTest.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ void testSuccess() {
661661
public void registerInstance(String serviceName, String groupName, Instance instance) {}
662662

663663
@Override
664-
public List<Instance> getAllInstances(String serviceName, String groupName) {
664+
public List<Instance> getAllInstances(String serviceName, String groupName, boolean subscribe) {
665665
return null;
666666
}
667667
};
@@ -674,7 +674,7 @@ public List<Instance> getAllInstances(String serviceName, String groupName) {
674674
Assertions.fail(e);
675675
}
676676
try {
677-
nacosNamingServiceWrapper.getAllInstances("Test", "Test");
677+
nacosNamingServiceWrapper.getAllInstancesWithoutSubscription("Test", "Test");
678678
} catch (NacosException e) {
679679
Assertions.fail(e);
680680
}
@@ -690,7 +690,8 @@ public void registerInstance(String serviceName, String groupName, Instance inst
690690
}
691691

692692
@Override
693-
public List<Instance> getAllInstances(String serviceName, String groupName) throws NacosException {
693+
public List<Instance> getAllInstances(String serviceName, String groupName, boolean subscribe)
694+
throws NacosException {
694695
throw new NacosException();
695696
}
696697
};
@@ -699,7 +700,9 @@ public List<Instance> getAllInstances(String serviceName, String groupName) thro
699700
new NacosNamingServiceWrapper(new NacosConnectionManager(namingService), 0, 0);
700701
Assertions.assertThrows(
701702
NacosException.class, () -> nacosNamingServiceWrapper.registerInstance("Test", "Test", null));
702-
Assertions.assertThrows(NacosException.class, () -> nacosNamingServiceWrapper.getAllInstances("Test", "Test"));
703+
Assertions.assertThrows(
704+
NacosException.class,
705+
() -> nacosNamingServiceWrapper.getAllInstancesWithoutSubscription("Test", "Test"));
703706
}
704707

705708
@Test
@@ -717,7 +720,8 @@ public void registerInstance(String serviceName, String groupName, Instance inst
717720
}
718721

719722
@Override
720-
public List<Instance> getAllInstances(String serviceName, String groupName) throws NacosException {
723+
public List<Instance> getAllInstances(String serviceName, String groupName, boolean subscribe)
724+
throws NacosException {
721725
if (count2.incrementAndGet() < 10) {
722726
throw new NacosException();
723727
}
@@ -735,9 +739,11 @@ public List<Instance> getAllInstances(String serviceName, String groupName) thro
735739
Assertions.fail(e);
736740
}
737741

738-
Assertions.assertThrows(NacosException.class, () -> nacosNamingServiceWrapper.getAllInstances("Test", "Test"));
742+
Assertions.assertThrows(
743+
NacosException.class,
744+
() -> nacosNamingServiceWrapper.getAllInstancesWithoutSubscription("Test", "Test"));
739745
try {
740-
nacosNamingServiceWrapper.getAllInstances("Test", "Test");
746+
nacosNamingServiceWrapper.getAllInstancesWithoutSubscription("Test", "Test");
741747
} catch (NacosException e) {
742748
Assertions.fail(e);
743749
}

0 commit comments

Comments
 (0)