* Move and rename NamingProxy.java * rename package naming.net --> naming.remote * Create Naming client proxy interface
This commit is contained in:
parent
85a0398978
commit
39986a867a
@ -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) {
|
||||
|
@ -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<Instance> 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);
|
||||
}
|
||||
|
||||
|
@ -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<String, BeatInfo> dom2Beat = new ConcurrentHashMap<String, BeatInfo>();
|
||||
|
||||
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
|
||||
|
@ -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() {
|
||||
|
@ -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;
|
||||
}
|
@ -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<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())) {
|
||||
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();
|
||||
}
|
||||
}
|
@ -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;
|
@ -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;
|
@ -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;
|
@ -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,22 +236,15 @@ 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<String, String> params = new HashMap<String, String>(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()));
|
||||
@ -265,14 +253,7 @@ public class NamingProxy implements Closeable {
|
||||
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<String, String> params = new HashMap<String, String>(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<String> 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<String, String> params, String method) throws NacosException {
|
||||
return reqApi(api, params, Collections.EMPTY_MAP, method);
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -347,4 +347,10 @@ public class GrpcClient extends RpcClient {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() throws NacosException {
|
||||
if (!this.channel.isShutdown()) {
|
||||
this.channel.shutdownNow();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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
|
Loading…
Reference in New Issue
Block a user