From 39986a867a5a19bb8f4d047b800bf97a778efe90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BF=8A=20SionYang?= <263976490@qq.com> Date: Mon, 20 Jul 2020 16:32:07 +0800 Subject: [PATCH] [ISSUE #3386] Extract `NamingClientProxy` Interface (#3387) * Move and rename NamingProxy.java * rename package naming.net --> naming.remote * Create Naming client proxy interface --- .../naming/NacosNamingMaintainService.java | 6 +- .../client/naming/NacosNamingService.java | 15 +- .../nacos/client/naming/beat/BeatReactor.java | 8 +- .../nacos/client/naming/core/HostReactor.java | 8 +- .../naming/remote/NamingClientProxy.java | 148 ++++++++++++++++++ .../gprc/NamingGrpcClientProxy.java | 109 +++++++------ .../gprc/NamingPushResponseHandler.java | 2 +- .../{net => remote/http}/HttpClient.java | 2 +- .../http}/NamingHttpClientManager.java | 2 +- .../http/NamingHttpClientProxy.java} | 107 ++++++------- .../nacos/client/remote/RpcClient.java | 4 +- .../nacos/client/remote/grpc/GrpcClient.java | 6 + .../alibaba/nacos/client/BeatReactorTest.java | 6 +- .../client/naming/core/HostReactorTest.java | 10 +- .../alibaba/nacos/test/naming/NamingBase.java | 2 +- ...java => NamingHttpClientProxy_ITCase.java} | 2 +- 16 files changed, 290 insertions(+), 147 deletions(-) create mode 100644 client/src/main/java/com/alibaba/nacos/client/naming/remote/NamingClientProxy.java rename client/src/main/java/com/alibaba/nacos/client/naming/{net => remote}/gprc/NamingGrpcClientProxy.java (73%) rename client/src/main/java/com/alibaba/nacos/client/naming/{net => remote}/gprc/NamingPushResponseHandler.java (96%) rename client/src/main/java/com/alibaba/nacos/client/naming/{net => remote/http}/HttpClient.java (99%) rename client/src/main/java/com/alibaba/nacos/client/naming/{net => remote/http}/NamingHttpClientManager.java (98%) rename client/src/main/java/com/alibaba/nacos/client/naming/{net/NamingProxy.java => remote/http/NamingHttpClientProxy.java} (93%) rename test/src/test/java/com/alibaba/nacos/test/naming/{NamingProxy_ITCase.java => NamingHttpClientProxy_ITCase.java} (99%) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingMaintainService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingMaintainService.java index f44e41b64..9a455d75e 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingMaintainService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingMaintainService.java @@ -25,7 +25,7 @@ import com.alibaba.nacos.api.naming.pojo.Service; import com.alibaba.nacos.api.selector.AbstractSelector; import com.alibaba.nacos.api.selector.ExpressionSelector; import com.alibaba.nacos.api.selector.NoneSelector; -import com.alibaba.nacos.client.naming.net.NamingProxy; +import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy; import com.alibaba.nacos.client.naming.utils.InitUtils; import com.alibaba.nacos.client.utils.ValidatorUtils; import com.alibaba.nacos.common.utils.StringUtils; @@ -48,7 +48,7 @@ public class NacosNamingMaintainService implements NamingMaintainService { private String serverList; - private NamingProxy serverProxy; + private NamingHttpClientProxy serverProxy; public NacosNamingMaintainService(String serverList) throws NacosException { Properties properties = new Properties(); @@ -66,7 +66,7 @@ public class NacosNamingMaintainService implements NamingMaintainService { InitUtils.initSerialization(); initServerAddr(properties); InitUtils.initWebRootContext(); - serverProxy = new NamingProxy(namespace, endpoint, serverList, properties); + serverProxy = new NamingHttpClientProxy(namespace, endpoint, serverList, properties); } private void initServerAddr(Properties properties) { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index 4dc43c0bd..ca0186b87 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -30,8 +30,8 @@ import com.alibaba.nacos.client.naming.beat.BeatReactor; import com.alibaba.nacos.client.naming.core.Balancer; import com.alibaba.nacos.client.naming.core.EventDispatcher; import com.alibaba.nacos.client.naming.core.HostReactor; -import com.alibaba.nacos.client.naming.net.NamingProxy; -import com.alibaba.nacos.client.naming.net.gprc.NamingGrpcClientProxy; +import com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy; +import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy; import com.alibaba.nacos.client.naming.utils.CollectionUtils; import com.alibaba.nacos.client.naming.utils.InitUtils; import com.alibaba.nacos.client.naming.utils.UtilAndComs; @@ -73,7 +73,7 @@ public class NacosNamingService implements NamingService { private EventDispatcher eventDispatcher; - private NamingProxy serverProxy; + private NamingHttpClientProxy serverProxy; private NamingGrpcClientProxy grpcClientProxy; @@ -97,7 +97,7 @@ public class NacosNamingService implements NamingService { initLogName(properties); this.eventDispatcher = new EventDispatcher(); - this.serverProxy = new NamingProxy(this.namespace, this.endpoint, this.serverList, properties); + this.serverProxy = new NamingHttpClientProxy(this.namespace, this.endpoint, this.serverList, properties); this.beatReactor = new BeatReactor(this.serverProxy, initClientBeatThreadCount(properties)); this.hostReactor = new HostReactor(this.eventDispatcher, this.serverProxy, beatReactor, this.cacheDir, isLoadCacheAtStart(properties), initPollingThreadCount(properties)); @@ -314,8 +314,8 @@ public class NacosNamingService implements NamingService { // serviceInfo = hostReactor // .getServiceInfoDirectlyFromServer(NamingUtils.getGroupedName(serviceName, groupName), // StringUtils.join(clusters, ",")); - serviceInfo = grpcClientProxy.queryInstancesOfService(NamingUtils.getGroupedName(serviceName, groupName), - StringUtils.join(clusters, ","), 0, false); + serviceInfo = grpcClientProxy + .queryInstancesOfService(serviceName, groupName, StringUtils.join(clusters, ","), 0, false); } List list; if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) { @@ -471,8 +471,7 @@ public class NacosNamingService implements NamingService { // eventDispatcher.addListener(hostReactor // .getServiceInfo(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")), // StringUtils.join(clusters, ","), listener); - eventDispatcher.addListener(grpcClientProxy - .subscribe(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")), + eventDispatcher.addListener(grpcClientProxy.subscribe(serviceName, groupName, StringUtils.join(clusters, ",")), StringUtils.join(clusters, ","), listener); } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java index 3d405151d..2c08ab49c 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java @@ -23,7 +23,7 @@ import com.alibaba.nacos.api.naming.NamingResponseCode; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.utils.NamingUtils; import com.alibaba.nacos.client.monitor.MetricsMonitor; -import com.alibaba.nacos.client.naming.net.NamingProxy; +import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy; import com.alibaba.nacos.client.naming.utils.UtilAndComs; import com.alibaba.nacos.common.lifecycle.Closeable; import com.alibaba.nacos.common.utils.JacksonUtils; @@ -48,17 +48,17 @@ public class BeatReactor implements Closeable { private final ScheduledExecutorService executorService; - private final NamingProxy serverProxy; + private final NamingHttpClientProxy serverProxy; private boolean lightBeatEnabled = false; public final Map dom2Beat = new ConcurrentHashMap(); - public BeatReactor(NamingProxy serverProxy) { + public BeatReactor(NamingHttpClientProxy serverProxy) { this(serverProxy, UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT); } - public BeatReactor(NamingProxy serverProxy, int threadCount) { + public BeatReactor(NamingHttpClientProxy serverProxy, int threadCount) { this.serverProxy = serverProxy; this.executorService = new ScheduledThreadPoolExecutor(threadCount, new ThreadFactory() { @Override diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java index 759e0e0cd..c48e756f9 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java @@ -24,7 +24,7 @@ import com.alibaba.nacos.client.naming.backups.FailoverReactor; import com.alibaba.nacos.client.naming.beat.BeatInfo; import com.alibaba.nacos.client.naming.beat.BeatReactor; import com.alibaba.nacos.client.naming.cache.DiskCache; -import com.alibaba.nacos.client.naming.net.NamingProxy; +import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy; import com.alibaba.nacos.client.naming.utils.UtilAndComs; import com.alibaba.nacos.common.lifecycle.Closeable; import com.alibaba.nacos.common.utils.JacksonUtils; @@ -69,7 +69,7 @@ public class HostReactor implements Closeable { private final BeatReactor beatReactor; - private final NamingProxy serverProxy; + private final NamingHttpClientProxy serverProxy; private final FailoverReactor failoverReactor; @@ -77,12 +77,12 @@ public class HostReactor implements Closeable { private final ScheduledExecutorService executor; - public HostReactor(EventDispatcher eventDispatcher, NamingProxy serverProxy, BeatReactor beatReactor, + public HostReactor(EventDispatcher eventDispatcher, NamingHttpClientProxy serverProxy, BeatReactor beatReactor, String cacheDir) { this(eventDispatcher, serverProxy, beatReactor, cacheDir, false, UtilAndComs.DEFAULT_POLLING_THREAD_COUNT); } - public HostReactor(EventDispatcher eventDispatcher, NamingProxy serverProxy, BeatReactor beatReactor, + public HostReactor(EventDispatcher eventDispatcher, NamingHttpClientProxy serverProxy, BeatReactor beatReactor, String cacheDir, boolean loadCacheAtStart, int pollingThreadCount) { // init executorService this.executor = new ScheduledThreadPoolExecutor(pollingThreadCount, new ThreadFactory() { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/remote/NamingClientProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/remote/NamingClientProxy.java new file mode 100644 index 000000000..c7bcb2b61 --- /dev/null +++ b/client/src/main/java/com/alibaba/nacos/client/naming/remote/NamingClientProxy.java @@ -0,0 +1,148 @@ +/* + * 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.remote; + +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.pojo.Instance; +import com.alibaba.nacos.api.naming.pojo.ListView; +import com.alibaba.nacos.api.naming.pojo.Service; +import com.alibaba.nacos.api.naming.pojo.ServiceInfo; +import com.alibaba.nacos.api.selector.AbstractSelector; +import com.alibaba.nacos.common.lifecycle.Closeable; + +/** + * Naming Client Proxy. + * + * @author xiweng.yy + */ +public interface NamingClientProxy extends Closeable { + + /** + * 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 + */ + void registerService(String serviceName, String groupName, Instance instance) throws NacosException; + + /** + * Deregister instance from a service. + * + * @param serviceName name of service + * @param groupName group name + * @param instance instance + * @throws NacosException nacos exception + */ + void deregisterService(String serviceName, String groupName, Instance instance) throws NacosException; + + /** + * Update instance to service. + * + * @param serviceName service name + * @param groupName group name + * @param instance instance + * @throws NacosException nacos exception + */ + void updateInstance(String serviceName, String groupName, Instance instance) throws NacosException; + + /** + * Query instance list. + * + * @param serviceName service name + * @param groupName group name + * @param clusters clusters + * @param udpPort udp port + * @param healthyOnly healthy only + * @return service info + * @throws NacosException nacos exception + */ + ServiceInfo queryInstancesOfService(String serviceName, String groupName, String clusters, int udpPort, boolean healthyOnly) + throws NacosException; + + /** + * Query Service. + * + * @param serviceName service name + * @param groupName group name + * @return service + * @throws NacosException nacos exception + */ + Service queryService(String serviceName, String groupName) throws NacosException; + + /** + * Create service. + * + * @param service service + * @param selector selector + * @throws NacosException nacos exception + */ + void createService(Service service, AbstractSelector selector) throws NacosException; + + /** + * Delete service. + * + * @param serviceName service name + * @param groupName group name + * @return true if delete ok + * @throws NacosException nacos exception + */ + boolean deleteService(String serviceName, String groupName) throws NacosException; + + /** + * Update service. + * + * @param service service + * @param selector selector + * @throws NacosException nacos exception + */ + void updateService(Service service, AbstractSelector selector) throws NacosException; + + /** + * Get service list. + * + * @param pageNo page number + * @param pageSize size per page + * @param groupName group name of service + * @param selector selector + * @return list of service + * @throws NacosException nacos exception + */ + ListView getServiceList(int pageNo, int pageSize, String groupName, AbstractSelector selector) + throws NacosException; + + /** + * Subscribe service. + * + * @param serviceName service name + * @param groupName group name + * @param clusters clusters, current only support subscribe all clusters, maybe deprecated + * @return current service info of subscribe service + * @throws NacosException nacos exception + */ + ServiceInfo subscribe(String serviceName, String groupName, String clusters) throws NacosException; + + /** + * Unsubscribe service. + * + * @param serviceName full service name with group + * @param clusters clusters, current only support subscribe all clusters, maybe deprecated + * @throws NacosException nacos exception + */ + void unsubscribe(String serviceName, String clusters) throws NacosException; +} diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/gprc/NamingGrpcClientProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcClientProxy.java similarity index 73% rename from client/src/main/java/com/alibaba/nacos/client/naming/net/gprc/NamingGrpcClientProxy.java rename to client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcClientProxy.java index d8fe9c115..3671a66f4 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/gprc/NamingGrpcClientProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcClientProxy.java @@ -14,10 +14,12 @@ * limitations under the License. */ -package com.alibaba.nacos.client.naming.net.gprc; +package com.alibaba.nacos.client.naming.remote.gprc; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.pojo.Instance; +import com.alibaba.nacos.api.naming.pojo.ListView; +import com.alibaba.nacos.api.naming.pojo.Service; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.api.naming.remote.NamingRemoteConstants; import com.alibaba.nacos.api.naming.remote.request.InstanceRequest; @@ -25,9 +27,12 @@ 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.QueryServiceResponse; import com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse; +import com.alibaba.nacos.api.naming.utils.NamingUtils; import com.alibaba.nacos.api.remote.request.Request; import com.alibaba.nacos.api.remote.response.Response; +import com.alibaba.nacos.api.selector.AbstractSelector; import com.alibaba.nacos.client.naming.core.HostReactor; +import com.alibaba.nacos.client.naming.remote.NamingClientProxy; import com.alibaba.nacos.client.remote.RpcClient; import com.alibaba.nacos.client.remote.RpcClientFactory; import com.alibaba.nacos.client.remote.ServerListFactory; @@ -39,7 +44,7 @@ import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; * * @author xiweng.yy */ -public class NamingGrpcClientProxy { +public class NamingGrpcClientProxy implements NamingClientProxy { private final String namespaceId; @@ -65,14 +70,7 @@ public class NamingGrpcClientProxy { rpcClient.registerServerPushResponseHandler(new NamingPushResponseHandler(hostReactor)); } - /** - * 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 - */ + @Override public void registerService(String serviceName, String groupName, Instance instance) throws NacosException { NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance {}", namespaceId, serviceName, instance); @@ -82,14 +80,7 @@ public class NamingGrpcClientProxy { requestToServer(request, Response.class); } - /** - * deregister instance from a service. - * - * @param serviceName name of service - * @param groupName group name - * @param instance instance - * @throws NacosException nacos exception - */ + @Override public void deregisterService(String serviceName, String groupName, Instance instance) throws NacosException { NAMING_LOGGER .info("[DEREGISTER-SERVICE] {} deregistering service {} with instance: {}", namespaceId, serviceName, @@ -99,19 +90,15 @@ public class NamingGrpcClientProxy { requestToServer(request, Response.class); } - /** - * Query instance list. - * - * @param serviceName service name - * @param clusters clusters - * @param udpPort udp port - * @param healthyOnly healthy only - * @return service info - * @throws NacosException nacos exception - */ - public ServiceInfo queryInstancesOfService(String serviceName, String clusters, int udpPort, boolean healthyOnly) - throws NacosException { - ServiceQueryRequest request = new ServiceQueryRequest(namespaceId, serviceName); + @Override + public void updateInstance(String serviceName, String groupName, Instance instance) throws NacosException { + + } + + @Override + public ServiceInfo queryInstancesOfService(String serviceName, String groupName, String clusters, int udpPort, + boolean healthyOnly) throws NacosException { + ServiceQueryRequest request = new ServiceQueryRequest(namespaceId, NamingUtils.getGroupedName(serviceName, groupName)); request.setCluster(clusters); request.setHealthyOnly(healthyOnly); request.setUdpPort(udpPort); @@ -119,35 +106,50 @@ public class NamingGrpcClientProxy { return response.getServiceInfo(); } - /** - * Subscribe service. - * - * @param serviceName full service name with group - * @param clusters clusters, current only support subscribe all clusters, maybe deprecated - * @return current service info of subscribe service - * @throws NacosException nacos exception - */ - public ServiceInfo subscribe(String serviceName, String clusters) throws NacosException { - ServiceInfo serviceInfo = new ServiceInfo(serviceName, clusters); + @Override + public Service queryService(String serviceName, String groupName) throws NacosException { + return null; + } + + @Override + public void createService(Service service, AbstractSelector selector) throws NacosException { + + } + + @Override + public boolean deleteService(String serviceName, String groupName) throws NacosException { + return false; + } + + @Override + public void updateService(Service service, AbstractSelector selector) throws NacosException { + + } + + @Override + public ListView getServiceList(int pageNo, int pageSize, String groupName, AbstractSelector selector) + throws NacosException { + return null; + } + + @Override + public ServiceInfo subscribe(String serviceName, String groupName, String clusters) throws NacosException { + String serviceNameWithGroup = NamingUtils.getGroupedName(serviceName, groupName); + ServiceInfo serviceInfo = new ServiceInfo(serviceNameWithGroup, clusters); if (hostReactor.getServiceInfoMap().containsKey(serviceInfo.getKey())) { return hostReactor.getServiceInfoMap().get(serviceInfo.getKey()); } - hostReactor.updatingService(serviceName); - SubscribeServiceRequest request = new SubscribeServiceRequest(namespaceId, serviceName, clusters, true); + hostReactor.updatingService(serviceNameWithGroup); + SubscribeServiceRequest request = new SubscribeServiceRequest(namespaceId, serviceNameWithGroup, clusters, + true); SubscribeServiceResponse response = requestToServer(request, SubscribeServiceResponse.class); ServiceInfo result = response.getServiceInfo(); hostReactor.getServiceInfoMap().put(result.getKey(), result); - hostReactor.finishUpdating(serviceName); + hostReactor.finishUpdating(serviceNameWithGroup); return result; } - /** - * Unsubscribe service. - * - * @param serviceName full service name with group - * @param clusters clusters, current only support subscribe all clusters, maybe deprecated - * @throws NacosException nacos exception - */ + @Override public void unsubscribe(String serviceName, String clusters) throws NacosException { SubscribeServiceRequest request = new SubscribeServiceRequest(namespaceId, serviceName, clusters, false); requestToServer(request, SubscribeServiceResponse.class); @@ -169,4 +171,9 @@ public class NamingGrpcClientProxy { } throw new NacosException(NacosException.SERVER_ERROR, "Server return invalid response"); } + + @Override + public void shutdown() throws NacosException { + rpcClient.shutdown(); + } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/gprc/NamingPushResponseHandler.java b/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingPushResponseHandler.java similarity index 96% rename from client/src/main/java/com/alibaba/nacos/client/naming/net/gprc/NamingPushResponseHandler.java rename to client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingPushResponseHandler.java index 878b56732..7f991f174 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/gprc/NamingPushResponseHandler.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingPushResponseHandler.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.alibaba.nacos.client.naming.net.gprc; +package com.alibaba.nacos.client.naming.remote.gprc; import com.alibaba.nacos.api.naming.remote.response.NotifySubscriberResponse; import com.alibaba.nacos.api.remote.response.Response; diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/HttpClient.java b/client/src/main/java/com/alibaba/nacos/client/naming/remote/http/HttpClient.java similarity index 99% rename from client/src/main/java/com/alibaba/nacos/client/naming/net/HttpClient.java rename to client/src/main/java/com/alibaba/nacos/client/naming/remote/http/HttpClient.java index dd6ebe5a7..944c0acec 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/HttpClient.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/remote/http/HttpClient.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.alibaba.nacos.client.naming.net; +package com.alibaba.nacos.client.naming.remote.http; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.common.http.client.NacosRestTemplate; diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingHttpClientManager.java b/client/src/main/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientManager.java similarity index 98% rename from client/src/main/java/com/alibaba/nacos/client/naming/net/NamingHttpClientManager.java rename to client/src/main/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientManager.java index b66ee09e1..914a80b30 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingHttpClientManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientManager.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.alibaba.nacos.client.naming.net; +package com.alibaba.nacos.client.naming.remote.http; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.common.http.AbstractHttpClientFactory; diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientProxy.java similarity index 93% rename from client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java rename to client/src/main/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientProxy.java index 2d8f5791c..468a4efc0 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientProxy.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.alibaba.nacos.client.naming.net; +package com.alibaba.nacos.client.naming.remote.http; import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.SystemPropertyKeyConst; @@ -24,12 +24,15 @@ import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ListView; import com.alibaba.nacos.api.naming.pojo.Service; +import com.alibaba.nacos.api.naming.pojo.ServiceInfo; +import com.alibaba.nacos.api.naming.utils.NamingUtils; import com.alibaba.nacos.api.selector.AbstractSelector; import com.alibaba.nacos.api.selector.ExpressionSelector; import com.alibaba.nacos.api.selector.SelectorType; import com.alibaba.nacos.client.config.impl.SpasAdapter; import com.alibaba.nacos.client.monitor.MetricsMonitor; import com.alibaba.nacos.client.naming.beat.BeatInfo; +import com.alibaba.nacos.client.naming.remote.NamingClientProxy; import com.alibaba.nacos.client.naming.utils.CollectionUtils; import com.alibaba.nacos.client.naming.utils.NetUtils; import com.alibaba.nacos.client.naming.utils.SignUtil; @@ -43,7 +46,6 @@ import com.alibaba.nacos.common.http.HttpRestResult; import com.alibaba.nacos.common.http.client.NacosRestTemplate; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.Query; -import com.alibaba.nacos.common.lifecycle.Closeable; import com.alibaba.nacos.common.utils.HttpMethod; import com.alibaba.nacos.common.utils.IoUtils; import com.alibaba.nacos.common.utils.JacksonUtils; @@ -78,7 +80,7 @@ import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; * * @author nkorange */ -public class NamingProxy implements Closeable { +public class NamingHttpClientProxy implements NamingClientProxy { private final NacosRestTemplate nacosRestTemplate = NamingHttpClientManager.getInstance().getNacosRestTemplate(); @@ -108,7 +110,7 @@ public class NamingProxy implements Closeable { private ScheduledExecutorService executorService; - public NamingProxy(String namespaceId, String endpoint, String serverList, Properties properties) { + public NamingHttpClientProxy(String namespaceId, String endpoint, String serverList, Properties properties) { this.securityProxy = new SecurityProxy(properties, nacosRestTemplate); this.properties = properties; @@ -211,14 +213,7 @@ public class NamingProxy implements Closeable { } } - /** - * 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 - */ + @Override public void registerService(String serviceName, String groupName, Instance instance) throws NacosException { NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance: {}", namespaceId, serviceName, @@ -241,38 +236,24 @@ public class NamingProxy implements Closeable { } - /** - * 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 { - + @Override + public void deregisterService(String serviceName, String groupName, Instance instance) throws NacosException { NAMING_LOGGER .info("[DEREGISTER-SERVICE] {} deregistering service {} with instance: {}", namespaceId, serviceName, instance); - + final Map params = new HashMap(8); params.put(CommonParams.NAMESPACE_ID, namespaceId); - params.put(CommonParams.SERVICE_NAME, serviceName); + params.put(CommonParams.SERVICE_NAME, NamingUtils.getGroupedName(serviceName, groupName)); params.put(CommonParams.CLUSTER_NAME, instance.getClusterName()); params.put("ip", instance.getIp()); params.put("port", String.valueOf(instance.getPort())); params.put("ephemeral", String.valueOf(instance.isEphemeral())); - + reqApi(UtilAndComs.nacosUrlInstance, params, HttpMethod.DELETE); } - /** - * Update instance to service. - * - * @param serviceName service name - * @param groupName group name - * @param instance instance - * @throws NacosException nacos exception - */ + @Override public void updateInstance(String serviceName, String groupName, Instance instance) throws NacosException { NAMING_LOGGER .info("[UPDATE-SERVICE] {} update service {} with instance: {}", namespaceId, serviceName, instance); @@ -292,14 +273,24 @@ public class NamingProxy implements Closeable { reqApi(UtilAndComs.nacosUrlInstance, params, HttpMethod.PUT); } - /** - * Query Service. - * - * @param serviceName service name - * @param groupName group name - * @return service - * @throws NacosException nacos exception - */ + @Override + public ServiceInfo queryInstancesOfService(String serviceName, String groupName, String clusters, int udpPort, boolean healthyOnly) + throws NacosException { + final Map params = new HashMap(8); + params.put(CommonParams.NAMESPACE_ID, namespaceId); + params.put(CommonParams.SERVICE_NAME, serviceName); + params.put("clusters", clusters); + params.put("udpPort", String.valueOf(udpPort)); + params.put("clientIP", NetUtils.localIP()); + params.put("healthyOnly", String.valueOf(healthyOnly)); + String result = reqApi(UtilAndComs.nacosUrlBase + "/instance/list", params, HttpMethod.GET); + if (StringUtils.isNotEmpty(result)) { + return JacksonUtils.toObj(result, ServiceInfo.class); + } + return null; + } + + @Override public Service queryService(String serviceName, String groupName) throws NacosException { NAMING_LOGGER.info("[QUERY-SERVICE] {} query service : {}, {}", namespaceId, serviceName, groupName); @@ -312,13 +303,7 @@ public class NamingProxy implements Closeable { return JacksonUtils.toObj(result, Service.class); } - /** - * Create service. - * - * @param service service - * @param selector selector - * @throws NacosException nacos exception - */ + @Override public void createService(Service service, AbstractSelector selector) throws NacosException { NAMING_LOGGER.info("[CREATE-SERVICE] {} creating service : {}", namespaceId, service); @@ -335,14 +320,7 @@ public class NamingProxy implements Closeable { } - /** - * Delete service. - * - * @param serviceName service name - * @param groupName group name - * @return true if delete ok - * @throws NacosException nacos exception - */ + @Override public boolean deleteService(String serviceName, String groupName) throws NacosException { NAMING_LOGGER.info("[DELETE-SERVICE] {} deleting service : {} with groupName : {}", namespaceId, serviceName, groupName); @@ -356,13 +334,7 @@ public class NamingProxy implements Closeable { return "ok".equals(result); } - /** - * Update service. - * - * @param service service - * @param selector selector - * @throws NacosException nacos exception - */ + @Override public void updateService(Service service, AbstractSelector selector) throws NacosException { NAMING_LOGGER.info("[UPDATE-SERVICE] {} updating service : {}", namespaceId, service); @@ -450,6 +422,7 @@ public class NamingProxy implements Closeable { return getServiceList(pageNo, pageSize, groupName, null); } + @Override public ListView getServiceList(int pageNo, int pageSize, String groupName, AbstractSelector selector) throws NacosException { @@ -483,6 +456,16 @@ public class NamingProxy implements Closeable { return listView; } + @Override + public ServiceInfo subscribe(String serviceName, String groupName, String clusters) throws NacosException { + return null; + } + + @Override + public void unsubscribe(String serviceName, String clusters) throws NacosException { + + } + public String reqApi(String api, Map params, String method) throws NacosException { return reqApi(api, params, Collections.EMPTY_MAP, method); } diff --git a/client/src/main/java/com/alibaba/nacos/client/remote/RpcClient.java b/client/src/main/java/com/alibaba/nacos/client/remote/RpcClient.java index b42f68b4e..1ba347de4 100644 --- a/client/src/main/java/com/alibaba/nacos/client/remote/RpcClient.java +++ b/client/src/main/java/com/alibaba/nacos/client/remote/RpcClient.java @@ -20,6 +20,7 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.remote.request.Request; import com.alibaba.nacos.api.remote.response.Response; import com.alibaba.nacos.client.utils.LogUtils; +import com.alibaba.nacos.common.lifecycle.Closeable; import org.slf4j.Logger; import javax.annotation.PostConstruct; @@ -35,7 +36,7 @@ import java.util.function.Consumer; * @author liuzunfei * @version $Id: RpcClient.java, v 0.1 2020年07月13日 9:15 PM liuzunfei Exp $ */ -public abstract class RpcClient { +public abstract class RpcClient implements Closeable { private static final Logger LOGGER = LogUtils.logger(RpcClient.class); @@ -181,5 +182,4 @@ public abstract class RpcClient { public ServerListFactory getServerListFactory() { return serverListFactory; } - } diff --git a/client/src/main/java/com/alibaba/nacos/client/remote/grpc/GrpcClient.java b/client/src/main/java/com/alibaba/nacos/client/remote/grpc/GrpcClient.java index bd0506f59..6dadb7ace 100644 --- a/client/src/main/java/com/alibaba/nacos/client/remote/grpc/GrpcClient.java +++ b/client/src/main/java/com/alibaba/nacos/client/remote/grpc/GrpcClient.java @@ -347,4 +347,10 @@ public class GrpcClient extends RpcClient { } } + @Override + public void shutdown() throws NacosException { + if (!this.channel.isShutdown()) { + this.channel.shutdownNow(); + } + } } diff --git a/client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java b/client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java index 0b9b96d35..9da690595 100644 --- a/client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java @@ -19,7 +19,7 @@ package com.alibaba.nacos.client; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.naming.beat.BeatInfo; import com.alibaba.nacos.client.naming.beat.BeatReactor; -import com.alibaba.nacos.client.naming.net.NamingProxy; +import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -34,7 +34,7 @@ import java.util.concurrent.ScheduledThreadPoolExecutor; public class BeatReactorTest { @Mock - private NamingProxy namingProxy; + private NamingHttpClientProxy namingHttpClientProxy; @Test public void test() throws NoSuchFieldException, IllegalAccessException, InterruptedException, NacosException { @@ -48,7 +48,7 @@ public class BeatReactorTest { beatInfo.setScheduled(false); beatInfo.setPeriod(1000L); - BeatReactor beatReactor = new BeatReactor(namingProxy); + BeatReactor beatReactor = new BeatReactor(namingHttpClientProxy); beatReactor.addBeatInfo("testService", beatInfo); Assert.assertEquals(1, getActiveThread(beatReactor)); diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/core/HostReactorTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/core/HostReactorTest.java index 390ad3e2d..a803cc1ab 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/core/HostReactorTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/core/HostReactorTest.java @@ -21,7 +21,7 @@ import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.client.naming.beat.BeatInfo; import com.alibaba.nacos.client.naming.beat.BeatReactor; -import com.alibaba.nacos.client.naming.net.NamingProxy; +import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -42,7 +42,7 @@ public class HostReactorTest { private static final String CACHE_DIR = HostReactorTest.class.getResource("/").getPath() + "cache/"; @Mock - private NamingProxy namingProxy; + private NamingHttpClientProxy namingHttpClientProxy; @Mock private EventDispatcher eventDispatcher; @@ -53,7 +53,7 @@ public class HostReactorTest { @Before public void setUp() throws Exception { - beatReactor = new BeatReactor(namingProxy); + beatReactor = new BeatReactor(namingHttpClientProxy); BeatInfo beatInfo = new BeatInfo(); beatInfo.setServiceName("testName"); beatInfo.setIp("1.1.1.1"); @@ -64,7 +64,7 @@ public class HostReactorTest { beatInfo.setScheduled(false); beatInfo.setPeriod(1000L); beatReactor.addBeatInfo("testName", beatInfo); - hostReactor = new HostReactor(eventDispatcher, namingProxy, beatReactor, CACHE_DIR); + hostReactor = new HostReactor(eventDispatcher, namingHttpClientProxy, beatReactor, CACHE_DIR); } @Test @@ -78,7 +78,7 @@ public class HostReactorTest { @Test public void testGetServiceInfoDirectlyFromServer() throws NacosException { - when(namingProxy.queryList("testName", "testClusters", 0, false)).thenReturn(EXAMPLE); + when(namingHttpClientProxy.queryList("testName", "testClusters", 0, false)).thenReturn(EXAMPLE); ServiceInfo actual = hostReactor.getServiceInfoDirectlyFromServer("testName", "testClusters"); assertServiceInfo(actual); } diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java b/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java index df7bd20e6..7b89f1159 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java @@ -16,7 +16,7 @@ package com.alibaba.nacos.test.naming; import com.alibaba.nacos.api.naming.pojo.Instance; -import com.alibaba.nacos.client.naming.net.NamingHttpClientManager; +import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientManager; import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.http.HttpRestResult; import com.alibaba.nacos.common.http.client.NacosRestTemplate; diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/NamingProxy_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/NamingHttpClientProxy_ITCase.java similarity index 99% rename from test/src/test/java/com/alibaba/nacos/test/naming/NamingProxy_ITCase.java rename to test/src/test/java/com/alibaba/nacos/test/naming/NamingHttpClientProxy_ITCase.java index 8e91cfd02..b7aa0e0f8 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/NamingProxy_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/NamingHttpClientProxy_ITCase.java @@ -46,7 +46,7 @@ import java.util.concurrent.TimeUnit; @RunWith(SpringRunner.class) @SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -public class NamingProxy_ITCase { +public class NamingHttpClientProxy_ITCase { @LocalServerPort private int port; @Autowired