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

Conversation

XiaZhouxx
Copy link
Contributor

Please do not create a Pull Request without creating an issue first.

What is the purpose of the change

client sdk 寻址逻辑重构.

Brief changelog

XX

Verifying this change

XXXX

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Make sure there is a Github issue filed for the change (usually before you start working on it). Trivial changes like typos do not require a Github issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Format the pull request title like [ISSUE #123] Fix UnknownException when host config not exist. Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add integration-test in test module.
  • Run mvn -B clean package apache-rat:check findbugs:findbugs -Dmaven.test.skip=true to make sure basic checks pass. Run mvn clean install -DskipITs to make sure unit-test pass. Run mvn clean test-compile failsafe:integration-test to make sure integration-test pass.

本改动借助了通灵义码进行辅助编程

image image

@CLAassistant
Copy link

CLAassistant commented Jul 27, 2024

CLA assistant check
All committers have signed the CLA.

@XiaZhouxx
Copy link
Contributor Author

第一次尝试提交PR, 希望大佬能够review指点一下, 非常感谢🙏

Copy link
Collaborator

@KomachiSion KomachiSion left a comment

Choose a reason for hiding this comment

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

看起来有不少冲突,您的PR基于的基础分支应该比较老旧了, 建议尽快更新一下。

目前相对其他PR比较好的点:

  1. 保留了请求地址服务器时Header的语意。
  2. 通过ServerListHolder分离了Endpoint和ServerAddr的逻辑。

目前有问题的点:

  1. 配置中心ClientWorker和注册中心的HttpProxy的逻辑改动可能导致和以前的逻辑不一致,有风险
  2. 有些地址服务器相关参数过旧需要更新
  3. 地址服务器逻辑中缺少对queryParameter的支持(可以查看当前的配置中心部分的地址服务器寻址逻辑)

待确认:

  1. 同时传入endpoint和serverAddr, 但从endpoint中获取到了返回码200, 但是为空列表时,是否和现在的逻辑不一致

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(), 注册中心则还是沿用以前的逻辑在构建时随机指定了一个下标。

@@ -24,7 +24,10 @@
* @author onew
*/
public class Constants {

public static final String ENDPOINT_NAME = "endpoint";
Copy link
Collaborator

Choose a reason for hiding this comment

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

为什么不复用com.alibaba.nacos.api.PropertyKeyConst?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

为什么不复用com.alibaba.nacos.api.PropertyKeyConst?

这里是因为觉得有语意问题, 这个常量类都是用作NacosClientProperties在使用的.

if (NAMING_LOGGER.isDebugEnabled()) {
NAMING_LOGGER.debug("request {} failed.", server, e);
}
Random random = new Random();
Copy link
Collaborator

Choose a reason for hiding this comment

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

有个问题, 如果这样修改了, 是否会导致如果只输入一个域名或ip的情况下, 客户端失去了失败重试的能力。

continue;
}
List<String> serverList = delegate.initServerList(properties);
if (CollectionUtils.isNotEmpty(serverList)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

问个问题, 如果我同时输入endpoint,和serverAddr, 但是endpoint的地址返回的是空列表(内容为空列表,状态码为200), 是否和原先的寻址逻辑不符合?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

问个问题, 如果我同时输入endpoint,和serverAddr, 但是endpoint的地址返回的是空列表(内容为空列表,状态码为200), 是否和原先的寻址逻辑不符合?

确实不符合, 我再优化一下, 只考虑重构没有考虑到兼容版本问题

if (!StringUtils.isBlank(contentPathTmp)) {
this.contentPath = contentPathTmp;
}
String serverListNameTmp = properties.getProperty(PropertyKeyConst.CLUSTER_NAME);
Copy link
Collaborator

Choose a reason for hiding this comment

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

参数名已在2.3.3版本修改,请更新最新的代码。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

参数名已在2.3.3版本修改,请更新最新的代码。

非常抱歉, 没有确认分支就开始开发, 我更新再优化一下

if (StringUtils.isNotEmpty(endpoint)) {
this.namespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
this.moduleName = properties.getProperty(PropertyKeyConst.MODULE_NAME);
String contentPathTmp = properties.getProperty(PropertyKeyConst.CONTEXT_PATH);
Copy link
Collaborator

Choose a reason for hiding this comment

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

CONTEXT_PATH的逻辑也有修改,请同步

# Conflicts:
#	client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java
#	client/src/main/java/com/alibaba/nacos/client/naming/core/ServerListManager.java
#	client/src/test/java/com/alibaba/nacos/client/config/NacosConfigServiceTest.java
#	client/src/test/java/com/alibaba/nacos/client/config/http/ServerHttpAgentTest.java
#	client/src/test/java/com/alibaba/nacos/client/config/impl/ClientWorkerTest.java
#	client/src/test/java/com/alibaba/nacos/client/config/impl/ServerListManagerTest.java
#	client/src/test/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientProxyTest.java
#	client/src/test/java/com/alibaba/nacos/client/naming/utils/ServerDiscoveryHttpUtilTest.java
#	client/src/test/java/com/alibaba/nacos/client/serverlist/ServerListManagerTest.java
@XiaZhouxx
Copy link
Contributor Author

更新了一下, 也烦请大佬有空review一下

Copy link
Collaborator

@KomachiSion KomachiSion left a comment

Choose a reason for hiding this comment

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

感觉上问题已经快收敛完了, 再看下评论进行一次修改。

另外有一个问题可以思考下:

moduleName时通过单独的参数传递进去还是方properties里比较好?
我感觉放properties有一点晦涩,直接传进去是不是明显一点。

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.

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

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

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

NacosException exception = new NacosException();

Copy link
Collaborator

Choose a reason for hiding this comment

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

请使用nacos codestyle来reformat code, 不要修改缩进

if (null != refreshServerListExecutor) {
ThreadUtils.shutdownThreadPool(refreshServerListExecutor, NAMING_LOGGER);
}
NamingHttpClientManager.getInstance().shutdown();
Copy link
Collaborator

Choose a reason for hiding this comment

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

地址服务器应该处于更底层的逻辑,依赖NamingHttpClientManager可能不是很合适。
以及日志也打印在NAMING_LOGGER也不是很合适,

日志可以打印到REMOTE_LOGGER里, http访问可以通过NacosRestTemplate来替代。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

地址服务器应该处于更底层的逻辑,依赖NamingHttpClientManager可能不是很合适。 以及日志也打印在NAMING_LOGGER也不是很合适,

日志可以打印到REMOTE_LOGGER里, http访问可以通过NacosRestTemplate来替代。

是的, 作为底层逻辑不应该依赖上层业务 我更正一下

owner = delegate;
return serverList;
}
throw new NacosLoadException("serverList is empty,please check configuration");
Copy link
Collaborator

Choose a reason for hiding this comment

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

可以将NacosServerListHolder对应的class或name包含在异常中。这样提示信息能看到是目前是哪个类型被应用了,但是没获取到服务列表。

# db.password=nacos
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user=root
db.password=118756
Copy link
Collaborator

Choose a reason for hiding this comment

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

本地配置不用一起提交

@XiaZhouxx
Copy link
Contributor Author

感觉上问题已经快收敛完了, 再看下评论进行一次修改。

另外有一个问题可以思考下:

moduleName时通过单独的参数传递进去还是方properties里比较好? 我感觉放properties有一点晦涩,直接传进去是不是明显一点。

确实, 看一下是有点晦涩,这个属性我觉得应该是必要属性要依赖方感知和设置这个属性, 放properties的话貌似还需要去理解上下文这个属性的作用, 假设另一个模块引用的话可能是不知道这个属性的.

@XiaZhouxx
Copy link
Contributor Author

更正了一版, 对于ServerHttpAgent的改动之前有些问题 这里修复了, 配置中心保持一个访问同一只可访问的server的逻辑是保留了的,在genNextServer()内部就已经修改当前服务下标并返回的, 之前是采用的迭代器方式才额外调用了方法修改当前服务. @KomachiSion 也麻烦大佬有空了看一下有什么问题

boolean isFixServer = agent.serverListManager.isFixed;
metric.put("isFixedServer", isFixServer);
metric.put("addressUrl", agent.serverListManager.addressServerUrl);
metric.put("addressUrl", agent.serverListManager.getUrlString());
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个逻辑不一样吧, 之前的这个记录的是地址服务器的信息,现在变成了获取到的地址。

metric.put("serverUrls", agent.serverListManager.getUrlString());

String serverListHolderName = agent.serverListManager.getServerListHolderName();
metric.put("isFixedServer", FixedConfigNacosServerListHolder.NAME.equals(serverListHolderName));
Copy link
Collaborator

Choose a reason for hiding this comment

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

使用方还需要感知类型不太好。

@@ -119,7 +118,7 @@ private void init(Properties properties) throws NacosException {
private void initLogName(NacosClientProperties properties) {
logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME, DEFAULT_NAMING_LOG_FILE_PATH);
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

请勿修改缩进。

@@ -372,7 +370,7 @@ public String reqApi(String api, Map<String, String> params, Map<String, String>
} else {
Random random = new Random();
int index = random.nextInt(servers.size());

Copy link
Collaborator

Choose a reason for hiding this comment

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

缩进问题请全部检查一下

* @param properties nacos client properties
* @return
*/
public static String initContextPath(NacosClientProperties properties) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个方法名最好叫initEndpointContextPath,不然之后的开发者可能会误解。

@KomachiSion
Copy link
Collaborator

KomachiSion commented Aug 5, 2024

@XiaZhouxx 您好, 我收到主办方的消息, 说您的PR没有在活动官网上登记, 这样的话这个PR不算是有效的参赛PR,

方便抽空把报名信息在官网上填写一下。

https://tianchi.aliyun.com/competition/entrance/532215/information?spm=a2c22.12281976.0.0.41ab6c71nLISOm

对填写有疑问也可以发邮件到 [email protected] 询问。

@XiaZhouxx
Copy link
Contributor Author

@XiaZhouxx 您好, 我收到主办方的消息, 说您的PR没有在活动官网上登记, 这样的话这个PR不算是有效的参赛PR,

方便抽空把报名信息在官网上填写一下。

https://tianchi.aliyun.com/competition/entrance/532215/information?spm=a2c22.12281976.0.0.41ab6c71nLISOm

对填写有疑问也可以发邮件到 [email protected] 询问。

收到, 已经填报PR, 代码也重新格式化排版 @KomachiSion 也烦请大佬抽空看一下

Copy link
Collaborator

@KomachiSion KomachiSion left a comment

Choose a reason for hiding this comment

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

目前初步review似乎没有太大的问题了, 之后会在活动截止前对PR进行整体评判后决定是否合入。

我先跑一下CI,请关注一下结果,避免后续需要合入时由于CI问题被阻塞。

@XiaZhouxx
Copy link
Contributor Author

目前初步review似乎没有太大的问题了, 之后会在活动截止前对PR进行整体评判后决定是否合入。

我先跑一下CI,请关注一下结果,避免后续需要合入时由于CI问题被阻塞。

好的, 辛苦了

@XiaZhouxx
Copy link
Contributor Author

@KomachiSion 抱歉大佬有几个文件没有添加licenses, 麻烦再跑下呢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants