Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#5608 && #10223): When the custom instance id is empty, the id will be automatically generated. #10812

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(), service.getGroupedServiceName());
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(), service.getGroupedServiceName());
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 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<Instance> instances, String groupedServiceName) {
if (null != instances) {
for (Instance instance : instances) {
setInstanceIdIfEmpty(instance, groupedServiceName);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Instance> instanceList = new ArrayList<>();
Instance instance = new Instance();
instanceList.add(instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<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 groupedServiceName = "test";
InstanceUtil.batchSetInstanceIdIfEmpty(instances, groupedServiceName);
assertNotNull(instance1.getInstanceId());
assertNotNull(instance2.getInstanceId());
assertNotNull(instance3.getInstanceId());
}
}
Loading