Skip to content

Commit

Permalink
strip groupNamePrefix of instance serviceName at register or deregist…
Browse files Browse the repository at this point in the history
…er (#11354)
  • Loading branch information
zrlw authored Nov 15, 2023
1 parent a935fa1 commit 753a30b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,23 @@ public void registerInstance(String serviceName, Instance instance) throws Nacos
@Override
public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {
NamingUtils.checkInstanceIsLegal(instance);
checkAndStripGroupNamePrefix(instance, groupName);
clientProxy.registerService(serviceName, groupName, instance);
}

@Override
public void batchRegisterInstance(String serviceName, String groupName, List<Instance> instances)
throws NacosException {
NamingUtils.batchCheckInstanceIsLegal(instances);
batchCheckAndStripGroupNamePrefix(instances, groupName);
clientProxy.batchRegisterService(serviceName, groupName, instances);
}

@Override
public void batchDeregisterInstance(String serviceName, String groupName, List<Instance> instances)
throws NacosException {
NamingUtils.batchCheckInstanceIsLegal(instances);
batchCheckAndStripGroupNamePrefix(instances, groupName);
clientProxy.batchDeregisterService(serviceName, groupName, instances);
}

Expand Down Expand Up @@ -191,6 +194,7 @@ public void deregisterInstance(String serviceName, Instance instance) throws Nac

@Override
public void deregisterInstance(String serviceName, String groupName, Instance instance) throws NacosException {
checkAndStripGroupNamePrefix(instance, groupName);
clientProxy.deregisterService(serviceName, groupName, instance);
}

Expand Down Expand Up @@ -470,4 +474,22 @@ public void shutDown() throws NacosException {
NotifyCenter.deregisterSubscriber(changeNotifier);

}

private void batchCheckAndStripGroupNamePrefix(List<Instance> instances, String groupName) throws NacosException {
for (Instance instance : instances) {
checkAndStripGroupNamePrefix(instance, groupName);
}
}

private void checkAndStripGroupNamePrefix(Instance instance, String groupName) throws NacosException {
String serviceName = instance.getServiceName();
if (serviceName != null) {
String groupNameOfInstance = NamingUtils.getGroupName(serviceName);
if (!groupName.equals(groupNameOfInstance)) {
throw new NacosException(NacosException.CLIENT_INVALID_PARAM, String.format(
"wrong group name prefix of instance service name! it should be: %s, Instance: %s", groupName, instance));
}
instance.setServiceName(NamingUtils.getServiceName(serviceName));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,46 @@ public void testBatchRegisterInstance() throws NacosException {
argThat(instances -> CollectionUtils.isEqualCollection(instanceList, instances)));
}

@Test
public void testBatchRegisterInstanceWithGroupNamePrefix() throws NacosException {
Instance instance = new Instance();
String serviceName = "service1";
String ip = "1.1.1.1";
int port = 10000;
instance.setServiceName(Constants.DEFAULT_GROUP + "@@" + serviceName);
instance.setEphemeral(true);
instance.setPort(port);
instance.setIp(ip);
List<Instance> instanceList = new ArrayList<>();
instanceList.add(instance);
//when
client.batchRegisterInstance(serviceName, Constants.DEFAULT_GROUP, instanceList);
//then
verify(proxy, times(1)).batchRegisterService(eq(serviceName), eq(Constants.DEFAULT_GROUP),
argThat(instances -> CollectionUtils.isEqualCollection(instanceList, instances)));
}

@Test
public void testBatchRegisterInstanceWithWrongGroupNamePrefix() throws NacosException {
Instance instance = new Instance();
String serviceName = "service1";
String ip = "1.1.1.1";
int port = 10000;
instance.setServiceName("WrongGroup" + "@@" + serviceName);
instance.setEphemeral(true);
instance.setPort(port);
instance.setIp(ip);
List<Instance> instanceList = new ArrayList<>();
instanceList.add(instance);
//when
try {
client.batchRegisterInstance(serviceName, Constants.DEFAULT_GROUP, instanceList);
} catch (Exception e) {
Assert.assertTrue(e instanceof NacosException);
Assert.assertTrue(e.getMessage().contains("wrong group name prefix of instance service name"));
}
}

@Test
public void testBatchDeRegisterInstance() throws NacosException {
Instance instance = new Instance();
Expand Down

0 comments on commit 753a30b

Please sign in to comment.