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