Skip to content

Commit

Permalink
feat(#5608 && #10223): When the custom instance id is empty, the id w…
Browse files Browse the repository at this point in the history
…ill be automatically generated.
  • Loading branch information
Bo-Qiu committed Jul 25, 2023
1 parent f986916 commit 0683904
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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<Instance> instances, String serviceName) {
if (null != instances) {
for (Instance instance : instances) {
setInstanceIdIfEmpty(instance, serviceName);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Instance> 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());
}
}

0 comments on commit 0683904

Please sign in to comment.