提交 eca7a9a4 编写于 作者: 得少

support parsing endpoint rule

上级 acaee3ff
## 1.0.0-RC2(Mar 22, 2019)
## 1.0.0-RC4(Mar 22, 2019)
* [#923] Nacos 1.0.0 compatible with nacos-client 0.6.2
* [#938] Client beat processor task lost
* [#946] Change default server mode to AP
......
......@@ -16,7 +16,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC4</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -22,6 +22,8 @@ package com.alibaba.nacos.api;
*/
public class PropertyKeyConst {
public final static String IS_USE_ENDPOINT_PARSING_RULE = "isUseEndpointParsingRule";
public final static String ENDPOINT = "endpoint";
public final static String ENDPOINT_PORT = "endpointPort";
......
......@@ -16,7 +16,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC4</version>
<relativePath>../pom.xml</relativePath>
</parent>
......@@ -113,6 +113,7 @@
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
......
......@@ -82,7 +82,7 @@ public class NacosConfigService implements ConfigService {
}
private void initNamespace(Properties properties) {
String namespaceTmp = properties.getProperty(PropertyKeyConst.NAMESPACE);
String namespaceTmp = null;
namespaceTmp = TemplateUtils.stringBlankAndThenExecute(namespaceTmp, new Callable<String>() {
@Override
......@@ -98,6 +98,10 @@ public class NacosConfigService implements ConfigService {
return StringUtils.isNotBlank(namespace) ? namespace : "";
}
});
if (StringUtils.isBlank(namespaceTmp)) {
namespaceTmp = properties.getProperty(PropertyKeyConst.NAMESPACE);
}
namespace = namespaceTmp;
properties.put(PropertyKeyConst.NAMESPACE, namespace);
}
......
......@@ -20,14 +20,16 @@ import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.impl.EventDispatcher.ServerlistChangeEvent;
import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult;
import com.alibaba.nacos.client.config.utils.IOUtils;
import com.alibaba.nacos.client.utils.*;
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.StringUtils;
import org.slf4j.Logger;
import java.io.IOException;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
/**
......@@ -85,7 +87,9 @@ public class ServerListManager {
public ServerListManager(String endpoint, String namespace) throws NacosException {
isFixed = false;
isStarted = false;
endpoint = initEndpoint(endpoint);
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.ENDPOINT, endpoint);
endpoint = initEndpoint(properties);
if (StringUtils.isBlank(endpoint)) {
throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "endpoint is blank");
......@@ -108,7 +112,7 @@ public class ServerListManager {
public ServerListManager(Properties properties) throws NacosException {
isStarted = false;
String serverAddrsStr = properties.getProperty(PropertyKeyConst.SERVER_ADDR);
serverAddrsStr = properties.getProperty(PropertyKeyConst.SERVER_ADDR);
String namespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
initParam(properties);
if (StringUtils.isNotEmpty(serverAddrsStr)) {
......@@ -152,7 +156,7 @@ public class ServerListManager {
}
private void initParam(Properties properties) {
endpoint = initEndpoint(properties.getProperty(PropertyKeyConst.ENDPOINT));
endpoint = initEndpoint(properties);
String contentPathTmp = properties.getProperty(PropertyKeyConst.CONTEXT_PATH);
if (!StringUtils.isBlank(contentPathTmp)) {
......@@ -164,19 +168,22 @@ public class ServerListManager {
}
}
private String initEndpoint(String endpointTmp) {
private String initEndpoint(Properties properties) {
String endpointPortTmp = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT);
if (StringUtils.isNotBlank(endpointPortTmp)) {
endpointPort = Integer.parseInt(endpointPortTmp);
}
return TemplateUtils.stringBlankAndThenExecute(endpointTmp, new Callable<String>() {
@Override
public String call() {
String endpointUrl = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_URL);
return StringUtils.isNotBlank(endpointUrl) ? endpointUrl : "";
String endpointTmp = properties.getProperty(PropertyKeyConst.ENDPOINT);
if (Boolean.valueOf(properties.getProperty(PropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE, ParamUtil.USE_ENDPOINT_PARSING_RULE_DEFAULT_VALUE))) {
String endpointUrl = ParamUtil.parsingEndpointRule(endpointPortTmp);
if (StringUtils.isNotBlank(endpointUrl)) {
serverAddrsStr = "";
}
});
return endpointUrl;
}
return StringUtils.isNotBlank(endpointTmp) ? endpointTmp : "";
}
public synchronized void start() throws NacosException {
......@@ -368,6 +375,7 @@ public class ServerListManager {
public String addressServerUrl;
private String serverAddrsStr;
}
/**
......
......@@ -37,6 +37,7 @@ import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.StringUtils;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.client.utils.TemplateUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.math.NumberUtils;
......@@ -86,9 +87,7 @@ public class NacosNamingService implements NamingService {
}
private void init(Properties properties) {
serverList = properties.getProperty(PropertyKeyConst.SERVER_ADDR);
initNamespace(properties);
initEndpoint(properties);
initWebRootContext();
......@@ -160,13 +159,17 @@ public class NacosNamingService implements NamingService {
return;
}
String endpointUrl = TemplateUtils.stringEmptyAndThenExecute(properties.getProperty(PropertyKeyConst.ENDPOINT), new Callable<String>() {
@Override
public String call() {
return System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_URL);
//这里通过 dubbo/sca 侧来初始化默认传入的是 true
boolean isUseEndpointParsingRule = Boolean.valueOf(properties.getProperty(PropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE, ParamUtil.USE_ENDPOINT_PARSING_RULE_DEFAULT_VALUE));
String endpointUrl;
if (isUseEndpointParsingRule) {
endpointUrl = ParamUtil.parsingEndpointRule(properties.getProperty(PropertyKeyConst.ENDPOINT));
if (com.alibaba.nacos.client.utils.StringUtils.isNotBlank(endpointUrl)) {
serverList = "";
}
});
} else {
endpointUrl = properties.getProperty(PropertyKeyConst.ENDPOINT);
}
if (com.alibaba.nacos.client.utils.StringUtils.isBlank(endpointUrl)) {
return;
......@@ -179,6 +182,7 @@ public class NacosNamingService implements NamingService {
return System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT);
}
});
endpointPort = TemplateUtils.stringEmptyAndThenExecute(endpointPort, new Callable<String>() {
@Override
public String call() {
......@@ -192,10 +196,6 @@ public class NacosNamingService implements NamingService {
private void initNamespace(Properties properties) {
String tmpNamespace = null;
if (properties != null) {
tmpNamespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
}
tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable<String>() {
@Override
public String call() {
......@@ -224,6 +224,10 @@ public class NacosNamingService implements NamingService {
}
});
if (StringUtils.isEmpty(tmpNamespace) && properties != null) {
tmpNamespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
}
tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable<String>() {
@Override
public String call() {
......
......@@ -15,11 +15,13 @@
*/
package com.alibaba.nacos.client.utils;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.client.config.impl.HttpSimpleClient;
import org.slf4j.Logger;
import java.io.InputStream;
import java.util.Properties;
import java.util.regex.Pattern;
/**
* manage param tool
......@@ -27,8 +29,12 @@ import java.util.Properties;
* @author nacos
*/
public class ParamUtil {
private final static Logger LOGGER = LogUtils.logger(ParamUtil.class);
public final static String USE_ENDPOINT_PARSING_RULE_DEFAULT_VALUE = "false";
private static final Pattern PATTERN = Pattern.compile("\\$\\{[^}]+\\}");
private static String defaultContextPath = "nacos";
private static String defaultNodesPath = "serverlist";
private static String appKey;
......@@ -143,4 +149,44 @@ public class ParamUtil {
ParamUtil.defaultNodesPath = defaultNodesPath;
}
public static String parsingEndpointRule(String endpointUrl) {
// 配置文件中输入的话,以 ENV 中的优先,
if (endpointUrl == null
|| !PATTERN.matcher(endpointUrl).find()) {
// skip retrieve from system property and retrieve directly from system env
String endpointUrlSource = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_URL);
if (com.alibaba.nacos.client.utils.StringUtils.isNotBlank(endpointUrlSource)) {
endpointUrl = endpointUrlSource;
}
return StringUtils.isNotBlank(endpointUrl) ? endpointUrl : "";
}
endpointUrl = endpointUrl.substring(endpointUrl.indexOf("${") + 2,
endpointUrl.lastIndexOf("}"));
int defStartOf = endpointUrl.indexOf(":");
String defaultEndpointUrl = null;
if (defStartOf != -1) {
defaultEndpointUrl = endpointUrl.substring(defStartOf + 1);
endpointUrl = endpointUrl.substring(0, defStartOf);
}
String endpointUrlSource = System.getProperty(endpointUrl,
System.getenv(endpointUrl));
if (com.alibaba.nacos.client.utils.StringUtils.isBlank(endpointUrlSource)) {
endpointUrlSource = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_URL);
}
if (com.alibaba.nacos.client.utils.StringUtils.isBlank(endpointUrlSource)) {
if (com.alibaba.nacos.client.utils.StringUtils.isNotBlank(defaultEndpointUrl)) {
endpointUrl = defaultEndpointUrl;
}
} else {
endpointUrl = endpointUrlSource;
}
return StringUtils.isNotBlank(endpointUrl) ? endpointUrl : "";
}
}
......@@ -18,7 +18,7 @@
<parent>
<artifactId>nacos-all</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC4</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC4</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -17,7 +17,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC4</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC4</version>
</parent>
<artifactId>nacos-console</artifactId>
<!--<packaging>war</packaging>-->
......
......@@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC4</version>
<relativePath>../pom.xml</relativePath>
</parent>
......
......@@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC4</version>
<relativePath>../pom.xml</relativePath>
</parent>
......
......@@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC4</version>
<relativePath>../pom.xml</relativePath>
</parent>
......
......@@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC4</version>
<relativePath>../pom.xml</relativePath>
</parent>
......
......@@ -21,7 +21,7 @@
<inceptionYear>2018</inceptionYear>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC4</version>
<packaging>pom</packaging>
<name>Alibaba NACOS ${project.version}</name>
......
......@@ -17,7 +17,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC4</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册