Skip to content

Commit

Permalink
get all nacos instances without subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
zrlw committed Feb 12, 2025
1 parent 0871406 commit bbce7eb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ public void unsubscribe(String serviceName, String group, EventListener eventLis
}
}

public List<Instance> getAllInstances(String serviceName, String group) throws NacosException {
return apply(
() -> nacosConnectionManager.getNamingService().getAllInstances(handleInnerSymbol(serviceName), group));
public List<Instance> getAllInstancesWithoutSubscription(String serviceName, String group) throws NacosException {
return apply(() -> nacosConnectionManager
.getNamingService()
.getAllInstances(handleInnerSymbol(serviceName), group, false));
}

public void registerInstance(String serviceName, String group, Instance instance) throws NacosException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ public List<URL> lookup(final URL url) {
List<URL> urls = new LinkedList<>();
Set<String> serviceNames = getServiceNames(url, null);
for (String serviceName : serviceNames) {
List<Instance> instances =
namingService.getAllInstances(serviceName, getUrl().getGroup(Constants.DEFAULT_GROUP));
List<Instance> instances = namingService.getAllInstancesWithoutSubscription(
serviceName, getUrl().getGroup(Constants.DEFAULT_GROUP));
urls.addAll(buildURLs(url, instances));
}
return urls;
Expand Down Expand Up @@ -270,8 +270,8 @@ private void doSubscribe(final URL url, final NacosAggregateListener listener, f
* in https://github.com/apache/dubbo/issues/5978
*/
for (String serviceName : serviceNames) {
List<Instance> instances =
namingService.getAllInstances(serviceName, getUrl().getGroup(Constants.DEFAULT_GROUP));
List<Instance> instances = namingService.getAllInstancesWithoutSubscription(
serviceName, getUrl().getGroup(Constants.DEFAULT_GROUP));
notifySubscriber(url, serviceName, listener, instances);
}
for (String serviceName : serviceNames) {
Expand All @@ -280,8 +280,8 @@ private void doSubscribe(final URL url, final NacosAggregateListener listener, f
} else {
for (String serviceName : serviceNames) {
List<Instance> instances = new LinkedList<>();
instances.addAll(
namingService.getAllInstances(serviceName, getUrl().getGroup(Constants.DEFAULT_GROUP)));
instances.addAll(namingService.getAllInstancesWithoutSubscription(
serviceName, getUrl().getGroup(Constants.DEFAULT_GROUP)));
String serviceInterface = serviceName;
String[] segments = serviceName.split(SERVICE_NAME_SEPARATOR, -1);
if (segments.length == 4) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public List<Instance> getAllInstances(String serviceName, boolean subscribe) thr
}

@Override
public List<Instance> getAllInstances(String serviceName, String groupName, boolean subscribe) {
public List<Instance> getAllInstances(String serviceName, String groupName, boolean subscribe)
throws NacosException {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ void testSuccess() {
public void registerInstance(String serviceName, String groupName, Instance instance) {}

@Override
public List<Instance> getAllInstances(String serviceName, String groupName) {
public List<Instance> getAllInstances(String serviceName, String groupName, boolean subscribe) {
return null;
}
};
Expand All @@ -674,7 +674,7 @@ public List<Instance> getAllInstances(String serviceName, String groupName) {
Assertions.fail(e);
}
try {
nacosNamingServiceWrapper.getAllInstances("Test", "Test");
nacosNamingServiceWrapper.getAllInstancesWithoutSubscription("Test", "Test");
} catch (NacosException e) {
Assertions.fail(e);
}
Expand All @@ -690,7 +690,8 @@ public void registerInstance(String serviceName, String groupName, Instance inst
}

@Override
public List<Instance> getAllInstances(String serviceName, String groupName) throws NacosException {
public List<Instance> getAllInstances(String serviceName, String groupName, boolean subscribe)
throws NacosException {
throw new NacosException();
}
};
Expand All @@ -699,7 +700,9 @@ public List<Instance> getAllInstances(String serviceName, String groupName) thro
new NacosNamingServiceWrapper(new NacosConnectionManager(namingService), 0, 0);
Assertions.assertThrows(
NacosException.class, () -> nacosNamingServiceWrapper.registerInstance("Test", "Test", null));
Assertions.assertThrows(NacosException.class, () -> nacosNamingServiceWrapper.getAllInstances("Test", "Test"));
Assertions.assertThrows(
NacosException.class,
() -> nacosNamingServiceWrapper.getAllInstancesWithoutSubscription("Test", "Test"));
}

@Test
Expand All @@ -717,7 +720,8 @@ public void registerInstance(String serviceName, String groupName, Instance inst
}

@Override
public List<Instance> getAllInstances(String serviceName, String groupName) throws NacosException {
public List<Instance> getAllInstances(String serviceName, String groupName, boolean subscribe)
throws NacosException {
if (count2.incrementAndGet() < 10) {
throw new NacosException();
}
Expand All @@ -735,9 +739,11 @@ public List<Instance> getAllInstances(String serviceName, String groupName) thro
Assertions.fail(e);
}

Assertions.assertThrows(NacosException.class, () -> nacosNamingServiceWrapper.getAllInstances("Test", "Test"));
Assertions.assertThrows(
NacosException.class,
() -> nacosNamingServiceWrapper.getAllInstancesWithoutSubscription("Test", "Test"));
try {
nacosNamingServiceWrapper.getAllInstances("Test", "Test");
nacosNamingServiceWrapper.getAllInstancesWithoutSubscription("Test", "Test");
} catch (NacosException e) {
Assertions.fail(e);
}
Expand Down

0 comments on commit bbce7eb

Please sign in to comment.