Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #12189] Refactor Client SDK. #12432

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
2 changes: 2 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/PropertyKeyConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class PropertyKeyConst {
public static final String ENDPOINT_CLUSTER_NAME = "endpointClusterName";

public static final String SERVER_NAME = "serverName";

public static final String MODULE_NAME = "moduleName";

public static final String NAMESPACE = "namespace";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
import com.alibaba.nacos.client.config.impl.ClientWorker;
import com.alibaba.nacos.client.config.impl.LocalConfigInfoProcessor;
import com.alibaba.nacos.client.config.impl.LocalEncryptedDataKeyProcessor;
import com.alibaba.nacos.client.config.impl.ServerListManager;
import com.alibaba.nacos.client.config.utils.ContentUtils;
import com.alibaba.nacos.client.config.utils.ParamUtils;
import com.alibaba.nacos.client.serverlist.ServerListManager;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
Expand Down Expand Up @@ -81,9 +81,10 @@ public NacosConfigService(Properties properties) throws NacosException {

initNamespace(clientProperties);
this.configFilterChainManager = new ConfigFilterChainManager(clientProperties.asProperties());
ServerListManager serverListManager = new ServerListManager(clientProperties);
serverListManager.start();

properties.setProperty(PropertyKeyConst.MODULE_NAME, Constants.Config.CONFIG_MODULE);
ServerListManager serverListManager = new ServerListManager(properties);
serverListManager.refreshCurrentServerAddr();

this.worker = new ClientWorker(this.configFilterChainManager, serverListManager, clientProperties);
// will be deleted in 2.0 later versions
agent = new ServerHttpAgent(serverListManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.impl.ConfigHttpClientManager;
import com.alibaba.nacos.client.config.impl.ServerListManager;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.client.serverlist.ServerListManager;
import com.alibaba.nacos.client.utils.ContextPathUtil;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
Expand All @@ -29,6 +28,7 @@
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.http.param.Query;
import com.alibaba.nacos.common.utils.ExceptionUtil;
import com.alibaba.nacos.common.utils.HttpMethod;
import org.slf4j.Logger;

import java.net.ConnectException;
Expand Down Expand Up @@ -58,7 +58,7 @@ public class ServerHttpAgent implements HttpAgent {
public HttpRestResult<String> httpGet(String path, Map<String, String> headers, Map<String, String> paramValues,
String encode, long readTimeoutMs) throws Exception {
final long endTime = System.currentTimeMillis() + readTimeoutMs;
String currentServerAddr = serverListMgr.getCurrentServerAddr();
String currentServerAddr = serverListMgr.getCurrentServer();
int maxRetry = this.maxRetry;
HttpClientConfig httpConfig = HttpClientConfig.builder()
.setReadTimeOutMillis(Long.valueOf(readTimeoutMs).intValue())
Expand All @@ -74,35 +74,24 @@ public HttpRestResult<String> httpGet(String path, Map<String, String> headers,
newHeaders, query, String.class);
if (isFail(result)) {
LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}",
serverListMgr.getCurrentServerAddr(), result.getCode());
} else {
// Update the currently available server addr
serverListMgr.updateCurrentServerAddr(currentServerAddr);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个逻辑是配置中心为了保持一个访问同一只可访问的server而保持的逻辑, 我看你下面的acquireNextServer并没有保留这个逻辑,但是又移除了, 可能有点问题。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个逻辑是配置中心为了保持一个访问同一只可访问的server而保持的逻辑, 我看你下面的acquireNextServer并没有保留这个逻辑,但是又移除了, 可能有点问题。

这里是复用Naming模块的nextServer逻辑基于currentIndex, 在失败重试获取的逻辑里面会递增index, 应该不会导致Server下次访问会变动?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

配置和服务的一致性协议有所区别, 配置中心应该是尽可能保证访问到同一个节点上,注册中心可以随机分配。

因此这个逻辑之前时放在agent里和proxy里,没有放在地址服务器里。

既然这次是重构地址服务器,就不要改动外层agent和proxy的逻辑。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

配置和服务的一致性协议有所区别, 配置中心应该是尽可能保证访问到同一个节点上,注册中心可以随机分配。

因此这个逻辑之前时放在agent里和proxy里,没有放在地址服务器里。

既然这次是重构地址服务器,就不要改动外层agent和proxy的逻辑。

是的, 这里在重试genNextServer()找到可用Server后下标固定还是这个Server, 所以移除了updateCurrentServerAddr(), 注册中心则还是沿用以前的逻辑在构建时随机指定了一个下标。

return result;
serverListMgr.getCurrentServer(), result.getCode());
}
return result;
} catch (ConnectException connectException) {
LOGGER.error("[NACOS ConnectException httpGet] currentServerAddr:{}, err : {}",
serverListMgr.getCurrentServerAddr(), connectException.getMessage());
currentServerAddr, connectException.getMessage());
} catch (SocketTimeoutException socketTimeoutException) {
LOGGER.error("[NACOS SocketTimeoutException httpGet] currentServerAddr:{}, err : {}",
serverListMgr.getCurrentServerAddr(), socketTimeoutException.getMessage());
currentServerAddr, socketTimeoutException.getMessage());
} catch (Exception ex) {
LOGGER.error("[NACOS Exception httpGet] currentServerAddr: " + serverListMgr.getCurrentServerAddr(),
LOGGER.error("[NACOS Exception httpGet] currentServerAddr: " + currentServerAddr,
ex);
throw ex;
}

if (serverListMgr.getIterator().hasNext()) {
currentServerAddr = serverListMgr.getIterator().next();
} else {
maxRetry--;
if (maxRetry < 0) {
throw new ConnectException(
"[NACOS HTTP-GET] The maximum number of tolerable server reconnection errors has been reached");
}
serverListMgr.refreshCurrentServerAddr();
}


currentServerAddr = acquireNextServer(HttpMethod.GET, maxRetry);
maxRetry--;

} while (System.currentTimeMillis() <= endTime);

LOGGER.error("no available server");
Expand All @@ -113,7 +102,7 @@ public HttpRestResult<String> httpGet(String path, Map<String, String> headers,
public HttpRestResult<String> httpPost(String path, Map<String, String> headers, Map<String, String> paramValues,
String encode, long readTimeoutMs) throws Exception {
final long endTime = System.currentTimeMillis() + readTimeoutMs;
String currentServerAddr = serverListMgr.getCurrentServerAddr();
String currentServerAddr = serverListMgr.getCurrentServer();
int maxRetry = this.maxRetry;
HttpClientConfig httpConfig = HttpClientConfig.builder()
.setReadTimeOutMillis(Long.valueOf(readTimeoutMs).intValue())
Expand All @@ -130,11 +119,8 @@ public HttpRestResult<String> httpPost(String path, Map<String, String> headers,
if (isFail(result)) {
LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}", currentServerAddr,
result.getCode());
} else {
// Update the currently available server addr
serverListMgr.updateCurrentServerAddr(currentServerAddr);
return result;
}
return result;
} catch (ConnectException connectException) {
LOGGER.error("[NACOS ConnectException httpPost] currentServerAddr: {}, err : {}", currentServerAddr,
connectException.getMessage());
Expand All @@ -146,17 +132,9 @@ public HttpRestResult<String> httpPost(String path, Map<String, String> headers,
throw ex;
}

if (serverListMgr.getIterator().hasNext()) {
currentServerAddr = serverListMgr.getIterator().next();
} else {
maxRetry--;
if (maxRetry < 0) {
throw new ConnectException(
"[NACOS HTTP-POST] The maximum number of tolerable server reconnection errors has been reached");
}
serverListMgr.refreshCurrentServerAddr();
}

currentServerAddr = acquireNextServer(HttpMethod.POST, maxRetry);
maxRetry--;

} while (System.currentTimeMillis() <= endTime);

LOGGER.error("no available server, currentServerAddr : {}", currentServerAddr);
Expand All @@ -167,7 +145,7 @@ public HttpRestResult<String> httpPost(String path, Map<String, String> headers,
public HttpRestResult<String> httpDelete(String path, Map<String, String> headers, Map<String, String> paramValues,
String encode, long readTimeoutMs) throws Exception {
final long endTime = System.currentTimeMillis() + readTimeoutMs;
String currentServerAddr = serverListMgr.getCurrentServerAddr();
String currentServerAddr = serverListMgr.getCurrentServer();
int maxRetry = this.maxRetry;
HttpClientConfig httpConfig = HttpClientConfig.builder()
.setReadTimeOutMillis(Long.valueOf(readTimeoutMs).intValue())
Expand All @@ -183,43 +161,46 @@ public HttpRestResult<String> httpDelete(String path, Map<String, String> header
newHeaders, query, String.class);
if (isFail(result)) {
LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}",
serverListMgr.getCurrentServerAddr(), result.getCode());
} else {
// Update the currently available server addr
serverListMgr.updateCurrentServerAddr(currentServerAddr);
return result;
serverListMgr.getCurrentServer(), result.getCode());
}
return result;
} catch (ConnectException connectException) {
LOGGER.error("[NACOS ConnectException httpDelete] currentServerAddr:{}, err : {}",
serverListMgr.getCurrentServerAddr(), ExceptionUtil.getStackTrace(connectException));
currentServerAddr, ExceptionUtil.getStackTrace(connectException));
} catch (SocketTimeoutException stoe) {
LOGGER.error("[NACOS SocketTimeoutException httpDelete] currentServerAddr:{}, err : {}",
serverListMgr.getCurrentServerAddr(), ExceptionUtil.getStackTrace(stoe));
currentServerAddr, ExceptionUtil.getStackTrace(stoe));
} catch (Exception ex) {
LOGGER.error("[NACOS Exception httpDelete] currentServerAddr: " + serverListMgr.getCurrentServerAddr(),
LOGGER.error("[NACOS Exception httpDelete] currentServerAddr: " + currentServerAddr,
ex);
throw ex;
}

if (serverListMgr.getIterator().hasNext()) {
currentServerAddr = serverListMgr.getIterator().next();
} else {
maxRetry--;
if (maxRetry < 0) {
throw new ConnectException(
"[NACOS HTTP-DELETE] The maximum number of tolerable server reconnection errors has been reached");
}
serverListMgr.refreshCurrentServerAddr();
}

currentServerAddr = acquireNextServer(HttpMethod.DELETE, maxRetry);
maxRetry--;

} while (System.currentTimeMillis() <= endTime);

LOGGER.error("no available server");
throw new ConnectException("no available server");
}

private String acquireNextServer(String acquireMethod, int maxRetry) throws ConnectException {
if (serverListMgr.hasNext()) {
return serverListMgr.genNextServer();
}
maxRetry--;
if (maxRetry < 0) {
String exceptionMsg = String.format(
"[NACOS HTTP-%s] The maximum number of tolerable server reconnection errors has been reached", acquireMethod);
throw new ConnectException(exceptionMsg);
}
serverListMgr.refreshCurrentServerAddr();
return serverListMgr.getCurrentServer();
}

private String getUrl(String serverAddr, String relativePath) {
return serverAddr + ContextPathUtil.normalizeContextPath(serverListMgr.getContentPath()) + relativePath;
return serverAddr + ContextPathUtil.normalizeContextPath(serverListMgr.getContextPath()) + relativePath;
}

private boolean isFail(HttpRestResult<String> result) {
Expand All @@ -242,12 +223,12 @@ public ServerHttpAgent(ServerListManager mgr, Properties properties) {
}

public ServerHttpAgent(Properties properties) throws NacosException {
this.serverListMgr = new ServerListManager(NacosClientProperties.PROTOTYPE.derive(properties));
this.serverListMgr = new ServerListManager(properties);
}

@Override
public void start() throws NacosException {
serverListMgr.start();

}

@Override
Expand All @@ -262,7 +243,7 @@ public String getNamespace() {

@Override
public String getTenant() {
return serverListMgr.getTenant();
return serverListMgr.getNamespace();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager;
import com.alibaba.nacos.client.config.filter.impl.ConfigResponse;
import com.alibaba.nacos.client.config.utils.ContentUtils;
import com.alibaba.nacos.client.serverlist.ServerListManager;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.client.env.SourceType;
import com.alibaba.nacos.client.monitor.MetricsMonitor;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.client.serverlist.event.ServerListChangedEvent;
import com.alibaba.nacos.client.utils.AppNameUtils;
import com.alibaba.nacos.client.utils.EnvUtil;
import com.alibaba.nacos.client.utils.LogUtils;
Expand Down Expand Up @@ -527,11 +529,9 @@ Map<String, Object> getMetrics(List<ClientConfigMetricRequest.MetricsKey> metric
metric.put("listenConfigSize", String.valueOf(this.cacheMap.get().size()));
metric.put("clientVersion", VersionUtils.getFullClientVersion());
metric.put("snapshotDir", LocalConfigInfoProcessor.LOCAL_SNAPSHOT_PATH);
boolean isFixServer = agent.serverListManager.isFixed;
metric.put("isFixedServer", isFixServer);
metric.put("addressUrl", agent.serverListManager.addressServerUrl);
metric.put("serverUrls", agent.serverListManager.getUrlString());

metric.put("serverList", agent.serverListManager.getUrlString());
metric.put("serverListHolderStrategy", agent.serverListManager.getServerListHolderStrategy());

Map<ClientConfigMetricRequest.MetricsKey, Object> metricValues = getMetricsValue(metricsKeys);
metric.put("metricValues", metricValues);
Map<String, Object> metrics = new HashMap<>(1);
Expand Down Expand Up @@ -733,36 +733,37 @@ public void onDisConnect(Connection connection) {
}

});

rpcClientInner.serverListFactory(new ServerListFactory() {
@Override
public String genNextServer() {
return ConfigRpcTransportClient.super.serverListManager.getNextServerAddr();
return ConfigRpcTransportClient.super.serverListManager.genNextServer();

}

@Override
public String getCurrentServer() {
return ConfigRpcTransportClient.super.serverListManager.getCurrentServerAddr();
return ConfigRpcTransportClient.super.serverListManager.getCurrentServer();

}

@Override
public List<String> getServerList() {
return ConfigRpcTransportClient.super.serverListManager.getServerUrls();

return ConfigRpcTransportClient.super.serverListManager.getServerList();
}
});

subscriber = new Subscriber() {
@Override
public void onEvent(Event event) {
rpcClientInner.onServerListChange();
ServerListChangedEvent env = (ServerListChangedEvent) event;
if (Constants.Config.CONFIG_MODULE.equals(env.getModule())) {
rpcClientInner.onServerListChange();
}
}

@Override
public Class<? extends Event> subscribeType() {
return ServerListChangeEvent.class;
return ServerListChangedEvent.class;
}
};
NotifyCenter.registerSubscriber(subscriber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.client.serverlist.ServerListManager;
import com.alibaba.nacos.plugin.auth.api.RequestResource;
import com.alibaba.nacos.client.config.filter.impl.ConfigResponse;
import com.alibaba.nacos.client.security.SecurityProxy;
Expand Down Expand Up @@ -79,7 +80,7 @@ public ConfigTransportClient(NacosClientProperties properties, ServerListManager
this.tenant = properties.getProperty(PropertyKeyConst.NAMESPACE);
this.serverListManager = serverListManager;
this.properties = properties.asProperties();
this.securityProxy = new SecurityProxy(serverListManager.getServerUrls(),
this.securityProxy = new SecurityProxy(serverListManager.getServerList(),
ConfigHttpClientManager.getInstance().getNacosRestTemplate());
}

Expand Down

This file was deleted.

Loading