[ISSUE #3386] Extract NamingClientProxy Interface (#3387)

* Move and rename NamingProxy.java

* rename package naming.net --> naming.remote

* Create Naming client proxy interface
This commit is contained in:
杨翊 SionYang 2020-07-20 16:32:07 +08:00 committed by GitHub
parent 85a0398978
commit 39986a867a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 290 additions and 147 deletions

View File

@ -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.AbstractSelector;
import com.alibaba.nacos.api.selector.ExpressionSelector; import com.alibaba.nacos.api.selector.ExpressionSelector;
import com.alibaba.nacos.api.selector.NoneSelector; 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.naming.utils.InitUtils;
import com.alibaba.nacos.client.utils.ValidatorUtils; import com.alibaba.nacos.client.utils.ValidatorUtils;
import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.StringUtils;
@ -48,7 +48,7 @@ public class NacosNamingMaintainService implements NamingMaintainService {
private String serverList; private String serverList;
private NamingProxy serverProxy; private NamingHttpClientProxy serverProxy;
public NacosNamingMaintainService(String serverList) throws NacosException { public NacosNamingMaintainService(String serverList) throws NacosException {
Properties properties = new Properties(); Properties properties = new Properties();
@ -66,7 +66,7 @@ public class NacosNamingMaintainService implements NamingMaintainService {
InitUtils.initSerialization(); InitUtils.initSerialization();
initServerAddr(properties); initServerAddr(properties);
InitUtils.initWebRootContext(); InitUtils.initWebRootContext();
serverProxy = new NamingProxy(namespace, endpoint, serverList, properties); serverProxy = new NamingHttpClientProxy(namespace, endpoint, serverList, properties);
} }
private void initServerAddr(Properties properties) { private void initServerAddr(Properties properties) {

View File

@ -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.Balancer;
import com.alibaba.nacos.client.naming.core.EventDispatcher; import com.alibaba.nacos.client.naming.core.EventDispatcher;
import com.alibaba.nacos.client.naming.core.HostReactor; import com.alibaba.nacos.client.naming.core.HostReactor;
import com.alibaba.nacos.client.naming.net.NamingProxy; import com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy;
import com.alibaba.nacos.client.naming.net.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.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.InitUtils; import com.alibaba.nacos.client.naming.utils.InitUtils;
import com.alibaba.nacos.client.naming.utils.UtilAndComs; import com.alibaba.nacos.client.naming.utils.UtilAndComs;
@ -73,7 +73,7 @@ public class NacosNamingService implements NamingService {
private EventDispatcher eventDispatcher; private EventDispatcher eventDispatcher;
private NamingProxy serverProxy; private NamingHttpClientProxy serverProxy;
private NamingGrpcClientProxy grpcClientProxy; private NamingGrpcClientProxy grpcClientProxy;
@ -97,7 +97,7 @@ public class NacosNamingService implements NamingService {
initLogName(properties); initLogName(properties);
this.eventDispatcher = new EventDispatcher(); 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.beatReactor = new BeatReactor(this.serverProxy, initClientBeatThreadCount(properties));
this.hostReactor = new HostReactor(this.eventDispatcher, this.serverProxy, beatReactor, this.cacheDir, this.hostReactor = new HostReactor(this.eventDispatcher, this.serverProxy, beatReactor, this.cacheDir,
isLoadCacheAtStart(properties), initPollingThreadCount(properties)); isLoadCacheAtStart(properties), initPollingThreadCount(properties));
@ -314,8 +314,8 @@ public class NacosNamingService implements NamingService {
// serviceInfo = hostReactor // serviceInfo = hostReactor
// .getServiceInfoDirectlyFromServer(NamingUtils.getGroupedName(serviceName, groupName), // .getServiceInfoDirectlyFromServer(NamingUtils.getGroupedName(serviceName, groupName),
// StringUtils.join(clusters, ",")); // StringUtils.join(clusters, ","));
serviceInfo = grpcClientProxy.queryInstancesOfService(NamingUtils.getGroupedName(serviceName, groupName), serviceInfo = grpcClientProxy
StringUtils.join(clusters, ","), 0, false); .queryInstancesOfService(serviceName, groupName, StringUtils.join(clusters, ","), 0, false);
} }
List<Instance> list; List<Instance> list;
if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) { if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) {
@ -471,8 +471,7 @@ public class NacosNamingService implements NamingService {
// eventDispatcher.addListener(hostReactor // eventDispatcher.addListener(hostReactor
// .getServiceInfo(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")), // .getServiceInfo(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")),
// StringUtils.join(clusters, ","), listener); // StringUtils.join(clusters, ","), listener);
eventDispatcher.addListener(grpcClientProxy eventDispatcher.addListener(grpcClientProxy.subscribe(serviceName, groupName, StringUtils.join(clusters, ",")),
.subscribe(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")),
StringUtils.join(clusters, ","), listener); StringUtils.join(clusters, ","), listener);
} }

View File

@ -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.pojo.Instance;
import com.alibaba.nacos.api.naming.utils.NamingUtils; import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.client.monitor.MetricsMonitor; 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.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.common.lifecycle.Closeable; import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.JacksonUtils;
@ -48,17 +48,17 @@ public class BeatReactor implements Closeable {
private final ScheduledExecutorService executorService; private final ScheduledExecutorService executorService;
private final NamingProxy serverProxy; private final NamingHttpClientProxy serverProxy;
private boolean lightBeatEnabled = false; private boolean lightBeatEnabled = false;
public final Map<String, BeatInfo> dom2Beat = new ConcurrentHashMap<String, BeatInfo>(); public final Map<String, BeatInfo> dom2Beat = new ConcurrentHashMap<String, BeatInfo>();
public BeatReactor(NamingProxy serverProxy) { public BeatReactor(NamingHttpClientProxy serverProxy) {
this(serverProxy, UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT); this(serverProxy, UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT);
} }
public BeatReactor(NamingProxy serverProxy, int threadCount) { public BeatReactor(NamingHttpClientProxy serverProxy, int threadCount) {
this.serverProxy = serverProxy; this.serverProxy = serverProxy;
this.executorService = new ScheduledThreadPoolExecutor(threadCount, new ThreadFactory() { this.executorService = new ScheduledThreadPoolExecutor(threadCount, new ThreadFactory() {
@Override @Override

View File

@ -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.BeatInfo;
import com.alibaba.nacos.client.naming.beat.BeatReactor; import com.alibaba.nacos.client.naming.beat.BeatReactor;
import com.alibaba.nacos.client.naming.cache.DiskCache; 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.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.common.lifecycle.Closeable; import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.JacksonUtils;
@ -69,7 +69,7 @@ public class HostReactor implements Closeable {
private final BeatReactor beatReactor; private final BeatReactor beatReactor;
private final NamingProxy serverProxy; private final NamingHttpClientProxy serverProxy;
private final FailoverReactor failoverReactor; private final FailoverReactor failoverReactor;
@ -77,12 +77,12 @@ public class HostReactor implements Closeable {
private final ScheduledExecutorService executor; private final ScheduledExecutorService executor;
public HostReactor(EventDispatcher eventDispatcher, NamingProxy serverProxy, BeatReactor beatReactor, public HostReactor(EventDispatcher eventDispatcher, NamingHttpClientProxy serverProxy, BeatReactor beatReactor,
String cacheDir) { String cacheDir) {
this(eventDispatcher, serverProxy, beatReactor, cacheDir, false, UtilAndComs.DEFAULT_POLLING_THREAD_COUNT); 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) { String cacheDir, boolean loadCacheAtStart, int pollingThreadCount) {
// init executorService // init executorService
this.executor = new ScheduledThreadPoolExecutor(pollingThreadCount, new ThreadFactory() { this.executor = new ScheduledThreadPoolExecutor(pollingThreadCount, new ThreadFactory() {

View File

@ -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<String> 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;
}

View File

@ -14,10 +14,12 @@
* limitations under the License. * 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.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.Instance; 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.pojo.ServiceInfo;
import com.alibaba.nacos.api.naming.remote.NamingRemoteConstants; import com.alibaba.nacos.api.naming.remote.NamingRemoteConstants;
import com.alibaba.nacos.api.naming.remote.request.InstanceRequest; 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.request.SubscribeServiceRequest;
import com.alibaba.nacos.api.naming.remote.response.QueryServiceResponse; import com.alibaba.nacos.api.naming.remote.response.QueryServiceResponse;
import com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse; 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.request.Request;
import com.alibaba.nacos.api.remote.response.Response; 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.core.HostReactor;
import com.alibaba.nacos.client.naming.remote.NamingClientProxy;
import com.alibaba.nacos.client.remote.RpcClient; import com.alibaba.nacos.client.remote.RpcClient;
import com.alibaba.nacos.client.remote.RpcClientFactory; import com.alibaba.nacos.client.remote.RpcClientFactory;
import com.alibaba.nacos.client.remote.ServerListFactory; import com.alibaba.nacos.client.remote.ServerListFactory;
@ -39,7 +44,7 @@ import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER;
* *
* @author xiweng.yy * @author xiweng.yy
*/ */
public class NamingGrpcClientProxy { public class NamingGrpcClientProxy implements NamingClientProxy {
private final String namespaceId; private final String namespaceId;
@ -65,14 +70,7 @@ public class NamingGrpcClientProxy {
rpcClient.registerServerPushResponseHandler(new NamingPushResponseHandler(hostReactor)); rpcClient.registerServerPushResponseHandler(new NamingPushResponseHandler(hostReactor));
} }
/** @Override
* 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 { public void registerService(String serviceName, String groupName, Instance instance) throws NacosException {
NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance {}", namespaceId, serviceName, NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance {}", namespaceId, serviceName,
instance); instance);
@ -82,14 +80,7 @@ public class NamingGrpcClientProxy {
requestToServer(request, Response.class); requestToServer(request, Response.class);
} }
/** @Override
* deregister instance from a service.
*
* @param serviceName name of service
* @param groupName group name
* @param instance instance
* @throws NacosException nacos exception
*/
public void deregisterService(String serviceName, String groupName, Instance instance) throws NacosException { public void deregisterService(String serviceName, String groupName, Instance instance) throws NacosException {
NAMING_LOGGER NAMING_LOGGER
.info("[DEREGISTER-SERVICE] {} deregistering service {} with instance: {}", namespaceId, serviceName, .info("[DEREGISTER-SERVICE] {} deregistering service {} with instance: {}", namespaceId, serviceName,
@ -99,19 +90,15 @@ public class NamingGrpcClientProxy {
requestToServer(request, Response.class); requestToServer(request, Response.class);
} }
/** @Override
* Query instance list. public void updateInstance(String serviceName, String groupName, Instance instance) throws NacosException {
*
* @param serviceName service name }
* @param clusters clusters
* @param udpPort udp port @Override
* @param healthyOnly healthy only public ServiceInfo queryInstancesOfService(String serviceName, String groupName, String clusters, int udpPort,
* @return service info boolean healthyOnly) throws NacosException {
* @throws NacosException nacos exception ServiceQueryRequest request = new ServiceQueryRequest(namespaceId, NamingUtils.getGroupedName(serviceName, groupName));
*/
public ServiceInfo queryInstancesOfService(String serviceName, String clusters, int udpPort, boolean healthyOnly)
throws NacosException {
ServiceQueryRequest request = new ServiceQueryRequest(namespaceId, serviceName);
request.setCluster(clusters); request.setCluster(clusters);
request.setHealthyOnly(healthyOnly); request.setHealthyOnly(healthyOnly);
request.setUdpPort(udpPort); request.setUdpPort(udpPort);
@ -119,35 +106,50 @@ public class NamingGrpcClientProxy {
return response.getServiceInfo(); return response.getServiceInfo();
} }
/** @Override
* Subscribe service. public Service queryService(String serviceName, String groupName) throws NacosException {
* return null;
* @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 @Override
* @throws NacosException nacos exception public void createService(Service service, AbstractSelector selector) throws NacosException {
*/
public ServiceInfo subscribe(String serviceName, String clusters) throws NacosException { }
ServiceInfo serviceInfo = new ServiceInfo(serviceName, clusters);
@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<String> 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())) { if (hostReactor.getServiceInfoMap().containsKey(serviceInfo.getKey())) {
return hostReactor.getServiceInfoMap().get(serviceInfo.getKey()); return hostReactor.getServiceInfoMap().get(serviceInfo.getKey());
} }
hostReactor.updatingService(serviceName); hostReactor.updatingService(serviceNameWithGroup);
SubscribeServiceRequest request = new SubscribeServiceRequest(namespaceId, serviceName, clusters, true); SubscribeServiceRequest request = new SubscribeServiceRequest(namespaceId, serviceNameWithGroup, clusters,
true);
SubscribeServiceResponse response = requestToServer(request, SubscribeServiceResponse.class); SubscribeServiceResponse response = requestToServer(request, SubscribeServiceResponse.class);
ServiceInfo result = response.getServiceInfo(); ServiceInfo result = response.getServiceInfo();
hostReactor.getServiceInfoMap().put(result.getKey(), result); hostReactor.getServiceInfoMap().put(result.getKey(), result);
hostReactor.finishUpdating(serviceName); hostReactor.finishUpdating(serviceNameWithGroup);
return result; return result;
} }
/** @Override
* Unsubscribe service.
*
* @param serviceName full service name with group
* @param clusters clusters, current only support subscribe all clusters, maybe deprecated
* @throws NacosException nacos exception
*/
public void unsubscribe(String serviceName, String clusters) throws NacosException { public void unsubscribe(String serviceName, String clusters) throws NacosException {
SubscribeServiceRequest request = new SubscribeServiceRequest(namespaceId, serviceName, clusters, false); SubscribeServiceRequest request = new SubscribeServiceRequest(namespaceId, serviceName, clusters, false);
requestToServer(request, SubscribeServiceResponse.class); requestToServer(request, SubscribeServiceResponse.class);
@ -169,4 +171,9 @@ public class NamingGrpcClientProxy {
} }
throw new NacosException(NacosException.SERVER_ERROR, "Server return invalid response"); throw new NacosException(NacosException.SERVER_ERROR, "Server return invalid response");
} }
@Override
public void shutdown() throws NacosException {
rpcClient.shutdown();
}
} }

View File

@ -14,7 +14,7 @@
* limitations under the License. * 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.naming.remote.response.NotifySubscriberResponse;
import com.alibaba.nacos.api.remote.response.Response; import com.alibaba.nacos.api.remote.response.Response;

View File

@ -14,7 +14,7 @@
* limitations under the License. * 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.api.common.Constants;
import com.alibaba.nacos.common.http.client.NacosRestTemplate; import com.alibaba.nacos.common.http.client.NacosRestTemplate;

View File

@ -14,7 +14,7 @@
* limitations under the License. * 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.api.exception.NacosException;
import com.alibaba.nacos.common.http.AbstractHttpClientFactory; import com.alibaba.nacos.common.http.AbstractHttpClientFactory;

View File

@ -14,7 +14,7 @@
* limitations under the License. * 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.PropertyKeyConst;
import com.alibaba.nacos.api.SystemPropertyKeyConst; 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.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView; import com.alibaba.nacos.api.naming.pojo.ListView;
import com.alibaba.nacos.api.naming.pojo.Service; 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.AbstractSelector;
import com.alibaba.nacos.api.selector.ExpressionSelector; import com.alibaba.nacos.api.selector.ExpressionSelector;
import com.alibaba.nacos.api.selector.SelectorType; import com.alibaba.nacos.api.selector.SelectorType;
import com.alibaba.nacos.client.config.impl.SpasAdapter; import com.alibaba.nacos.client.config.impl.SpasAdapter;
import com.alibaba.nacos.client.monitor.MetricsMonitor; import com.alibaba.nacos.client.monitor.MetricsMonitor;
import com.alibaba.nacos.client.naming.beat.BeatInfo; 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.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.NetUtils; import com.alibaba.nacos.client.naming.utils.NetUtils;
import com.alibaba.nacos.client.naming.utils.SignUtil; 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.client.NacosRestTemplate;
import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.http.param.Query; 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.HttpMethod;
import com.alibaba.nacos.common.utils.IoUtils; import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.JacksonUtils;
@ -78,7 +80,7 @@ import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER;
* *
* @author nkorange * @author nkorange
*/ */
public class NamingProxy implements Closeable { public class NamingHttpClientProxy implements NamingClientProxy {
private final NacosRestTemplate nacosRestTemplate = NamingHttpClientManager.getInstance().getNacosRestTemplate(); private final NacosRestTemplate nacosRestTemplate = NamingHttpClientManager.getInstance().getNacosRestTemplate();
@ -108,7 +110,7 @@ public class NamingProxy implements Closeable {
private ScheduledExecutorService executorService; 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.securityProxy = new SecurityProxy(properties, nacosRestTemplate);
this.properties = properties; this.properties = properties;
@ -211,14 +213,7 @@ public class NamingProxy implements Closeable {
} }
} }
/** @Override
* 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 { public void registerService(String serviceName, String groupName, Instance instance) throws NacosException {
NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance: {}", namespaceId, serviceName, NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance: {}", namespaceId, serviceName,
@ -241,22 +236,15 @@ public class NamingProxy implements Closeable {
} }
/** @Override
* deregister instance from a service. public void deregisterService(String serviceName, String groupName, Instance instance) throws NacosException {
*
* @param serviceName name of service
* @param instance instance
* @throws NacosException nacos exception
*/
public void deregisterService(String serviceName, Instance instance) throws NacosException {
NAMING_LOGGER NAMING_LOGGER
.info("[DEREGISTER-SERVICE] {} deregistering service {} with instance: {}", namespaceId, serviceName, .info("[DEREGISTER-SERVICE] {} deregistering service {} with instance: {}", namespaceId, serviceName,
instance); instance);
final Map<String, String> params = new HashMap<String, String>(8); final Map<String, String> params = new HashMap<String, String>(8);
params.put(CommonParams.NAMESPACE_ID, namespaceId); 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(CommonParams.CLUSTER_NAME, instance.getClusterName());
params.put("ip", instance.getIp()); params.put("ip", instance.getIp());
params.put("port", String.valueOf(instance.getPort())); params.put("port", String.valueOf(instance.getPort()));
@ -265,14 +253,7 @@ public class NamingProxy implements Closeable {
reqApi(UtilAndComs.nacosUrlInstance, params, HttpMethod.DELETE); reqApi(UtilAndComs.nacosUrlInstance, params, HttpMethod.DELETE);
} }
/** @Override
* Update instance to service.
*
* @param serviceName service name
* @param groupName group name
* @param instance instance
* @throws NacosException nacos exception
*/
public void updateInstance(String serviceName, String groupName, Instance instance) throws NacosException { public void updateInstance(String serviceName, String groupName, Instance instance) throws NacosException {
NAMING_LOGGER NAMING_LOGGER
.info("[UPDATE-SERVICE] {} update service {} with instance: {}", namespaceId, serviceName, instance); .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); reqApi(UtilAndComs.nacosUrlInstance, params, HttpMethod.PUT);
} }
/** @Override
* Query Service. public ServiceInfo queryInstancesOfService(String serviceName, String groupName, String clusters, int udpPort, boolean healthyOnly)
* throws NacosException {
* @param serviceName service name final Map<String, String> params = new HashMap<String, String>(8);
* @param groupName group name params.put(CommonParams.NAMESPACE_ID, namespaceId);
* @return service params.put(CommonParams.SERVICE_NAME, serviceName);
* @throws NacosException nacos exception 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 { public Service queryService(String serviceName, String groupName) throws NacosException {
NAMING_LOGGER.info("[QUERY-SERVICE] {} query service : {}, {}", namespaceId, serviceName, groupName); 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); return JacksonUtils.toObj(result, Service.class);
} }
/** @Override
* Create service.
*
* @param service service
* @param selector selector
* @throws NacosException nacos exception
*/
public void createService(Service service, AbstractSelector selector) throws NacosException { public void createService(Service service, AbstractSelector selector) throws NacosException {
NAMING_LOGGER.info("[CREATE-SERVICE] {} creating service : {}", namespaceId, service); NAMING_LOGGER.info("[CREATE-SERVICE] {} creating service : {}", namespaceId, service);
@ -335,14 +320,7 @@ public class NamingProxy implements Closeable {
} }
/** @Override
* Delete service.
*
* @param serviceName service name
* @param groupName group name
* @return true if delete ok
* @throws NacosException nacos exception
*/
public boolean deleteService(String serviceName, String groupName) throws NacosException { public boolean deleteService(String serviceName, String groupName) throws NacosException {
NAMING_LOGGER.info("[DELETE-SERVICE] {} deleting service : {} with groupName : {}", namespaceId, serviceName, NAMING_LOGGER.info("[DELETE-SERVICE] {} deleting service : {} with groupName : {}", namespaceId, serviceName,
groupName); groupName);
@ -356,13 +334,7 @@ public class NamingProxy implements Closeable {
return "ok".equals(result); return "ok".equals(result);
} }
/** @Override
* Update service.
*
* @param service service
* @param selector selector
* @throws NacosException nacos exception
*/
public void updateService(Service service, AbstractSelector selector) throws NacosException { public void updateService(Service service, AbstractSelector selector) throws NacosException {
NAMING_LOGGER.info("[UPDATE-SERVICE] {} updating service : {}", namespaceId, service); NAMING_LOGGER.info("[UPDATE-SERVICE] {} updating service : {}", namespaceId, service);
@ -450,6 +422,7 @@ public class NamingProxy implements Closeable {
return getServiceList(pageNo, pageSize, groupName, null); return getServiceList(pageNo, pageSize, groupName, null);
} }
@Override
public ListView<String> getServiceList(int pageNo, int pageSize, String groupName, AbstractSelector selector) public ListView<String> getServiceList(int pageNo, int pageSize, String groupName, AbstractSelector selector)
throws NacosException { throws NacosException {
@ -483,6 +456,16 @@ public class NamingProxy implements Closeable {
return listView; 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<String, String> params, String method) throws NacosException { public String reqApi(String api, Map<String, String> params, String method) throws NacosException {
return reqApi(api, params, Collections.EMPTY_MAP, method); return reqApi(api, params, Collections.EMPTY_MAP, method);
} }

View File

@ -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.request.Request;
import com.alibaba.nacos.api.remote.response.Response; import com.alibaba.nacos.api.remote.response.Response;
import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.common.lifecycle.Closeable;
import org.slf4j.Logger; import org.slf4j.Logger;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
@ -35,7 +36,7 @@ import java.util.function.Consumer;
* @author liuzunfei * @author liuzunfei
* @version $Id: RpcClient.java, v 0.1 2020年07月13日 9:15 PM liuzunfei Exp $ * @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); private static final Logger LOGGER = LogUtils.logger(RpcClient.class);
@ -181,5 +182,4 @@ public abstract class RpcClient {
public ServerListFactory getServerListFactory() { public ServerListFactory getServerListFactory() {
return serverListFactory; return serverListFactory;
} }
} }

View File

@ -347,4 +347,10 @@ public class GrpcClient extends RpcClient {
} }
} }
@Override
public void shutdown() throws NacosException {
if (!this.channel.isShutdown()) {
this.channel.shutdownNow();
}
}
} }

View File

@ -19,7 +19,7 @@ package com.alibaba.nacos.client;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.naming.beat.BeatInfo; import com.alibaba.nacos.client.naming.beat.BeatInfo;
import com.alibaba.nacos.client.naming.beat.BeatReactor; 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.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -34,7 +34,7 @@ import java.util.concurrent.ScheduledThreadPoolExecutor;
public class BeatReactorTest { public class BeatReactorTest {
@Mock @Mock
private NamingProxy namingProxy; private NamingHttpClientProxy namingHttpClientProxy;
@Test @Test
public void test() throws NoSuchFieldException, IllegalAccessException, InterruptedException, NacosException { public void test() throws NoSuchFieldException, IllegalAccessException, InterruptedException, NacosException {
@ -48,7 +48,7 @@ public class BeatReactorTest {
beatInfo.setScheduled(false); beatInfo.setScheduled(false);
beatInfo.setPeriod(1000L); beatInfo.setPeriod(1000L);
BeatReactor beatReactor = new BeatReactor(namingProxy); BeatReactor beatReactor = new BeatReactor(namingHttpClientProxy);
beatReactor.addBeatInfo("testService", beatInfo); beatReactor.addBeatInfo("testService", beatInfo);
Assert.assertEquals(1, getActiveThread(beatReactor)); Assert.assertEquals(1, getActiveThread(beatReactor));

View File

@ -21,7 +21,7 @@ import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.client.naming.beat.BeatInfo; import com.alibaba.nacos.client.naming.beat.BeatInfo;
import com.alibaba.nacos.client.naming.beat.BeatReactor; 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.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -42,7 +42,7 @@ public class HostReactorTest {
private static final String CACHE_DIR = HostReactorTest.class.getResource("/").getPath() + "cache/"; private static final String CACHE_DIR = HostReactorTest.class.getResource("/").getPath() + "cache/";
@Mock @Mock
private NamingProxy namingProxy; private NamingHttpClientProxy namingHttpClientProxy;
@Mock @Mock
private EventDispatcher eventDispatcher; private EventDispatcher eventDispatcher;
@ -53,7 +53,7 @@ public class HostReactorTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
beatReactor = new BeatReactor(namingProxy); beatReactor = new BeatReactor(namingHttpClientProxy);
BeatInfo beatInfo = new BeatInfo(); BeatInfo beatInfo = new BeatInfo();
beatInfo.setServiceName("testName"); beatInfo.setServiceName("testName");
beatInfo.setIp("1.1.1.1"); beatInfo.setIp("1.1.1.1");
@ -64,7 +64,7 @@ public class HostReactorTest {
beatInfo.setScheduled(false); beatInfo.setScheduled(false);
beatInfo.setPeriod(1000L); beatInfo.setPeriod(1000L);
beatReactor.addBeatInfo("testName", beatInfo); beatReactor.addBeatInfo("testName", beatInfo);
hostReactor = new HostReactor(eventDispatcher, namingProxy, beatReactor, CACHE_DIR); hostReactor = new HostReactor(eventDispatcher, namingHttpClientProxy, beatReactor, CACHE_DIR);
} }
@Test @Test
@ -78,7 +78,7 @@ public class HostReactorTest {
@Test @Test
public void testGetServiceInfoDirectlyFromServer() throws NacosException { 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"); ServiceInfo actual = hostReactor.getServiceInfoDirectlyFromServer("testName", "testClusters");
assertServiceInfo(actual); assertServiceInfo(actual);
} }

View File

@ -16,7 +16,7 @@
package com.alibaba.nacos.test.naming; package com.alibaba.nacos.test.naming;
import com.alibaba.nacos.api.naming.pojo.Instance; 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.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.http.HttpRestResult; import com.alibaba.nacos.common.http.HttpRestResult;
import com.alibaba.nacos.common.http.client.NacosRestTemplate; import com.alibaba.nacos.common.http.client.NacosRestTemplate;

View File

@ -46,7 +46,7 @@ import java.util.concurrent.TimeUnit;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"}, @SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class NamingProxy_ITCase { public class NamingHttpClientProxy_ITCase {
@LocalServerPort @LocalServerPort
private int port; private int port;
@Autowired @Autowired