From d00c133c3e52ec5e2131b207fe76fab6d0d30dc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BF=8A=20SionYang?= <263976490@qq.com> Date: Wed, 15 Jul 2020 20:22:07 +0800 Subject: [PATCH] Add naming support gRPC for register and deregister instance (#3343) --- ...stants.java => NamingRemoteConstants.java} | 15 ++- .../remote/request/InstanceRequest.java | 68 ++++++++++++ .../remote/request/NamingCommonRequest.java | 71 ++++++++++++ .../InstanceResponse.java} | 26 +++-- .../nacos/api/remote/ResponseRegistry.java | 7 +- .../nacos/api/remote/request/Request.java | 2 +- .../config/remote/ConfigGrpcClientProxy.java | 16 ++- .../naming/net/NamingGrpcClientProxy.java | 98 +++++++++++++++++ .../ConfigChangeListenRequestHandler.java | 17 ++- .../remote/ConfigQueryRequestHandler.java | 17 ++- .../nacos/core/remote/RequestHandler.java | 59 +++++----- .../naming/remote/InstanceRequestHandler.java | 104 ++++++++++++++++++ ...va => SubscribeServiceRequestHandler.java} | 12 +- 13 files changed, 450 insertions(+), 62 deletions(-) rename api/src/main/java/com/alibaba/nacos/api/naming/remote/{NamingRequestTypeConstants.java => NamingRemoteConstants.java} (65%) create mode 100644 api/src/main/java/com/alibaba/nacos/api/naming/remote/request/InstanceRequest.java create mode 100644 api/src/main/java/com/alibaba/nacos/api/naming/remote/request/NamingCommonRequest.java rename api/src/main/java/com/alibaba/nacos/api/naming/remote/{NamingCommonRequest.java => response/InstanceResponse.java} (50%) create mode 100644 client/src/main/java/com/alibaba/nacos/client/naming/net/NamingGrpcClientProxy.java create mode 100644 naming/src/main/java/com/alibaba/nacos/naming/remote/InstanceRequestHandler.java rename naming/src/main/java/com/alibaba/nacos/naming/remote/{ServiceInstanceChangeListenRequestHandler.java => SubscribeServiceRequestHandler.java} (76%) diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/remote/NamingRequestTypeConstants.java b/api/src/main/java/com/alibaba/nacos/api/naming/remote/NamingRemoteConstants.java similarity index 65% rename from api/src/main/java/com/alibaba/nacos/api/naming/remote/NamingRequestTypeConstants.java rename to api/src/main/java/com/alibaba/nacos/api/naming/remote/NamingRemoteConstants.java index 85a919f18..e6c5a34b8 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/remote/NamingRequestTypeConstants.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/remote/NamingRemoteConstants.java @@ -16,15 +16,18 @@ 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 - * @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"; } diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/remote/request/InstanceRequest.java b/api/src/main/java/com/alibaba/nacos/api/naming/remote/request/InstanceRequest.java new file mode 100644 index 000000000..aea6d993f --- /dev/null +++ b/api/src/main/java/com/alibaba/nacos/api/naming/remote/request/InstanceRequest.java @@ -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; + } +} diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/remote/request/NamingCommonRequest.java b/api/src/main/java/com/alibaba/nacos/api/naming/remote/request/NamingCommonRequest.java new file mode 100644 index 000000000..5a2158e93 --- /dev/null +++ b/api/src/main/java/com/alibaba/nacos/api/naming/remote/request/NamingCommonRequest.java @@ -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; + } +} diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/remote/NamingCommonRequest.java b/api/src/main/java/com/alibaba/nacos/api/naming/remote/response/InstanceResponse.java similarity index 50% rename from api/src/main/java/com/alibaba/nacos/api/naming/remote/NamingCommonRequest.java rename to api/src/main/java/com/alibaba/nacos/api/naming/remote/response/InstanceResponse.java index 24c6e28a0..4c7b11699 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/remote/NamingCommonRequest.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/remote/response/InstanceResponse.java @@ -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"); * you may not use this file except in compliance with the License. @@ -14,20 +14,26 @@ * 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 - * @version $Id: NamingCommonRequest.java, v 0.1 2020年07月14日 7:26 PM liuzunfei Exp $ + * @author xiweng.yy */ -public abstract class NamingCommonRequest extends Request { +public class InstanceResponse extends Response { - @Override - public String getModule() { - return "naming"; + public InstanceResponse() { + } + + 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); } } diff --git a/api/src/main/java/com/alibaba/nacos/api/remote/ResponseRegistry.java b/api/src/main/java/com/alibaba/nacos/api/remote/ResponseRegistry.java index 47cee1c8b..f82c5a403 100644 --- a/api/src/main/java/com/alibaba/nacos/api/remote/ResponseRegistry.java +++ b/api/src/main/java/com/alibaba/nacos/api/remote/ResponseRegistry.java @@ -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.ConfigQueryResponse; 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.HeartBeatResponse; import com.alibaba.nacos.api.remote.response.ResponseTypeConstants; @@ -51,10 +52,12 @@ public class ResponseRegistry { //naming response registry //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) { return REGISTRY_RESPONSES.get(type); } -} \ No newline at end of file +} diff --git a/api/src/main/java/com/alibaba/nacos/api/remote/request/Request.java b/api/src/main/java/com/alibaba/nacos/api/remote/request/Request.java index 4a94e8ba3..58e319822 100644 --- a/api/src/main/java/com/alibaba/nacos/api/remote/request/Request.java +++ b/api/src/main/java/com/alibaba/nacos/api/remote/request/Request.java @@ -18,8 +18,8 @@ package com.alibaba.nacos.api.remote.request; /** * Request. + * * @author liuzunfei - * @version $Id: Request.java, v 0.1 2020年07月13日 3:46 PM liuzunfei Exp $ */ public abstract class Request { diff --git a/client/src/main/java/com/alibaba/nacos/client/config/remote/ConfigGrpcClientProxy.java b/client/src/main/java/com/alibaba/nacos/client/config/remote/ConfigGrpcClientProxy.java index 419a32d62..06f482cba 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/remote/ConfigGrpcClientProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/remote/ConfigGrpcClientProxy.java @@ -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; diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingGrpcClientProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingGrpcClientProxy.java new file mode 100644 index 000000000..c6b05f019 --- /dev/null +++ b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingGrpcClientProxy.java @@ -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()); + } + } +} diff --git a/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigChangeListenRequestHandler.java b/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigChangeListenRequestHandler.java index d0432b9de..6cfee66ea 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigChangeListenRequestHandler.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigChangeListenRequestHandler.java @@ -1,6 +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.config.server.remote; diff --git a/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigQueryRequestHandler.java b/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigQueryRequestHandler.java index 066ccc1d0..b80e214cc 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigQueryRequestHandler.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigQueryRequestHandler.java @@ -1,6 +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.config.server.remote; diff --git a/core/src/main/java/com/alibaba/nacos/core/remote/RequestHandler.java b/core/src/main/java/com/alibaba/nacos/core/remote/RequestHandler.java index d77900c6b..cc814bc69 100644 --- a/core/src/main/java/com/alibaba/nacos/core/remote/RequestHandler.java +++ b/core/src/main/java/com/alibaba/nacos/core/remote/RequestHandler.java @@ -13,54 +13,57 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + 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.remote.response.Response; 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 org.springframework.beans.factory.annotation.Autowired; +import javax.annotation.PostConstruct; +import java.util.List; + /** + * Nacos based request handler. + * * @author liuzunfei - * @version $Id: RequestHandler.java, v 0.1 2020年07月13日 8:22 PM liuzunfei Exp $ + * @author xiweng.yy */ -public abstract class RequestHandler { - - +public abstract class RequestHandler { + @Autowired private RequestHandlerRegistry requestHandlerRegistry; - + @PostConstruct - public void init(){ + public void init() { requestHandlerRegistry.registryHandler(this); } - + /** + * Parse request body to specified {@link Request}. * - * @param bodyString - * @param - * @return + * @param bodyString body string + * @return request */ - abstract public T parseBodyString(String bodyString); - + public abstract T parseBodyString(String bodyString); + /** - * handler request - * @param request - * @return - * @throws NacosException + * Handler request. + * + * @param request request + * @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 + * Return the request type that this handler can handler. + * + * @return request types this handler responsible. */ - abstract public List getRequestTypes(); - + public abstract List getRequestTypes(); + } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/remote/InstanceRequestHandler.java b/naming/src/main/java/com/alibaba/nacos/naming/remote/InstanceRequestHandler.java new file mode 100644 index 000000000..f58f63706 --- /dev/null +++ b/naming/src/main/java/com/alibaba/nacos/naming/remote/InstanceRequestHandler.java @@ -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 { + + @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 getRequestTypes() { + return Lists.newArrayList(NamingRemoteConstants.REGISTER_INSTANCE, + NamingRemoteConstants.DE_REGISTER_INSTANCE); + } +} diff --git a/naming/src/main/java/com/alibaba/nacos/naming/remote/ServiceInstanceChangeListenRequestHandler.java b/naming/src/main/java/com/alibaba/nacos/naming/remote/SubscribeServiceRequestHandler.java similarity index 76% rename from naming/src/main/java/com/alibaba/nacos/naming/remote/ServiceInstanceChangeListenRequestHandler.java rename to naming/src/main/java/com/alibaba/nacos/naming/remote/SubscribeServiceRequestHandler.java index 5492ff3d1..d84a4cfab 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/remote/ServiceInstanceChangeListenRequestHandler.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/remote/SubscribeServiceRequestHandler.java @@ -17,7 +17,7 @@ package com.alibaba.nacos.naming.remote; 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.RequestMeta; import com.alibaba.nacos.api.remote.response.Response; @@ -30,19 +30,17 @@ import org.springframework.stereotype.Component; import java.util.List; /** - * handler to handle service instance change listen request. + * Handler to handle subscribe service. * * @author liuzunfei - * @version $Id: ServiceInstanceChangeListenRequestHandler.java, v 0.1 2020年07月14日 7:55 PM liuzunfei Exp $ + * @author xiweng.yy */ @Component -public class ServiceInstanceChangeListenRequestHandler extends RequestHandler { +public class SubscribeServiceRequestHandler extends RequestHandler { @Autowired AsyncListenContext asyncListenContext; - private static final String LISTEN_CONTEXT_TYPE = "CONFIG"; - @Override public Request parseBodyString(String bodyString) { return null; @@ -55,6 +53,6 @@ public class ServiceInstanceChangeListenRequestHandler extends RequestHandler { @Override public List getRequestTypes() { - return Lists.newArrayList(NamingRequestTypeConstants.SERVICE_INSTANCE_CHANGE); + return Lists.newArrayList(NamingRemoteConstants.SUBSCRIBE_SERVICE); } }