From 149c50f41704501b11edd414a7197eb609348836 Mon Sep 17 00:00:00 2001 From: huhongjie2014 <139250842+huhongjie2014@users.noreply.github.com> Date: Tue, 25 Jul 2023 09:43:15 +0800 Subject: [PATCH] [ISSUE #10792]When nacos client use endpoint, the registration center should support configuring context-path and cluster-name like the configuration center (#10793) --- .../client/naming/core/ServerListManager.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/ServerListManager.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/ServerListManager.java index 0cab2bd34..3e9fd2837 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/ServerListManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/ServerListManager.java @@ -25,6 +25,8 @@ import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientManager; import com.alibaba.nacos.client.naming.utils.CollectionUtils; import com.alibaba.nacos.client.naming.utils.InitUtils; import com.alibaba.nacos.client.naming.utils.NamingHttpUtil; +import com.alibaba.nacos.client.utils.ContextPathUtil; +import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.common.executor.NameThreadFactory; import com.alibaba.nacos.common.http.HttpRestResult; import com.alibaba.nacos.common.http.client.NacosRestTemplate; @@ -50,7 +52,6 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; -import static com.alibaba.nacos.common.constant.RequestUrlConstants.HTTP_PREFIX; /** * Server list manager. @@ -74,6 +75,10 @@ public class ServerListManager implements ServerListFactory, Closeable { private ScheduledExecutorService refreshServerListExecutor; private String endpoint; + + private String contentPath = ParamUtil.getDefaultContextPath(); + + private String serverListName = ParamUtil.getDefaultNodesPath(); private String nacosDomain; @@ -96,6 +101,16 @@ public class ServerListManager implements ServerListFactory, Closeable { private void initServerAddr(NacosClientProperties properties) { this.endpoint = InitUtils.initEndpoint(properties); if (StringUtils.isNotEmpty(endpoint)) { + + String contentPathTmp = properties.getProperty(PropertyKeyConst.CONTEXT_PATH); + if (!StringUtils.isBlank(contentPathTmp)) { + this.contentPath = contentPathTmp; + } + String serverListNameTmp = properties.getProperty(PropertyKeyConst.CLUSTER_NAME); + if (!StringUtils.isBlank(serverListNameTmp)) { + this.serverListName = serverListNameTmp; + } + this.serversFromEndpoint = getServerListFromEndpoint(); refreshServerListExecutor = new ScheduledThreadPoolExecutor(1, new NameThreadFactory("com.alibaba.nacos.client.naming.server.list.refresher")); @@ -115,7 +130,10 @@ public class ServerListManager implements ServerListFactory, Closeable { private List getServerListFromEndpoint() { try { - String urlString = HTTP_PREFIX + endpoint + "/nacos/serverlist"; + StringBuilder addressServerUrlTem = new StringBuilder( + String.format("http://%s%s/%s", this.endpoint, + ContextPathUtil.normalizeContextPath(this.contentPath), this.serverListName)); + String urlString = addressServerUrlTem.toString(); Header header = NamingHttpUtil.builderHeader(); Query query = StringUtils.isNotBlank(namespace) ? Query.newInstance().addParam("namespace", namespace)