From 753a30b37d21bada8511f8e26a3331bab26f202a Mon Sep 17 00:00:00 2001 From: zrlw Date: Wed, 15 Nov 2023 16:21:51 +0800 Subject: [PATCH] strip groupNamePrefix of instance serviceName at register or deregister (#11354) --- .../client/naming/NacosNamingService.java | 22 ++++++++++ .../client/naming/NacosNamingServiceTest.java | 40 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index fa7f063becf..18583cc8f94 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -142,6 +142,7 @@ 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); } @@ -149,6 +150,7 @@ public void registerInstance(String serviceName, String groupName, Instance inst public void batchRegisterInstance(String serviceName, String groupName, List instances) throws NacosException { NamingUtils.batchCheckInstanceIsLegal(instances); + batchCheckAndStripGroupNamePrefix(instances, groupName); clientProxy.batchRegisterService(serviceName, groupName, instances); } @@ -156,6 +158,7 @@ public void batchRegisterInstance(String serviceName, String groupName, List instances) throws NacosException { NamingUtils.batchCheckInstanceIsLegal(instances); + batchCheckAndStripGroupNamePrefix(instances, groupName); clientProxy.batchDeregisterService(serviceName, groupName, instances); } @@ -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); } @@ -470,4 +474,22 @@ public void shutDown() throws NacosException { NotifyCenter.deregisterSubscriber(changeNotifier); } + + private void batchCheckAndStripGroupNamePrefix(List 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)); + } + } } diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java index 7fb4e091d41..1b9dcfbb707 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java @@ -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 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 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();