Skip to content

Commit

Permalink
[ISSUE#5863]add nacos client batchRegitserInstance request (alibaba#8675
Browse files Browse the repository at this point in the history
)

* add nacos client batchRegitserInstance request

* adjustment code

* fix word and do not remove indent
  • Loading branch information
chenhao26-nineteen authored Jul 7, 2022
1 parent 5aabba1 commit 073f9e0
Show file tree
Hide file tree
Showing 14 changed files with 219 additions and 5 deletions.
11 changes: 11 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ void registerInstance(String serviceName, String groupName, String ip, int port,
*/
void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException;

/**
* batch register instance to service with specified instance properties.
*
* @param serviceName name of service
* @param groupName group of service
* @param instances instances to register
* @throws NacosException nacos exception
* @since 2.1.1
*/
void batchRegisterInstance(String serviceName, String groupName, List<Instance> instances) throws NacosException;

/**
* deregister instance from a service.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.utils.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import static com.alibaba.nacos.api.common.Constants.CLUSTER_NAME_PATTERN_STRING;
Expand Down Expand Up @@ -145,6 +148,31 @@ public static void checkInstanceIsLegal(Instance instance) throws NacosException
}
}

/**
* check batch register is Ephemeral.
* @param instance instance
* @throws NacosException NacosException
*/
public static void checkInstanceIsEphemeral(Instance instance) throws NacosException {
if (!instance.isEphemeral()) {
throw new NacosException(NacosException.INVALID_PARAM,
String.format("Batch registration does not allow persistent instance registration , Instance:%s", instance));
}
}

/**
* Batch verify the validity of instances.
* @param instances List of instances to be registered
* @throws NacosException Nacos
*/
public static void batchCheckInstanceIsLegal(List<Instance> instances) throws NacosException {
Set<Instance> newInstanceSet = new HashSet<>(instances);
for (Instance instance : newInstanceSet) {
checkInstanceIsEphemeral(instance);
checkInstanceIsLegal(instance);
}
}

/**
* Check string is a number or not.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ public void registerInstance(String serviceName, String groupName, Instance inst
clientProxy.registerService(serviceName, groupName, instance);
}

@Override
public void batchRegisterInstance(String serviceName, String groupName, List<Instance> instances)
throws NacosException {
NamingUtils.batchCheckInstanceIsLegal(instances);
clientProxy.batchRegisterService(serviceName, groupName, instances);
}

@Override
public void deregisterInstance(String serviceName, String ip, int port) throws NacosException {
deregisterInstance(serviceName, ip, port, Constants.DEFAULT_CLUSTER_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.alibaba.nacos.api.selector.AbstractSelector;
import com.alibaba.nacos.common.lifecycle.Closeable;

import java.util.List;
import java.util.Set;

/**
Expand All @@ -43,6 +44,17 @@ public interface NamingClientProxy extends Closeable {
*/
void registerService(String serviceName, String groupName, Instance instance) throws NacosException;

/**
* Batch register instance to service with specified instance properties.
*
* @param serviceName service name
* @param groupName group name
* @param instances instance
* @throws NacosException nacos exception
* @since 2.1.1
*/
void batchRegisterService(String serviceName, String groupName, List<Instance> instances) throws NacosException;

/**
* Deregister instance from a service.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientManager;
import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy;
import com.alibaba.nacos.client.security.SecurityProxy;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.ThreadUtils;

import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -94,6 +96,17 @@ public void registerService(String serviceName, String groupName, Instance insta
getExecuteClientProxy(instance).registerService(serviceName, groupName, instance);
}

@Override
public void batchRegisterService(String serviceName, String groupName, List<Instance> instances)
throws NacosException {
NAMING_LOGGER.info("batchRegisterInstance instances: {} ,serviceName: {} begin.", instances, serviceName);
if (CollectionUtils.isEmpty(instances)) {
NAMING_LOGGER.warn("batchRegisterInstance instances is Empty:{}", instances);
}
grpcClientProxy.batchRegisterService(serviceName, groupName, instances);
NAMING_LOGGER.info("batchRegisterInstance instances: {} ,serviceName: {} finish.", instances, serviceName);
}

@Override
public void deregisterService(String serviceName, String groupName, Instance instance) throws NacosException {
getExecuteClientProxy(instance).deregisterService(serviceName, groupName, instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.api.naming.remote.NamingRemoteConstants;
import com.alibaba.nacos.api.naming.remote.request.AbstractNamingRequest;
import com.alibaba.nacos.api.naming.remote.request.BatchInstanceRequest;
import com.alibaba.nacos.api.naming.remote.request.InstanceRequest;
import com.alibaba.nacos.api.naming.remote.request.ServiceListRequest;
import com.alibaba.nacos.api.naming.remote.request.ServiceQueryRequest;
import com.alibaba.nacos.api.naming.remote.request.SubscribeServiceRequest;
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.ServiceListResponse;
import com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse;
Expand All @@ -50,6 +52,7 @@
import com.alibaba.nacos.common.utils.JacksonUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
Expand Down Expand Up @@ -114,6 +117,29 @@ public void registerService(String serviceName, String groupName, Instance insta
doRegisterService(serviceName, groupName, instance);
}

@Override
public void batchRegisterService(String serviceName, String groupName, List<Instance> instances)
throws NacosException {
redoService.cacheInstanceForRedo(serviceName, groupName, instances);
doBatchRegisterService(serviceName, groupName, instances);
}

/**
* Execute batch register operation.
*
* @param serviceName service name
* @param groupName group name
* @param instances instances
* @throws NacosException NacosException
*/
public void doBatchRegisterService(String serviceName, String groupName, List<Instance> instances)
throws NacosException {
BatchInstanceRequest request = new BatchInstanceRequest(namespaceId, serviceName, groupName,
NamingRemoteConstants.BATCH_REGISTER_INSTANCE, instances);
requestToServer(request, BatchInstanceResponse.class);
redoService.instanceRegistered(serviceName, groupName);
}

/**
* Execute register operation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy;
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.naming.remote.gprc.redo.data.SubscriberRedoData;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.common.executor.NameThreadFactory;
import com.alibaba.nacos.common.remote.client.ConnectionEventListener;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand Down Expand Up @@ -104,6 +106,21 @@ public void cacheInstanceForRedo(String serviceName, String groupName, Instance
}
}

/**
* Cache registered instance for redo.
*
* @param serviceName service name
* @param groupName group name
* @param instances batch registered instance
*/
public void cacheInstanceForRedo(String serviceName, String groupName, List<Instance> instances) {
String key = NamingUtils.getGroupedName(serviceName, groupName);
BatchInstanceRedoData redoData = BatchInstanceRedoData.build(serviceName, groupName, instances);
synchronized (registeredInstances) {
registeredInstances.put(key, redoData);
}
}

/**
* Instance register successfully, mark registered status as {@code true}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy;
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.naming.remote.gprc.redo.data.RedoData;
import com.alibaba.nacos.client.naming.remote.gprc.redo.data.SubscriberRedoData;
Expand Down Expand Up @@ -75,7 +76,7 @@ private void redoForInstance(InstanceRedoData redoData) throws NacosException {
if (isClientDisabled()) {
return;
}
clientProxy.doRegisterService(serviceName, groupName, redoData.get());
processRegisterRedoType(redoData, serviceName, groupName);
break;
case UNREGISTER:
if (isClientDisabled()) {
Expand All @@ -91,6 +92,16 @@ private void redoForInstance(InstanceRedoData redoData) throws NacosException {

}

private void processRegisterRedoType(InstanceRedoData redoData, String serviceName, String groupName) throws NacosException {
if (redoData instanceof BatchInstanceRedoData) {
// Execute Batch Register
BatchInstanceRedoData batchInstanceRedoData = (BatchInstanceRedoData) redoData;
clientProxy.doBatchRegisterService(serviceName, groupName, batchInstanceRedoData.getInstances());
return;
}
clientProxy.doRegisterService(serviceName, groupName, redoData.get());
}

private void redoForSubscribes() {
for (SubscriberRedoData each : redoService.findSubscriberRedoData()) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.client.naming.remote.gprc.redo.data;

import com.alibaba.nacos.api.naming.pojo.Instance;

import java.util.List;

/**
* batch instance redo service.
*
* @author <a href="mailto:[email protected]">chenhao26</a>
*/
public class BatchInstanceRedoData extends InstanceRedoData {

List<Instance> instances;

public List<Instance> getInstances() {
return instances;
}

public void setInstances(List<Instance> instances) {
this.instances = instances;
}

protected BatchInstanceRedoData(String serviceName, String groupName) {
super(serviceName, groupName);
}

/**
* build BatchInstanceRedoData.
*
* @param serviceName service name
* @param groupName group name
* @param instances instances
* @return build BatchInstanceRedoData
*/
public static BatchInstanceRedoData build(String serviceName, String groupName, List<Instance> instances) {
BatchInstanceRedoData result = new BatchInstanceRedoData(serviceName, groupName);
result.setInstances(instances);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
public class InstanceRedoData extends RedoData<Instance> {

private InstanceRedoData(String serviceName, String groupName) {
protected InstanceRedoData(String serviceName, String groupName) {
super(serviceName, groupName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ public void registerService(String serviceName, String groupName, Instance insta

}

@Override
public void batchRegisterService(String serviceName, String groupName, List<Instance> instances) {
throw new UnsupportedOperationException("Do not support persistent instances to perform batch registration methods.");
}

@Override
public void deregisterService(String serviceName, String groupName, Instance instance) throws NacosException {
NAMING_LOGGER
Expand Down Expand Up @@ -482,12 +487,11 @@ public String callServer(String api, Map<String, String> params, Map<String, Str
}
url = NamingHttpClientManager.getInstance().getPrefix() + curServer + api;
}

try {
HttpRestResult<String> restResult = nacosRestTemplate
.exchangeForm(url, header, Query.newInstance().initParams(params), body, method, String.class);
end = System.currentTimeMillis();

MetricsMonitor.getNamingRequestMonitor(method, url, String.valueOf(restResult.getCode()))
.observe(end - start);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.mockito.junit.MockitoJUnitRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -94,7 +95,13 @@ protected MockNamingClientProxy(SecurityProxy securityProxy) {
public void registerService(String serviceName, String groupName, Instance instance) throws NacosException {

}

@Override
public void batchRegisterService(String serviceName, String groupName, List<Instance> instances)
throws NacosException {

}

@Override
public void deregisterService(String serviceName, String groupName, Instance instance) throws NacosException {

Expand Down
Loading

0 comments on commit 073f9e0

Please sign in to comment.