strip groupNamePrefix of instance serviceName at register or deregister (#11354)
This commit is contained in:
parent
a935fa1092
commit
753a30b37d
@ -142,6 +142,7 @@ public class NacosNamingService implements NamingService {
|
||||
@Override
|
||||
public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {
|
||||
NamingUtils.checkInstanceIsLegal(instance);
|
||||
checkAndStripGroupNamePrefix(instance, groupName);
|
||||
clientProxy.registerService(serviceName, groupName, instance);
|
||||
}
|
||||
|
||||
@ -149,6 +150,7 @@ public class NacosNamingService implements NamingService {
|
||||
public void batchRegisterInstance(String serviceName, String groupName, List<Instance> instances)
|
||||
throws NacosException {
|
||||
NamingUtils.batchCheckInstanceIsLegal(instances);
|
||||
batchCheckAndStripGroupNamePrefix(instances, groupName);
|
||||
clientProxy.batchRegisterService(serviceName, groupName, instances);
|
||||
}
|
||||
|
||||
@ -156,6 +158,7 @@ public class NacosNamingService implements NamingService {
|
||||
public void batchDeregisterInstance(String serviceName, String groupName, List<Instance> instances)
|
||||
throws NacosException {
|
||||
NamingUtils.batchCheckInstanceIsLegal(instances);
|
||||
batchCheckAndStripGroupNamePrefix(instances, groupName);
|
||||
clientProxy.batchDeregisterService(serviceName, groupName, instances);
|
||||
}
|
||||
|
||||
@ -191,6 +194,7 @@ public class NacosNamingService implements NamingService {
|
||||
|
||||
@Override
|
||||
public void deregisterInstance(String serviceName, String groupName, Instance instance) throws NacosException {
|
||||
checkAndStripGroupNamePrefix(instance, groupName);
|
||||
clientProxy.deregisterService(serviceName, groupName, instance);
|
||||
}
|
||||
|
||||
@ -470,4 +474,22 @@ public class NacosNamingService implements NamingService {
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -120,6 +120,46 @@ public class NacosNamingServiceTest {
|
||||
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();
|
||||
|
Loading…
Reference in New Issue
Block a user