Skip to content

Commit

Permalink
feat(11752): The contentPath of AddressServerUrl is not flexible.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo-Qiu committed Feb 28, 2024
1 parent 139f4f2 commit 271ba2b
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 61 deletions.
4 changes: 4 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 @@ -33,6 +33,8 @@ public class PropertyKeyConst {

public static final String ENDPOINT_PORT = "endpointPort";

public static final String ENDPOINT_CONTEXT_PATH = "endpointContextPath";

public static final String SERVER_NAME = "serverName";

public static final String NAMESPACE = "namespace";
Expand Down Expand Up @@ -94,6 +96,8 @@ public static class SystemEnv {

public static final String ALIBABA_ALIWARE_ENDPOINT_PORT = "ALIBABA_ALIWARE_ENDPOINT_PORT";

public static final String ALIBABA_ALIWARE_ENDPOINT_CONTEXT_PATH = "ALIBABA_ALIWARE_ENDPOINT_CONTEXT_PATH";

public static final String ALIBABA_ALIWARE_NAMESPACE = "ALIBABA_ALIWARE_NAMESPACE";

public static final String ALIBABA_ALIWARE_ENDPOINT_URL = "ALIBABA_ALIWARE_ENDPOINT_URL";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.alibaba.nacos.client.utils.ContextPathUtil;
import com.alibaba.nacos.client.utils.EnvUtil;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.client.utils.TemplateUtils;
import com.alibaba.nacos.common.executor.NameThreadFactory;
import com.alibaba.nacos.common.http.HttpRestResult;
Expand All @@ -31,8 +32,8 @@
import com.alibaba.nacos.common.http.param.Query;
import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.common.utils.InternetAddressUtil;
import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.common.utils.ThreadUtils;
import org.slf4j.Logger;
Expand All @@ -48,7 +49,6 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.alibaba.nacos.client.utils.ParamUtil;

import static com.alibaba.nacos.common.constant.RequestUrlConstants.HTTPS_PREFIX;
import static com.alibaba.nacos.common.constant.RequestUrlConstants.HTTP_PREFIX;
Expand Down Expand Up @@ -82,21 +82,18 @@ public class ServerListManager implements Closeable {

public static final String FIXED_NAME = "fixed";

private final int initServerlistRetryTimes = 5;

/**
* Connection timeout and socket timeout with other servers.
*/
static final int TIMEOUT = 5000;
private final int initServerListRetryTimes = 5;

final boolean isFixed;

boolean isStarted = false;
boolean isStarted;

private String endpoint;

private int endpointPort = 8080;

private String endpointContextPath;

private String contentPath = ParamUtil.getDefaultContextPath();

private String serverListName = ParamUtil.getDefaultNodesPath();
Expand All @@ -107,8 +104,6 @@ public class ServerListManager implements Closeable {

private Iterator<String> iterator;

public String serverPort = ParamUtil.getDefaultServerPort();

public String addressServerUrl;

private String serverAddrsStr;
Expand Down Expand Up @@ -161,34 +156,24 @@ public ServerListManager(String endpoint) throws NacosException {
public ServerListManager(String endpoint, String namespace) throws NacosException {
this.isFixed = false;
this.isStarted = false;
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.ENDPOINT, endpoint);
final NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
this.endpoint = initEndpoint(clientProperties);

if (StringUtils.isBlank(endpoint)) {
throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "endpoint is blank");
}
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.ENDPOINT, endpoint);
final NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
initParam(clientProperties);
if (StringUtils.isNotBlank(namespace)) {
this.namespace = namespace;
this.tenant = namespace;
}

this.name = initServerName(null);
initAddressServerUrl(clientProperties);
}

public ServerListManager(NacosClientProperties properties) throws NacosException {
this.isStarted = false;
this.serverAddrsStr = properties.getProperty(PropertyKeyConst.SERVER_ADDR);
String namespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
initParam(properties);

if (StringUtils.isNotBlank(namespace)) {
this.namespace = namespace;
this.tenant = namespace;
}

if (StringUtils.isNotEmpty(serverAddrsStr)) {
this.isFixed = true;
List<String> serverAddrs = new ArrayList<>();
Expand All @@ -209,7 +194,6 @@ public ServerListManager(NacosClientProperties properties) throws NacosException
}
this.serverUrls = serverAddrs;
this.name = initServerName(properties);

} else {
if (StringUtils.isBlank(endpoint)) {
throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "endpoint is blank");
Expand All @@ -218,7 +202,18 @@ public ServerListManager(NacosClientProperties properties) throws NacosException
this.name = initServerName(properties);
initAddressServerUrl(properties);
}

}

private void initNameSpace(NacosClientProperties properties) {
String namespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
if (StringUtils.isNotBlank(namespace)) {
this.namespace = namespace;
this.tenant = namespace;
}
}

private void initServerAddr(NacosClientProperties properties) {
this.serverAddrsStr = properties.getProperty(PropertyKeyConst.SERVER_ADDR);
}

private String initServerName(NacosClientProperties properties) {
Expand All @@ -227,70 +222,92 @@ private String initServerName(NacosClientProperties properties) {
if (properties != null && properties.containsKey(PropertyKeyConst.SERVER_NAME)) {
serverName = properties.getProperty(PropertyKeyConst.SERVER_NAME);
} else {
// if fix url,use fix url join string.
// if fix url, use fix url join string.
if (isFixed) {
serverName = FIXED_NAME + "-" + (StringUtils.isNotBlank(namespace) ? (StringUtils.trim(namespace) + "-")
: "") + getFixedNameSuffix(serverUrls.toArray(new String[0]));
} else {
//if use endpoint , use endpoint ,content path ,serverlist name
serverName = CUSTOM_NAME + "-" + String
.join("_", endpoint, String.valueOf(endpointPort), contentPath, serverListName) + (
StringUtils.isNotBlank(namespace) ? ("_" + StringUtils.trim(namespace)) : "");
//if use endpoint, use endpoint, content path, serverList name
String contextPathTmp =
StringUtils.isNotBlank(this.endpointContextPath) ? this.endpointContextPath : this.contentPath;
serverName =
CUSTOM_NAME + "-" + String.join("_", endpoint, String.valueOf(endpointPort), contextPathTmp,
serverListName) + (StringUtils.isNotBlank(namespace) ? ("_" + StringUtils.trim(
namespace)) : "");
}
}
serverName = serverName.replaceAll("\\/", "_");
serverName = serverName.replaceAll("\\:", "_");

return serverName;
}

private void initAddressServerUrl(NacosClientProperties properties) {
if (isFixed) {
return;
}
String contextPathTem = StringUtils.isNotBlank(this.endpointContextPath) ? ContextPathUtil.normalizeContextPath(
this.endpointContextPath) : ContextPathUtil.normalizeContextPath(this.contentPath);
StringBuilder addressServerUrlTem = new StringBuilder(
String.format("http://%s:%d%s/%s", this.endpoint, this.endpointPort,
ContextPathUtil.normalizeContextPath(this.contentPath), this.serverListName));
String.format("http://%s:%d%s/%s", this.endpoint, this.endpointPort, contextPathTem,
this.serverListName));
boolean hasQueryString = false;
if (StringUtils.isNotBlank(namespace)) {
addressServerUrlTem.append("?namespace=").append(namespace);
hasQueryString = true;
}
if (properties != null && properties.containsKey(PropertyKeyConst.ENDPOINT_QUERY_PARAMS)) {
addressServerUrlTem
.append(hasQueryString ? "&" : "?" + properties.getProperty(PropertyKeyConst.ENDPOINT_QUERY_PARAMS));
addressServerUrlTem.append(
hasQueryString ? "&" : "?" + properties.getProperty(PropertyKeyConst.ENDPOINT_QUERY_PARAMS));

}

this.addressServerUrl = addressServerUrlTem.toString();
LOGGER.info("serverName = {}, address server url = {}", this.name, this.addressServerUrl);
}

private void initParam(NacosClientProperties properties) {
this.endpoint = initEndpoint(properties);

String contentPathTmp = properties.getProperty(PropertyKeyConst.CONTEXT_PATH);
if (!StringUtils.isBlank(contentPathTmp)) {
this.contentPath = contentPathTmp;
initServerAddr(properties);
initNameSpace(properties);
initEndpoint(properties);
initEndpointPort(properties);
initEndpointContextPath(properties);
initContextPath(properties);
initServerListName(properties);
}

private void initEndpointContextPath(NacosClientProperties properties) {
String endpointContextPathTmp = TemplateUtils.stringEmptyAndThenExecute(
properties.getProperty(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_CONTEXT_PATH),
() -> properties.getProperty(PropertyKeyConst.ENDPOINT_CONTEXT_PATH));
if (StringUtils.isNotBlank(endpointContextPathTmp)) {
this.endpointContextPath = endpointContextPathTmp;
}
}

private void initEndpointPort(NacosClientProperties properties) {
String endpointPortTmp = TemplateUtils.stringEmptyAndThenExecute(
properties.getProperty(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT),
() -> properties.getProperty(PropertyKeyConst.ENDPOINT_PORT));
if (StringUtils.isNotBlank(endpointPortTmp)) {
this.endpointPort = Integer.parseInt(endpointPortTmp);
}
}

private void initServerListName(NacosClientProperties properties) {
String serverListNameTmp = properties.getProperty(PropertyKeyConst.CLUSTER_NAME);
if (!StringUtils.isBlank(serverListNameTmp)) {
this.serverListName = serverListNameTmp;
}
}

private String initEndpoint(final NacosClientProperties properties) {

String endpointPortTmp = TemplateUtils
.stringEmptyAndThenExecute(properties.getProperty(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT),
() -> properties.getProperty(PropertyKeyConst.ENDPOINT_PORT));

if (StringUtils.isNotBlank(endpointPortTmp)) {
this.endpointPort = Integer.parseInt(endpointPortTmp);
private void initContextPath(NacosClientProperties properties) {
String contentPathTmp = properties.getProperty(PropertyKeyConst.CONTEXT_PATH);
if (!StringUtils.isBlank(contentPathTmp)) {
this.contentPath = contentPathTmp;
}

}

private void initEndpoint(final NacosClientProperties properties) {
String endpointTmp = properties.getProperty(PropertyKeyConst.ENDPOINT);

// Whether to enable domain name resolution rules
String isUseEndpointRuleParsing = properties.getProperty(PropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE,
properties.getProperty(SystemPropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE,
Expand All @@ -300,10 +317,9 @@ private String initEndpoint(final NacosClientProperties properties) {
if (StringUtils.isNotBlank(endpointUrl)) {
this.serverAddrsStr = "";
}
return endpointUrl;
this.endpoint = endpointUrl;
}

return StringUtils.isNotBlank(endpointTmp) ? endpointTmp : "";
this.endpoint = StringUtils.isNotBlank(endpointTmp) ? endpointTmp : "";
}

/**
Expand All @@ -318,7 +334,7 @@ public synchronized void start() throws NacosException {
}

GetServerListTask getServersTask = new GetServerListTask(addressServerUrl);
for (int i = 0; i < initServerlistRetryTimes && serverUrls.isEmpty(); ++i) {
for (int i = 0; i < initServerListRetryTimes && serverUrls.isEmpty(); ++i) {
getServersTask.run();
if (!serverUrls.isEmpty()) {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class ServerListManager implements ServerListFactory, Closeable {

private String endpoint;

private String endpointContentPath;

private String contentPath = ParamUtil.getDefaultContextPath();

private String serverListName = ParamUtil.getDefaultNodesPath();
Expand All @@ -101,7 +103,10 @@ public ServerListManager(NacosClientProperties properties, String namespace) {
private void initServerAddr(NacosClientProperties properties) {
this.endpoint = InitUtils.initEndpoint(properties);
if (StringUtils.isNotEmpty(endpoint)) {

String endpointContentPathTmp = properties.getProperty(PropertyKeyConst.ENDPOINT_CONTEXT_PATH);
if (!StringUtils.isBlank(endpointContentPathTmp)) {
this.endpointContentPath = endpointContentPathTmp;
}
String contentPathTmp = properties.getProperty(PropertyKeyConst.CONTEXT_PATH);
if (!StringUtils.isBlank(contentPathTmp)) {
this.contentPath = contentPathTmp;
Expand Down Expand Up @@ -130,9 +135,13 @@ private void initServerAddr(NacosClientProperties properties) {

private List<String> getServerListFromEndpoint() {
try {
StringBuilder addressServerUrlTem = new StringBuilder(String.format("http://%s%s/%s", this.endpoint,
ContextPathUtil.normalizeContextPath(this.contentPath), this.serverListName));
String urlString = addressServerUrlTem.toString();
String contentPathTmp;
if (StringUtils.isNotBlank(this.endpointContentPath)) {
contentPathTmp = ContextPathUtil.normalizeContextPath(this.endpointContentPath);
} else {
contentPathTmp = ContextPathUtil.normalizeContextPath(this.contentPath);
}
String urlString = String.format("http://%s%s/%s", this.endpoint, contentPathTmp, this.serverListName);
Header header = NamingHttpUtil.builderHeader();
Query query = StringUtils.isNotBlank(namespace) ? Query.newInstance().addParam("namespace", namespace)
: Query.EMPTY;
Expand Down
Loading

0 comments on commit 271ba2b

Please sign in to comment.