Skip to content
New issue

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

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

Already on GitHub? # to your account

[ISSUE #12189] Refactor Client SDK. #12432

Closed
wants to merge 15 commits into from
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,8 @@ public NacosConfigService(Properties properties) throws NacosException {

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

ServerListManager serverListManager = new ServerListManager(properties, Constants.Config.CONFIG_MODULE);

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 @@ -16,10 +16,10 @@

package com.alibaba.nacos.client.config.http;

import com.alibaba.nacos.api.common.Constants;
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 +29,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 All @@ -46,7 +47,7 @@ public class ServerHttpAgent implements HttpAgent {

private static final Logger LOGGER = LogUtils.logger(ServerHttpAgent.class);

private NacosRestTemplate nacosRestTemplate = ConfigHttpClientManager.getInstance().getNacosRestTemplate();
private final NacosRestTemplate nacosRestTemplate = ConfigHttpClientManager.getInstance().getNacosRestTemplate();

private String encode;

Expand All @@ -56,9 +57,9 @@ public class ServerHttpAgent implements HttpAgent {

@Override
public HttpRestResult<String> httpGet(String path, Map<String, String> headers, Map<String, String> paramValues,
String encode, long readTimeoutMs) throws Exception {
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,34 +75,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());
currentServerAddr, 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;
}
} 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);

Expand All @@ -111,9 +102,9 @@ public HttpRestResult<String> httpGet(String path, Map<String, String> headers,

@Override
public HttpRestResult<String> httpPost(String path, Map<String, String> headers, Map<String, String> paramValues,
String encode, long readTimeoutMs) throws Exception {
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 @@ -131,8 +122,6 @@ public HttpRestResult<String> httpPost(String path, Map<String, String> headers,
LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}", currentServerAddr,
result.getCode());
} else {
// Update the currently available server addr
serverListMgr.updateCurrentServerAddr(currentServerAddr);
return result;
}
} catch (ConnectException connectException) {
Expand All @@ -146,16 +135,8 @@ 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);

Expand All @@ -165,9 +146,9 @@ public HttpRestResult<String> httpPost(String path, Map<String, String> headers,

@Override
public HttpRestResult<String> httpDelete(String path, Map<String, String> headers, Map<String, String> paramValues,
String encode, long readTimeoutMs) throws Exception {
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 +164,47 @@ 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());
serverListMgr.getCurrentServer(), result.getCode());
} else {
// Update the currently available server addr
serverListMgr.updateCurrentServerAddr(currentServerAddr);
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 +227,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, Constants.Config.CONFIG_MODULE);
}

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

}

@Override
Expand All @@ -262,7 +247,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,10 +529,11 @@ 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("addressUrl", agent.serverListManager.getAddressUrl());
metric.put("serverUrls", agent.serverListManager.getUrlString());
String serverListHolderName = agent.serverListManager.getServerListHolderName();
metric.put("isFixedServer", agent.serverListManager.isFixedServer());
metric.put("serverListHolder", serverListHolderName);

Map<ClientConfigMetricRequest.MetricsKey, Object> metricValues = getMetricsValue(metricsKeys);
metric.put("metricValues", metricValues);
Expand Down Expand Up @@ -733,36 +736,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
Loading