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..c836ff56814 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(), request.getServiceName()); 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..5662f6504e3 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(), request.getServiceName()); 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..60ab4873b15 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 serviceName serviceName from request + */ + public static void setInstanceIdIfEmpty(Instance instance, String serviceName) { + if (null != instance && StringUtils.isEmpty(instance.getInstanceId())) { + DefaultInstanceIdGenerator idGenerator = new DefaultInstanceIdGenerator(serviceName, + instance.getClusterName(), instance.getIp(), instance.getPort()); + instance.setInstanceId(idGenerator.generateInstanceId()); + } + } + + /** + * Batch set instance id if empty. + * + * @param instances instances from request + * @param serviceName serviceName from request + */ + public static void batchSetInstanceIdIfEmpty(List instances, String serviceName) { + if (null != instances) { + for (Instance instance : instances) { + setInstanceIdIfEmpty(instance, serviceName); + } + } + } } 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..262986d0bcf 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,14 +20,18 @@ 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; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; public class InstanceUtilTest { @@ -83,4 +87,45 @@ 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); + instance.setServiceName("test1"); + String serviceName = "test"; + InstanceUtil.setInstanceIdIfEmpty(instance, serviceName); + assertNotNull(instance.getInstanceId()); + + DefaultInstanceIdGenerator idGenerator = new DefaultInstanceIdGenerator(instance.getServiceName(), + instance.getClusterName(), instance.getIp(), instance.getPort()); + assertEquals(instance.getInstanceId(), idGenerator.generateInstanceId()); + + DefaultInstanceIdGenerator idGenerator1 = new DefaultInstanceIdGenerator(serviceName, instance.getClusterName(), + instance.getIp(), instance.getPort()); + assertNotEquals(instance.getInstanceId(), idGenerator1.generateInstanceId()); + + String customInsId = "customInstanceId_1"; + Instance instance1 = new Instance(); + instance1.setInstanceId(customInsId); + InstanceUtil.setInstanceIdIfEmpty(instance1, serviceName); + 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 serviceName = "test"; + InstanceUtil.batchSetInstanceIdIfEmpty(instances, serviceName); + assertNotNull(instance1.getInstanceId()); + assertNotNull(instance2.getInstanceId()); + assertNotNull(instance3.getInstanceId()); + } }