From 837012488904e5df40875283d8a2dac84a085c07 Mon Sep 17 00:00:00 2001 From: "blake.qiu" <46370663+Bo-Qiu@users.noreply.github.com> Date: Tue, 1 Aug 2023 15:40:31 +0800 Subject: [PATCH] feat(#5608 && #10223): When the custom instance id is empty, the id will be automatically generated. (#10812) --- .../handler/BatchInstanceRequestHandler.java | 2 + .../rpc/handler/InstanceRequestHandler.java | 2 + .../nacos/naming/utils/InstanceUtil.java | 31 ++++++++++++++++ .../BatchInstanceRequestHandlerTest.java | 2 + .../handler/InstanceRequestHandlerTest.java | 4 +- .../nacos/naming/utils/InstanceUtilTest.java | 37 +++++++++++++++++++ 6 files changed, 77 insertions(+), 1 deletion(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/remote/rpc/handler/BatchInstanceRequestHandler.java b/naming/src/main/java/com/alibaba/nacos/naming/remote/rpc/handler/BatchInstanceRequestHandler.java index f6958238bc2..0b0b158b136 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/remote/rpc/handler/BatchInstanceRequestHandler.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/remote/rpc/handler/BatchInstanceRequestHandler.java @@ -25,6 +25,7 @@ import com.alibaba.nacos.core.remote.RequestHandler; import com.alibaba.nacos.naming.core.v2.pojo.Service; import com.alibaba.nacos.naming.core.v2.service.impl.EphemeralClientOperationServiceImpl; +import com.alibaba.nacos.naming.utils.InstanceUtil; import com.alibaba.nacos.plugin.auth.constant.ActionTypes; import org.springframework.stereotype.Component; @@ -47,6 +48,7 @@ public BatchInstanceRequestHandler(EphemeralClientOperationServiceImpl clientOpe public BatchInstanceResponse handle(BatchInstanceRequest request, RequestMeta meta) throws NacosException { Service service = Service.newService(request.getNamespace(), request.getGroupName(), request.getServiceName(), true); + InstanceUtil.batchSetInstanceIdIfEmpty(request.getInstances(), service.getGroupedServiceName()); switch (request.getType()) { case NamingRemoteConstants.BATCH_REGISTER_INSTANCE: return batchRegisterInstance(service, request, meta); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/remote/rpc/handler/InstanceRequestHandler.java b/naming/src/main/java/com/alibaba/nacos/naming/remote/rpc/handler/InstanceRequestHandler.java index 54d366c4eb9..d2c1129c82d 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/remote/rpc/handler/InstanceRequestHandler.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/remote/rpc/handler/InstanceRequestHandler.java @@ -29,6 +29,7 @@ import com.alibaba.nacos.core.remote.RequestHandler; import com.alibaba.nacos.naming.core.v2.pojo.Service; import com.alibaba.nacos.naming.core.v2.service.impl.EphemeralClientOperationServiceImpl; +import com.alibaba.nacos.naming.utils.InstanceUtil; import com.alibaba.nacos.plugin.auth.constant.ActionTypes; import org.springframework.stereotype.Component; @@ -51,6 +52,7 @@ public InstanceRequestHandler(EphemeralClientOperationServiceImpl clientOperatio public InstanceResponse handle(InstanceRequest request, RequestMeta meta) throws NacosException { Service service = Service .newService(request.getNamespace(), request.getGroupName(), request.getServiceName(), true); + InstanceUtil.setInstanceIdIfEmpty(request.getInstance(), service.getGroupedServiceName()); switch (request.getType()) { case NamingRemoteConstants.REGISTER_INSTANCE: return registerInstance(service, request, meta); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/utils/InstanceUtil.java b/naming/src/main/java/com/alibaba/nacos/naming/utils/InstanceUtil.java index 1d4897bd2ef..6cad09f16b6 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/utils/InstanceUtil.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/utils/InstanceUtil.java @@ -18,12 +18,15 @@ import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.utils.NamingUtils; +import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.naming.constants.Constants; import com.alibaba.nacos.naming.core.v2.metadata.InstanceMetadata; import com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo; import com.alibaba.nacos.naming.core.v2.pojo.Service; +import com.alibaba.nacos.naming.pojo.instance.DefaultInstanceIdGenerator; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -101,4 +104,32 @@ public static Instance deepCopy(Instance source) { target.setMetadata(new HashMap<>(source.getMetadata())); return target; } + + /** + * If the instance id is empty, use the default-instance-id-generator method to set the instance id. + * + * @param instance instance from request + * @param groupedServiceName groupedServiceName from service + */ + public static void setInstanceIdIfEmpty(Instance instance, String groupedServiceName) { + if (null != instance && StringUtils.isEmpty(instance.getInstanceId())) { + DefaultInstanceIdGenerator idGenerator = new DefaultInstanceIdGenerator(groupedServiceName, + instance.getClusterName(), instance.getIp(), instance.getPort()); + instance.setInstanceId(idGenerator.generateInstanceId()); + } + } + + /** + * Batch set instance id if empty. + * + * @param instances instances from request + * @param groupedServiceName groupedServiceName from service + */ + public static void batchSetInstanceIdIfEmpty(List instances, String groupedServiceName) { + if (null != instances) { + for (Instance instance : instances) { + setInstanceIdIfEmpty(instance, groupedServiceName); + } + } + } } diff --git a/naming/src/test/java/com/alibaba/nacos/naming/remote/rpc/handler/BatchInstanceRequestHandlerTest.java b/naming/src/test/java/com/alibaba/nacos/naming/remote/rpc/handler/BatchInstanceRequestHandlerTest.java index 6e104cb9304..83f5617da12 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/remote/rpc/handler/BatchInstanceRequestHandlerTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/remote/rpc/handler/BatchInstanceRequestHandlerTest.java @@ -53,6 +53,8 @@ public class BatchInstanceRequestHandlerTest { public void testHandle() throws NacosException { BatchInstanceRequest batchInstanceRequest = new BatchInstanceRequest(); batchInstanceRequest.setType(NamingRemoteConstants.BATCH_REGISTER_INSTANCE); + batchInstanceRequest.setServiceName("service1"); + batchInstanceRequest.setGroupName("group1"); List instanceList = new ArrayList<>(); Instance instance = new Instance(); instanceList.add(instance); diff --git a/naming/src/test/java/com/alibaba/nacos/naming/remote/rpc/handler/InstanceRequestHandlerTest.java b/naming/src/test/java/com/alibaba/nacos/naming/remote/rpc/handler/InstanceRequestHandlerTest.java index 66380c5bf26..97fd9c4dc99 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/remote/rpc/handler/InstanceRequestHandlerTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/remote/rpc/handler/InstanceRequestHandlerTest.java @@ -50,12 +50,14 @@ public class InstanceRequestHandlerTest { public void testHandle() throws NacosException { InstanceRequest instanceRequest = new InstanceRequest(); instanceRequest.setType(NamingRemoteConstants.REGISTER_INSTANCE); + instanceRequest.setServiceName("service1"); + instanceRequest.setGroupName("group1"); Instance instance = new Instance(); instanceRequest.setInstance(instance); RequestMeta requestMeta = new RequestMeta(); instanceRequestHandler.handle(instanceRequest, requestMeta); Mockito.verify(clientOperationService).registerInstance(Mockito.any(), Mockito.any(), Mockito.anyString()); - + instanceRequest.setType(NamingRemoteConstants.DE_REGISTER_INSTANCE); instanceRequestHandler.handle(instanceRequest, requestMeta); Mockito.verify(clientOperationService).deregisterInstance(Mockito.any(), Mockito.any(), Mockito.anyString()); diff --git a/naming/src/test/java/com/alibaba/nacos/naming/utils/InstanceUtilTest.java b/naming/src/test/java/com/alibaba/nacos/naming/utils/InstanceUtilTest.java index fea4fe19082..9c4c2d18ca1 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/utils/InstanceUtilTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/utils/InstanceUtilTest.java @@ -20,10 +20,13 @@ import com.alibaba.nacos.naming.core.v2.metadata.InstanceMetadata; import com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo; import com.alibaba.nacos.naming.core.v2.pojo.Service; +import com.alibaba.nacos.naming.pojo.instance.DefaultInstanceIdGenerator; import org.junit.Before; import org.junit.Test; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -83,4 +86,38 @@ public void testDeepCopy() { Instance instance = InstanceUtil.deepCopy(source); assertNotNull(instance); } + + @Test + public void testSetInstanceIdIfEmpty() { + Instance instance = new Instance(); + instance.setIp("1.1.1.1"); + instance.setPort(8890); + String groupedServiceName = "test"; + InstanceUtil.setInstanceIdIfEmpty(instance, groupedServiceName); + assertNotNull(instance.getInstanceId()); + DefaultInstanceIdGenerator idGenerator = new DefaultInstanceIdGenerator(groupedServiceName, + instance.getClusterName(), instance.getIp(), instance.getPort()); + assertEquals(instance.getInstanceId(), idGenerator.generateInstanceId()); + String customInsId = "customInstanceId_1"; + Instance instance1 = new Instance(); + instance1.setInstanceId(customInsId); + InstanceUtil.setInstanceIdIfEmpty(instance1, groupedServiceName); + assertEquals(instance1.getInstanceId(), customInsId); + } + + @Test + public void testBatchSetInstanceIdIfEmpty() { + List instances = new ArrayList<>(); + Instance instance1 = new Instance(); + Instance instance2 = new Instance(); + Instance instance3 = new Instance(); + instances.add(instance1); + instances.add(instance2); + instances.add(instance3); + String groupedServiceName = "test"; + InstanceUtil.batchSetInstanceIdIfEmpty(instances, groupedServiceName); + assertNotNull(instance1.getInstanceId()); + assertNotNull(instance2.getInstanceId()); + assertNotNull(instance3.getInstanceId()); + } }