feat/support Batch DeRegister (#9330)

* support Batch DeRegister

* Adjust the de registration method

* use batchRegisterService
This commit is contained in:
陈浩 Nineteen 2022-10-24 10:01:06 +08:00 committed by GitHub
parent 1076d5cf61
commit 308d772a9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 132 additions and 0 deletions

View File

@ -107,6 +107,17 @@ public interface NamingService {
*/ */
void batchRegisterInstance(String serviceName, String groupName, List<Instance> instances) throws NacosException; void batchRegisterInstance(String serviceName, String groupName, List<Instance> instances) throws NacosException;
/**
* batch deRegister instance to service with specified instance properties.
*
* @param serviceName name of service
* @param groupName group of service
* @param instances instances to deRegister
* @throws NacosException nacos exception
* @since 2.2.0
*/
void batchDeregisterInstance(String serviceName, String groupName, List<Instance> instances) throws NacosException;
/** /**
* deregister instance from a service. * deregister instance from a service.
* *

View File

@ -156,6 +156,13 @@ public class NacosNamingService implements NamingService {
clientProxy.batchRegisterService(serviceName, groupName, instances); clientProxy.batchRegisterService(serviceName, groupName, instances);
} }
@Override
public void batchDeregisterInstance(String serviceName, String groupName, List<Instance> instances)
throws NacosException {
NamingUtils.batchCheckInstanceIsLegal(instances);
clientProxy.batchDeregisterService(serviceName, groupName, instances);
}
@Override @Override
public void deregisterInstance(String serviceName, String ip, int port) throws NacosException { public void deregisterInstance(String serviceName, String ip, int port) throws NacosException {
deregisterInstance(serviceName, ip, port, Constants.DEFAULT_CLUSTER_NAME); deregisterInstance(serviceName, ip, port, Constants.DEFAULT_CLUSTER_NAME);

View File

@ -55,6 +55,17 @@ public interface NamingClientProxy extends Closeable {
*/ */
void batchRegisterService(String serviceName, String groupName, List<Instance> instances) throws NacosException; void batchRegisterService(String serviceName, String groupName, List<Instance> instances) throws NacosException;
/**
* Batch register instance to service with specified instance properties.
*
* @param serviceName service name
* @param groupName group name
* @param instances deRegister instance
* @throws NacosException nacos exception
* @since 2.2.0
*/
void batchDeregisterService(String serviceName, String groupName, List<Instance> instances) throws NacosException;
/** /**
* Deregister instance from a service. * Deregister instance from a service.
* *

View File

@ -107,6 +107,17 @@ public class NamingClientProxyDelegate implements NamingClientProxy {
NAMING_LOGGER.info("batchRegisterInstance instances: {} ,serviceName: {} finish.", instances, serviceName); NAMING_LOGGER.info("batchRegisterInstance instances: {} ,serviceName: {} finish.", instances, serviceName);
} }
@Override
public void batchDeregisterService(String serviceName, String groupName, List<Instance> instances)
throws NacosException {
NAMING_LOGGER.info("batch DeregisterInstance instances: {} ,serviceName: {} begin.", instances, serviceName);
if (CollectionUtils.isEmpty(instances)) {
NAMING_LOGGER.warn("batch DeregisterInstance instances is Empty:{}", instances);
}
grpcClientProxy.batchDeregisterService(serviceName, groupName, instances);
NAMING_LOGGER.info("batch DeregisterInstance instances: {} ,serviceName: {} finish.", instances, serviceName);
}
@Override @Override
public void deregisterService(String serviceName, String groupName, Instance instance) throws NacosException { public void deregisterService(String serviceName, String groupName, Instance instance) throws NacosException {
getExecuteClientProxy(instance).deregisterService(serviceName, groupName, instance); getExecuteClientProxy(instance).deregisterService(serviceName, groupName, instance);

View File

@ -33,6 +33,7 @@ import com.alibaba.nacos.api.naming.remote.response.BatchInstanceResponse;
import com.alibaba.nacos.api.naming.remote.response.QueryServiceResponse; import com.alibaba.nacos.api.naming.remote.response.QueryServiceResponse;
import com.alibaba.nacos.api.naming.remote.response.ServiceListResponse; import com.alibaba.nacos.api.naming.remote.response.ServiceListResponse;
import com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse; import com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse;
import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.api.remote.RemoteConstants; import com.alibaba.nacos.api.remote.RemoteConstants;
import com.alibaba.nacos.api.remote.response.Response; import com.alibaba.nacos.api.remote.response.Response;
import com.alibaba.nacos.api.remote.response.ResponseCode; import com.alibaba.nacos.api.remote.response.ResponseCode;
@ -42,6 +43,8 @@ import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder;
import com.alibaba.nacos.client.naming.event.ServerListChangedEvent; import com.alibaba.nacos.client.naming.event.ServerListChangedEvent;
import com.alibaba.nacos.client.naming.remote.AbstractNamingClientProxy; import com.alibaba.nacos.client.naming.remote.AbstractNamingClientProxy;
import com.alibaba.nacos.client.naming.remote.gprc.redo.NamingGrpcRedoService; import com.alibaba.nacos.client.naming.remote.gprc.redo.NamingGrpcRedoService;
import com.alibaba.nacos.client.naming.remote.gprc.redo.data.BatchInstanceRedoData;
import com.alibaba.nacos.client.naming.remote.gprc.redo.data.InstanceRedoData;
import com.alibaba.nacos.client.security.SecurityProxy; import com.alibaba.nacos.client.security.SecurityProxy;
import com.alibaba.nacos.common.notify.Event; import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.common.notify.NotifyCenter;
@ -49,14 +52,18 @@ import com.alibaba.nacos.common.remote.ConnectionType;
import com.alibaba.nacos.common.remote.client.RpcClient; import com.alibaba.nacos.common.remote.client.RpcClient;
import com.alibaba.nacos.common.remote.client.RpcClientFactory; import com.alibaba.nacos.common.remote.client.RpcClientFactory;
import com.alibaba.nacos.common.remote.client.ServerListFactory; import com.alibaba.nacos.common.remote.client.ServerListFactory;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.JacksonUtils;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;
import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER;
@ -124,6 +131,50 @@ public class NamingGrpcClientProxy extends AbstractNamingClientProxy {
doBatchRegisterService(serviceName, groupName, instances); doBatchRegisterService(serviceName, groupName, instances);
} }
@Override
public void batchDeregisterService(String serviceName, String groupName, List<Instance> instances)
throws NacosException {
List<Instance> retainInstance = getRetainInstance(serviceName, groupName, instances);
batchRegisterService(serviceName, groupName, retainInstance);
}
/**
* Get instance list that need to be deregistered.
* @param serviceName service name
* @param groupName group name
* @param instances instance list
* @return instance list that need to be deregistered.
*/
private List<Instance> getRetainInstance(String serviceName, String groupName, List<Instance> instances) throws NacosException {
if (CollectionUtils.isEmpty(instances)) {
throw new NacosException(NacosException.INVALID_PARAM,
String.format("[Batch deRegistration] need deRegister instance is empty, instances: %s,", instances));
}
String combinedServiceName = NamingUtils.getGroupedName(serviceName, groupName);
InstanceRedoData instanceRedoData = redoService.getRegisteredInstancesBykey(combinedServiceName);
if (!(instanceRedoData instanceof BatchInstanceRedoData)) {
throw new NacosException(NacosException.INVALID_PARAM,
String.format("[Batch deRegistration] batch deRegister is not BatchInstanceRedoData type , instances: %s,", instances));
}
BatchInstanceRedoData batchInstanceRedoData = (BatchInstanceRedoData) instanceRedoData;
List<Instance> allInstance = batchInstanceRedoData.getInstances();
if (CollectionUtils.isEmpty(allInstance)) {
throw new NacosException(NacosException.INVALID_PARAM,
String.format("[Batch deRegistration] not found all registerInstance , serviceName%s , groupName: %s", serviceName, groupName));
}
Map<Instance, Instance> instanceMap = instances
.stream().collect(Collectors.toMap(Function.identity(), Function.identity()));
List<Instance> retainInstances = new ArrayList<>();
for (Instance instance : allInstance) {
if (!instanceMap.containsKey(instance)) {
retainInstances.add(instance);
}
}
return retainInstances;
}
/** /**
* Execute batch register operation. * Execute batch register operation.
* *

View File

@ -277,6 +277,14 @@ public class NamingGrpcRedoService implements ConnectionEventListener {
return result; return result;
} }
/**
* get Cache service.
* @return cache service
*/
public InstanceRedoData getRegisteredInstancesBykey(String combinedServiceName) {
return registeredInstances.get(combinedServiceName);
}
/** /**
* Shutdown redo service. * Shutdown redo service.
*/ */

View File

@ -172,6 +172,12 @@ public class NamingHttpClientProxy extends AbstractNamingClientProxy {
throw new UnsupportedOperationException("Do not support persistent instances to perform batch registration methods."); throw new UnsupportedOperationException("Do not support persistent instances to perform batch registration methods.");
} }
@Override
public void batchDeregisterService(String serviceName, String groupName, List<Instance> instances)
throws NacosException {
throw new UnsupportedOperationException("Do not support persistent instances to perform batch de registration methods.");
}
@Override @Override
public void deregisterService(String serviceName, String groupName, Instance instance) throws NacosException { public void deregisterService(String serviceName, String groupName, Instance instance) throws NacosException {
NAMING_LOGGER NAMING_LOGGER

View File

@ -116,6 +116,27 @@ public class NacosNamingServiceTest {
instances -> CollectionUtils.isEqualCollection(instanceList, instances))); instances -> CollectionUtils.isEqualCollection(instanceList, instances)));
} }
@Test
public void testBatchDeRegisterInstance() throws NacosException {
Instance instance = new Instance();
String serviceName = "service1";
String ip = "1.1.1.1";
int port = 10000;
instance.setServiceName(serviceName);
instance.setEphemeral(true);
instance.setPort(port);
instance.setIp(ip);
List<Instance> instanceList = new ArrayList<>();
instanceList.add(instance);
//when
try {
client.batchDeregisterInstance(serviceName, Constants.DEFAULT_GROUP, instanceList);
} catch (Exception e) {
Assert.assertTrue(e instanceof NacosException);
Assert.assertTrue(e.getMessage().contains("not found"));
}
}
@Test @Test
public void testRegisterInstance2() throws NacosException { public void testRegisterInstance2() throws NacosException {
//given //given

View File

@ -109,6 +109,12 @@ public class AbstractNamingClientProxyTest {
} }
@Override
public void batchDeregisterService(String serviceName, String groupName, List<Instance> instances)
throws NacosException {
}
@Override @Override
public void deregisterService(String serviceName, String groupName, Instance instance) throws NacosException { public void deregisterService(String serviceName, String groupName, Instance instance) throws NacosException {