Add naming support gRPC for register and deregister instance (#3343)
This commit is contained in:
parent
139b211e21
commit
d00c133c3e
@ -16,15 +16,18 @@
|
|||||||
|
|
||||||
package com.alibaba.nacos.api.naming.remote;
|
package com.alibaba.nacos.api.naming.remote;
|
||||||
|
|
||||||
import com.alibaba.nacos.api.remote.request.RequestTypeConstants;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* retain all naming module request type constants.
|
* Retain all naming module request type constants.
|
||||||
*
|
*
|
||||||
* @author liuzunfei
|
* @author liuzunfei
|
||||||
* @version $Id: NamingRequestTypeConstants.java, v 0.1 2020年07月13日 9:12 PM liuzunfei Exp $
|
* @author xiweng.yy
|
||||||
*/
|
*/
|
||||||
public class NamingRequestTypeConstants extends RequestTypeConstants {
|
public class NamingRemoteConstants {
|
||||||
|
|
||||||
|
public static final String REGISTER_INSTANCE = "registerInstance";
|
||||||
|
|
||||||
|
public static final String DE_REGISTER_INSTANCE = "deregisterInstance";
|
||||||
|
|
||||||
|
public static final String SUBSCRIBE_SERVICE = "subscribeService";
|
||||||
|
|
||||||
public static final String SERVICE_INSTANCE_CHANGE = "SERVICE_INSTANCE_CHANGE";
|
|
||||||
}
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* 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 com.alibaba.nacos.api.naming.utils.NamingUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Nacos instances request.
|
||||||
|
*
|
||||||
|
* @author xiweng.yy
|
||||||
|
*/
|
||||||
|
public class InstanceRequest extends NamingCommonRequest {
|
||||||
|
|
||||||
|
private String type;
|
||||||
|
|
||||||
|
private Instance instance;
|
||||||
|
|
||||||
|
public InstanceRequest() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public InstanceRequest(String namespace, String type, Instance instance) {
|
||||||
|
this(namespace, instance.getServiceName(), NamingUtils.getGroupName(instance.getServiceName()), type, instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
public InstanceRequest(String namespace, String serviceName, String type, Instance instance) {
|
||||||
|
super(namespace, serviceName, null);
|
||||||
|
this.type = type;
|
||||||
|
this.instance = instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InstanceRequest(String namespace, String serviceName, String groupName, String type, Instance instance) {
|
||||||
|
super(namespace, serviceName, groupName);
|
||||||
|
this.type = type;
|
||||||
|
this.instance = instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getType() {
|
||||||
|
return this.type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInstance(Instance instance) {
|
||||||
|
this.instance = instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Instance getInstance() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* 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.remote.request.Request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uniform remote request of naming module.
|
||||||
|
*
|
||||||
|
* @author liuzunfei
|
||||||
|
*/
|
||||||
|
public abstract class NamingCommonRequest extends Request {
|
||||||
|
|
||||||
|
private String namespace;
|
||||||
|
|
||||||
|
private String serviceName;
|
||||||
|
|
||||||
|
private String groupName;
|
||||||
|
|
||||||
|
public NamingCommonRequest() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public NamingCommonRequest(String namespace, String serviceName, String groupName) {
|
||||||
|
this.namespace = namespace;
|
||||||
|
this.serviceName = serviceName;
|
||||||
|
this.groupName = groupName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getModule() {
|
||||||
|
return "naming";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNamespace() {
|
||||||
|
return namespace;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNamespace(String namespace) {
|
||||||
|
this.namespace = namespace;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getServiceName() {
|
||||||
|
return serviceName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setServiceName(String serviceName) {
|
||||||
|
this.serviceName = serviceName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGroupName() {
|
||||||
|
return groupName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGroupName(String groupName) {
|
||||||
|
this.groupName = groupName;
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 1999-2020 Alibaba Group Holding Ltd.
|
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -14,20 +14,26 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.alibaba.nacos.api.naming.remote;
|
package com.alibaba.nacos.api.naming.remote.response;
|
||||||
|
|
||||||
import com.alibaba.nacos.api.remote.request.Request;
|
import com.alibaba.nacos.api.remote.response.Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* uniform remote request of naming module
|
* Instance response.
|
||||||
*
|
*
|
||||||
* @author liuzunfei
|
* @author xiweng.yy
|
||||||
* @version $Id: NamingCommonRequest.java, v 0.1 2020年07月14日 7:26 PM liuzunfei Exp $
|
|
||||||
*/
|
*/
|
||||||
public abstract class NamingCommonRequest extends Request {
|
public class InstanceResponse extends Response {
|
||||||
|
|
||||||
@Override
|
public InstanceResponse() {
|
||||||
public String getModule() {
|
}
|
||||||
return "naming";
|
|
||||||
|
public InstanceResponse(int resultCode, String message, String type) {
|
||||||
|
this(resultCode, 0, message, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public InstanceResponse(int resultCode, int errorCode, String message, String type) {
|
||||||
|
super(type, resultCode, message);
|
||||||
|
setErrorCode(errorCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -20,7 +20,8 @@ import com.alibaba.nacos.api.config.remote.response.ConfigChangeListenResponse;
|
|||||||
import com.alibaba.nacos.api.config.remote.response.ConfigChangeNotifyResponse;
|
import com.alibaba.nacos.api.config.remote.response.ConfigChangeNotifyResponse;
|
||||||
import com.alibaba.nacos.api.config.remote.response.ConfigQueryResponse;
|
import com.alibaba.nacos.api.config.remote.response.ConfigQueryResponse;
|
||||||
import com.alibaba.nacos.api.config.remote.response.ConfigResponseTypeConstants;
|
import com.alibaba.nacos.api.config.remote.response.ConfigResponseTypeConstants;
|
||||||
import com.alibaba.nacos.api.naming.remote.NamingRequestTypeConstants;
|
import com.alibaba.nacos.api.naming.remote.NamingRemoteConstants;
|
||||||
|
import com.alibaba.nacos.api.naming.remote.response.InstanceResponse;
|
||||||
import com.alibaba.nacos.api.remote.response.ConnectResetResponse;
|
import com.alibaba.nacos.api.remote.response.ConnectResetResponse;
|
||||||
import com.alibaba.nacos.api.remote.response.HeartBeatResponse;
|
import com.alibaba.nacos.api.remote.response.HeartBeatResponse;
|
||||||
import com.alibaba.nacos.api.remote.response.ResponseTypeConstants;
|
import com.alibaba.nacos.api.remote.response.ResponseTypeConstants;
|
||||||
@ -51,10 +52,12 @@ public class ResponseRegistry {
|
|||||||
|
|
||||||
//naming response registry
|
//naming response registry
|
||||||
//REGISTRY_RESPONSES.put(NamingRequestTypeConstants.SERVICE_INSTANCE_CHANGE, ServiceI.class);
|
//REGISTRY_RESPONSES.put(NamingRequestTypeConstants.SERVICE_INSTANCE_CHANGE, ServiceI.class);
|
||||||
|
REGISTRY_RESPONSES.put(NamingRemoteConstants.REGISTER_INSTANCE, InstanceResponse.class);
|
||||||
|
REGISTRY_RESPONSES.put(NamingRemoteConstants.DE_REGISTER_INSTANCE, InstanceResponse.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Class getClassByType(String type) {
|
public static Class getClassByType(String type) {
|
||||||
return REGISTRY_RESPONSES.get(type);
|
return REGISTRY_RESPONSES.get(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,8 @@ package com.alibaba.nacos.api.remote.request;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Request.
|
* Request.
|
||||||
|
*
|
||||||
* @author liuzunfei
|
* @author liuzunfei
|
||||||
* @version $Id: Request.java, v 0.1 2020年07月13日 3:46 PM liuzunfei Exp $
|
|
||||||
*/
|
*/
|
||||||
public abstract class Request {
|
public abstract class Request {
|
||||||
|
|
||||||
|
@ -1,5 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* Alipay.com Inc. Copyright (c) 2004-2020 All Rights Reserved.
|
* 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.client.config.remote;
|
package com.alibaba.nacos.client.config.remote;
|
||||||
|
@ -0,0 +1,98 @@
|
|||||||
|
/*
|
||||||
|
* 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.client.naming.net;
|
||||||
|
|
||||||
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
|
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||||
|
import com.alibaba.nacos.api.naming.remote.NamingRemoteConstants;
|
||||||
|
import com.alibaba.nacos.api.naming.remote.request.InstanceRequest;
|
||||||
|
import com.alibaba.nacos.api.remote.response.Response;
|
||||||
|
import com.alibaba.nacos.client.remote.RpcClient;
|
||||||
|
import com.alibaba.nacos.client.remote.RpcClientFactory;
|
||||||
|
import com.alibaba.nacos.client.remote.ServerListFactory;
|
||||||
|
|
||||||
|
import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Naming grpc client proxy.
|
||||||
|
*
|
||||||
|
* @author xiweng.yy
|
||||||
|
*/
|
||||||
|
public class NamingGrpcClientProxy {
|
||||||
|
|
||||||
|
private final String namespaceId;
|
||||||
|
|
||||||
|
private RpcClient rpcClient;
|
||||||
|
|
||||||
|
public NamingGrpcClientProxy(String namespaceId) {
|
||||||
|
this.namespaceId = namespaceId;
|
||||||
|
rpcClient = RpcClientFactory.getClient("naming");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() throws NacosException {
|
||||||
|
rpcClient.init(new ServerListFactory() {
|
||||||
|
@Override
|
||||||
|
public String genNextServer() {
|
||||||
|
return "localhost:8848";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getCurrentServer() {
|
||||||
|
return "localhost:8848";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
rpcClient.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* register a instance to service with specified instance properties.
|
||||||
|
*
|
||||||
|
* @param serviceName name of service
|
||||||
|
* @param groupName group of service
|
||||||
|
* @param instance instance to register
|
||||||
|
* @throws NacosException nacos exception
|
||||||
|
*/
|
||||||
|
public void registerService(String serviceName, String groupName, Instance instance) throws NacosException {
|
||||||
|
NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance {}", namespaceId, serviceName,
|
||||||
|
instance);
|
||||||
|
InstanceRequest request = new InstanceRequest(namespaceId, serviceName, groupName,
|
||||||
|
NamingRemoteConstants.REGISTER_INSTANCE, instance);
|
||||||
|
Response response = rpcClient.request(request);
|
||||||
|
if (200 != response.getResultCode()) {
|
||||||
|
throw new NacosException(response.getErrorCode(), response.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* deregister instance from a service.
|
||||||
|
*
|
||||||
|
* @param serviceName name of service
|
||||||
|
* @param instance instance
|
||||||
|
* @throws NacosException nacos exception
|
||||||
|
*/
|
||||||
|
public void deregisterService(String serviceName, Instance instance) throws NacosException {
|
||||||
|
NAMING_LOGGER
|
||||||
|
.info("[DEREGISTER-SERVICE] {} deregistering service {} with instance: {}", namespaceId, serviceName,
|
||||||
|
instance);
|
||||||
|
InstanceRequest request = new InstanceRequest(namespaceId, serviceName,
|
||||||
|
NamingRemoteConstants.DE_REGISTER_INSTANCE, instance);
|
||||||
|
Response response = rpcClient.request(request);
|
||||||
|
if (200 != response.getResultCode()) {
|
||||||
|
throw new NacosException(response.getErrorCode(), response.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* Alipay.com Inc.
|
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||||
* Copyright (c) 2004-2020 All Rights Reserved.
|
*
|
||||||
|
* 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.config.server.remote;
|
package com.alibaba.nacos.config.server.remote;
|
||||||
|
@ -1,6 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* Alipay.com Inc.
|
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||||
* Copyright (c) 2004-2020 All Rights Reserved.
|
*
|
||||||
|
* 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.config.server.remote;
|
package com.alibaba.nacos.config.server.remote;
|
||||||
|
|
||||||
|
@ -13,54 +13,57 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.alibaba.nacos.core.remote;
|
package com.alibaba.nacos.core.remote;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
|
||||||
|
|
||||||
import com.alibaba.nacos.api.exception.NacosException;
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
import com.alibaba.nacos.api.remote.response.Response;
|
|
||||||
import com.alibaba.nacos.api.remote.request.Request;
|
import com.alibaba.nacos.api.remote.request.Request;
|
||||||
import com.alibaba.nacos.api.remote.request.RequestMeta;
|
import com.alibaba.nacos.api.remote.request.RequestMeta;
|
||||||
|
import com.alibaba.nacos.api.remote.response.Response;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Nacos based request handler.
|
||||||
|
*
|
||||||
* @author liuzunfei
|
* @author liuzunfei
|
||||||
* @version $Id: RequestHandler.java, v 0.1 2020年07月13日 8:22 PM liuzunfei Exp $
|
* @author xiweng.yy
|
||||||
*/
|
*/
|
||||||
public abstract class RequestHandler<T> {
|
public abstract class RequestHandler<T extends Request> {
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RequestHandlerRegistry requestHandlerRegistry;
|
private RequestHandlerRegistry requestHandlerRegistry;
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init(){
|
public void init() {
|
||||||
requestHandlerRegistry.registryHandler(this);
|
requestHandlerRegistry.registryHandler(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Parse request body to specified {@link Request}.
|
||||||
*
|
*
|
||||||
* @param bodyString
|
* @param bodyString body string
|
||||||
* @param <T>
|
* @return request
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
abstract public <T extends Request> T parseBodyString(String bodyString);
|
public abstract T parseBodyString(String bodyString);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* handler request
|
* Handler request.
|
||||||
* @param request
|
*
|
||||||
* @return
|
* @param request request
|
||||||
* @throws NacosException
|
* @param meta request meta data
|
||||||
|
* @return response
|
||||||
|
* @throws NacosException nacos exception when handle request has problem.
|
||||||
*/
|
*/
|
||||||
abstract public Response handle(Request request, RequestMeta meta) throws NacosException;
|
public abstract Response handle(Request request, RequestMeta meta) throws NacosException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* retrun the request type that this handler can handler
|
* Return the request type that this handler can handler.
|
||||||
* @return
|
*
|
||||||
|
* @return request types this handler responsible.
|
||||||
*/
|
*/
|
||||||
abstract public List<String> getRequestTypes();
|
public abstract List<String> getRequestTypes();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
|
import com.alibaba.nacos.api.naming.remote.request.InstanceRequest;
|
||||||
|
import com.alibaba.nacos.api.naming.remote.NamingRemoteConstants;
|
||||||
|
import com.alibaba.nacos.api.naming.remote.response.InstanceResponse;
|
||||||
|
import com.alibaba.nacos.api.remote.request.Request;
|
||||||
|
import com.alibaba.nacos.api.remote.request.RequestMeta;
|
||||||
|
import com.alibaba.nacos.api.remote.response.Response;
|
||||||
|
import com.alibaba.nacos.common.utils.JacksonUtils;
|
||||||
|
import com.alibaba.nacos.common.utils.StringUtils;
|
||||||
|
import com.alibaba.nacos.core.remote.RequestHandler;
|
||||||
|
import com.alibaba.nacos.naming.core.Instance;
|
||||||
|
import com.alibaba.nacos.naming.core.ServiceManager;
|
||||||
|
import com.alibaba.nacos.naming.misc.Loggers;
|
||||||
|
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instance request handler.
|
||||||
|
*
|
||||||
|
* @author xiweng.yy
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class InstanceRequestHandler extends RequestHandler<InstanceRequest> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ServiceManager serviceManager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InstanceRequest parseBodyString(String bodyString) {
|
||||||
|
return JacksonUtils.toObj(bodyString, InstanceRequest.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Response handle(Request request, RequestMeta meta) throws NacosException {
|
||||||
|
InstanceRequest instanceRequest = (InstanceRequest) request;
|
||||||
|
String namespace = instanceRequest.getNamespace();
|
||||||
|
String serviceName = instanceRequest.getServiceName();
|
||||||
|
switch (instanceRequest.getType()) {
|
||||||
|
case NamingRemoteConstants.REGISTER_INSTANCE:
|
||||||
|
if (!serviceManager.containService(namespace, serviceName)) {
|
||||||
|
serviceManager.createEmptyService(namespace, serviceName, false);
|
||||||
|
}
|
||||||
|
Instance instance = parseInstance(instanceRequest.getInstance());
|
||||||
|
instance.setServiceName(serviceName);
|
||||||
|
instance.setInstanceId(instance.generateInstanceId());
|
||||||
|
instance.setLastBeat(System.currentTimeMillis());
|
||||||
|
// Register instance by connection, do not need keep alive by beat.
|
||||||
|
instance.setMarked(true);
|
||||||
|
instance.validate();
|
||||||
|
serviceManager.addInstance(namespace, serviceName, instance.isEphemeral(), instance);
|
||||||
|
break;
|
||||||
|
case NamingRemoteConstants.DE_REGISTER_INSTANCE:
|
||||||
|
if (!serviceManager.containService(namespace, serviceName)) {
|
||||||
|
Loggers.SRV_LOG.warn("remove instance from non-exist service: {}", serviceName);
|
||||||
|
return new InstanceResponse(200, "success", request.getType());
|
||||||
|
}
|
||||||
|
instance = parseInstance(instanceRequest.getInstance());
|
||||||
|
serviceManager.removeInstance(namespace, serviceName, instance.isEphemeral(), instance);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new NacosException(NacosException.INVALID_PARAM, String.format("Unsupported request type %s", instanceRequest.getType()));
|
||||||
|
}
|
||||||
|
return new InstanceResponse(200, "success", request.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Instance parseInstance(com.alibaba.nacos.api.naming.pojo.Instance instance) {
|
||||||
|
Instance result = new Instance(instance.getIp(), instance.getPort());
|
||||||
|
result.setClusterName(StringUtils.isBlank(instance.getClusterName()) ? UtilsAndCommons.DEFAULT_CLUSTER_NAME
|
||||||
|
: instance.getClusterName());
|
||||||
|
result.setEnabled(instance.isEnabled());
|
||||||
|
result.setEphemeral(instance.isEphemeral());
|
||||||
|
result.setWeight(instance.getWeight());
|
||||||
|
result.setMetadata(instance.getMetadata());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getRequestTypes() {
|
||||||
|
return Lists.newArrayList(NamingRemoteConstants.REGISTER_INSTANCE,
|
||||||
|
NamingRemoteConstants.DE_REGISTER_INSTANCE);
|
||||||
|
}
|
||||||
|
}
|
@ -17,7 +17,7 @@
|
|||||||
package com.alibaba.nacos.naming.remote;
|
package com.alibaba.nacos.naming.remote;
|
||||||
|
|
||||||
import com.alibaba.nacos.api.exception.NacosException;
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
import com.alibaba.nacos.api.naming.remote.NamingRequestTypeConstants;
|
import com.alibaba.nacos.api.naming.remote.NamingRemoteConstants;
|
||||||
import com.alibaba.nacos.api.remote.request.Request;
|
import com.alibaba.nacos.api.remote.request.Request;
|
||||||
import com.alibaba.nacos.api.remote.request.RequestMeta;
|
import com.alibaba.nacos.api.remote.request.RequestMeta;
|
||||||
import com.alibaba.nacos.api.remote.response.Response;
|
import com.alibaba.nacos.api.remote.response.Response;
|
||||||
@ -30,19 +30,17 @@ import org.springframework.stereotype.Component;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* handler to handle service instance change listen request.
|
* Handler to handle subscribe service.
|
||||||
*
|
*
|
||||||
* @author liuzunfei
|
* @author liuzunfei
|
||||||
* @version $Id: ServiceInstanceChangeListenRequestHandler.java, v 0.1 2020年07月14日 7:55 PM liuzunfei Exp $
|
* @author xiweng.yy
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class ServiceInstanceChangeListenRequestHandler extends RequestHandler {
|
public class SubscribeServiceRequestHandler extends RequestHandler {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
AsyncListenContext asyncListenContext;
|
AsyncListenContext asyncListenContext;
|
||||||
|
|
||||||
private static final String LISTEN_CONTEXT_TYPE = "CONFIG";
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Request parseBodyString(String bodyString) {
|
public Request parseBodyString(String bodyString) {
|
||||||
return null;
|
return null;
|
||||||
@ -55,6 +53,6 @@ public class ServiceInstanceChangeListenRequestHandler extends RequestHandler {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getRequestTypes() {
|
public List<String> getRequestTypes() {
|
||||||
return Lists.newArrayList(NamingRequestTypeConstants.SERVICE_INSTANCE_CHANGE);
|
return Lists.newArrayList(NamingRemoteConstants.SUBSCRIBE_SERVICE);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user