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

[ISSUE#5863] Define InstanceRequest InstanceResponse and Handler #8489

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 @@ -26,6 +26,8 @@ public class NamingRemoteConstants {

public static final String REGISTER_INSTANCE = "registerInstance";

public static final String BATCH_REGISTER_INSTANCE = "batchRegisterInstance";

public static final String DE_REGISTER_INSTANCE = "deregisterInstance";

public static final String QUERY_SERVICE = "queryService";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 1999-2018 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.api.naming.remote.request;

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

import java.util.List;

/**
* The client registers multiple service instance request.
*
* @author <a href="mailto:[email protected]">chenhao26</a>
*/
public class BatchInstanceRequest extends AbstractNamingRequest {

private String type;

/**
* save all service instance.
*/
private List<Instance> instances;

public BatchInstanceRequest() {
}

public BatchInstanceRequest(String namespace, String serviceName, String groupName, String type,
List<Instance> instances) {
super(namespace, serviceName, groupName);
this.type = type;
this.instances = instances;
}

public void setType(String type) {
this.type = type;
}

public String getType() {
return this.type;
}

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

public void setInstances(List<Instance> instances) {
this.instances = instances;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 1999-2018 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.api.naming.remote.response;

import com.alibaba.nacos.api.remote.response.Response;

/**
* batch instance response.
*
* @author <a href="mailto:[email protected]">chenhao26</a>
*/
public class BatchInstanceResponse extends Response {

private String type;

public BatchInstanceResponse() {
}

public BatchInstanceResponse(String type) {
this.type = type;
}

public void setType(String type) {
this.type = type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.alibaba.nacos.naming.pojo.Subscriber;
import com.alibaba.nacos.naming.constants.Constants;

import java.util.List;
import java.util.Map;

/**
Expand All @@ -42,6 +43,15 @@ public interface ClientOperationService {
*/
void registerInstance(Service service, Instance instance, String clientId);

/**
* Batch register instance to service.
*
* @param service service
* @param instances instances
* @param clientId id of client
*/
void batchRegisterInstance(Service service, List<Instance> instances, String clientId);

/**
* Deregister instance from service.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.alibaba.nacos.naming.pojo.Subscriber;
import org.springframework.stereotype.Component;

import java.util.List;

/**
* Implementation of external exposure.
*
Expand All @@ -50,6 +52,12 @@ public void registerInstance(Service service, Instance instance, String clientId
operationService.registerInstance(service, instance, clientId);
}

@Override
public void batchRegisterInstance(Service service, List<Instance> instances, String clientId) {
final ClientOperationService operationService = chooseClientOperationService(instances.get(0));
operationService.batchRegisterInstance(service, instances, clientId);
}

@Override
public void deregisterInstance(Service service, Instance instance, String clientId) {
if (!ServiceManager.getInstance().containSingleton(service)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import com.alibaba.nacos.naming.pojo.Subscriber;
import org.springframework.stereotype.Component;

import java.util.List;

/**
* Operation service for ephemeral clients and services.
*
Expand Down Expand Up @@ -67,6 +69,11 @@ public void registerInstance(Service service, Instance instance, String clientId
.publishEvent(new MetadataEvent.InstanceMetadataEvent(singleton, instanceInfo.getMetadataId(), false));
}

@Override
public void batchRegisterInstance(Service service, List<Instance> instances, String clientId) {
//TODO EphemeralClientOperationServiceImpl batchRegisterInstance
}

@Override
public void deregisterInstance(Service service, Instance instance, String clientId) {
if (!ServiceManager.getInstance().containSingleton(service)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public void registerInstance(Service service, Instance instance, String clientId
}
}

@Override
public void batchRegisterInstance(Service service, List<Instance> instances, String clientId) {
//TODO PersistentClientOperationServiceImpl Nacos batchRegister
}

@Override
public void deregisterInstance(Service service, Instance instance, String clientId) {
final InstanceStoreRequest request = new InstanceStoreRequest();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 1999-2018 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.naming.remote.rpc.handler;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.remote.NamingRemoteConstants;
import com.alibaba.nacos.api.naming.remote.request.BatchInstanceRequest;
import com.alibaba.nacos.api.naming.remote.response.BatchInstanceResponse;
import com.alibaba.nacos.api.remote.request.RequestMeta;
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 org.springframework.stereotype.Component;

/**
* The client registers multiple service instance request.
*
* @author <a href="mailto:[email protected]">chenhao26</a>
*/
@Component("batchInstanceRequestHandler")
public class BatchInstanceRequestHandler extends RequestHandler<BatchInstanceRequest, BatchInstanceResponse> {

private final EphemeralClientOperationServiceImpl clientOperationService;

public BatchInstanceRequestHandler(EphemeralClientOperationServiceImpl clientOperationService) {
this.clientOperationService = clientOperationService;
}

@Override
public BatchInstanceResponse handle(BatchInstanceRequest request, RequestMeta meta) throws NacosException {
Service service = Service.newService(request.getNamespace(), request.getGroupName(), request.getServiceName(),
true);
switch (request.getType()) {
case NamingRemoteConstants.BATCH_REGISTER_INSTANCE:
return batchRegisterInstance(service, request, meta);
default:
throw new NacosException(NacosException.INVALID_PARAM,
String.format("Unsupported request type %s", request.getType()));
}
}

private BatchInstanceResponse batchRegisterInstance(Service service, BatchInstanceRequest request,
RequestMeta meta) {
clientOperationService.batchRegisterInstance(service, request.getInstances(), meta.getConnectionId());
return new BatchInstanceResponse(NamingRemoteConstants.BATCH_REGISTER_INSTANCE);
}
}