Skip to content

Commit

Permalink
Merge pull request #1044 from alibaba/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Fury Zhu authored Apr 10, 2019
2 parents 20a6a3b + 3b470fa commit 3041f0a
Show file tree
Hide file tree
Showing 19 changed files with 136 additions and 42 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 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
Expand Down
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC3</version>
<version>1.0.0</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 2 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 @@ -22,6 +22,8 @@
*/
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";
Expand Down
3 changes: 2 additions & 1 deletion client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC3</version>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -113,6 +113,7 @@
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public NacosConfigService(Properties properties) throws NacosException {
}

private void initNamespace(Properties properties) {
String namespaceTmp = properties.getProperty(PropertyKeyConst.NAMESPACE);
String namespaceTmp = null;

namespaceTmp = TemplateUtils.stringBlankAndThenExecute(namespaceTmp, new Callable<String>() {
@Override
Expand All @@ -98,6 +98,10 @@ public String call() {
return StringUtils.isNotBlank(namespace) ? namespace : "";
}
});

if (StringUtils.isBlank(namespaceTmp)) {
namespaceTmp = properties.getProperty(PropertyKeyConst.NAMESPACE);
}
namespace = namespaceTmp;
properties.put(PropertyKeyConst.NAMESPACE, namespace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ public ServerListManager(String endpoint) throws NacosException {
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");
Expand All @@ -108,7 +110,7 @@ public ServerListManager(String endpoint, String namespace) throws NacosExceptio

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)) {
Expand Down Expand Up @@ -152,7 +154,7 @@ public ServerListManager(Properties properties) throws NacosException {
}

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)) {
Expand All @@ -164,19 +166,29 @@ private void initParam(Properties properties) {
}
}

private String initEndpoint(String endpointTmp) {
String endpointPortTmp = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT);
if (StringUtils.isNotBlank(endpointPortTmp)) {
endpointPort = Integer.parseInt(endpointPortTmp);
}
private String initEndpoint(final Properties properties) {

return TemplateUtils.stringBlankAndThenExecute(endpointTmp, new Callable<String>() {
String endpointPortTmp = TemplateUtils.stringEmptyAndThenExecute(System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT), new Callable<String>() {
@Override
public String call() {
String endpointUrl = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_URL);
return StringUtils.isNotBlank(endpointUrl) ? endpointUrl : "";
return properties.getProperty(PropertyKeyConst.ENDPOINT_PORT);
}
});

if (StringUtils.isNotBlank(endpointPortTmp)) {
endpointPort = Integer.parseInt(endpointPortTmp);
}

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 {
Expand Down Expand Up @@ -368,6 +380,7 @@ public String getTenant() {

public String addressServerUrl;

private String serverAddrsStr;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
import com.alibaba.nacos.client.naming.core.HostReactor;
import com.alibaba.nacos.client.naming.net.NamingProxy;
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.StringUtils;
import com.alibaba.nacos.client.utils.TemplateUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.math.NumberUtils;
Expand Down Expand Up @@ -86,9 +87,7 @@ public NacosNamingService(Properties properties) {
}

private void init(Properties properties) {

serverList = properties.getProperty(PropertyKeyConst.SERVER_ADDR);

initNamespace(properties);
initEndpoint(properties);
initWebRootContext();
Expand Down Expand Up @@ -150,30 +149,35 @@ private void initCacheDir() {
}
}

private void initEndpoint(Properties properties) {
private void initEndpoint(final Properties properties) {
if (properties == null) {

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)) {
if (StringUtils.isBlank(endpointUrl)) {
return;
}

String endpointPort = TemplateUtils.stringEmptyAndThenExecute(properties.getProperty(PropertyKeyConst.ENDPOINT_PORT), new Callable<String>() {
String endpointPort = TemplateUtils.stringEmptyAndThenExecute(System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT), new Callable<String>() {
@Override
public String call() {

return System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT);
return properties.getProperty(PropertyKeyConst.ENDPOINT_PORT);
}
});

endpointPort = TemplateUtils.stringEmptyAndThenExecute(endpointPort, new Callable<String>() {
@Override
public String call() {
Expand All @@ -187,10 +191,6 @@ public String call() {
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() {
Expand Down Expand Up @@ -219,6 +219,10 @@ public String call() {
}
});

if (StringUtils.isEmpty(tmpNamespace) && properties != null) {
tmpNamespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
}

tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable<String>() {
@Override
public String call() {
Expand Down
49 changes: 49 additions & 0 deletions client/src/main/java/com/alibaba/nacos/client/utils/ParamUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,27 @@
*/
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.concurrent.Callable;
import java.util.regex.Pattern;

/**
* manage param tool
*
* @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;
Expand Down Expand Up @@ -143,4 +150,46 @@ public static void setDefaultNodesPath(String 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 = TemplateUtils.stringBlankAndThenExecute(System.getProperty(endpointUrl,
System.getenv(endpointUrl)), new Callable<String>() {
@Override
public String call() {
return 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 : "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.alibaba.nacos.client.utils;

import java.util.Collection;

/**
* string util
*
Expand Down Expand Up @@ -71,4 +73,23 @@ public static String substringBetween(String str, String open, String close) {
}
return null;
}

public static String join(Collection collection, String separator) {
if (collection == null) {
return null;
}

StringBuilder stringBuilder = new StringBuilder();
Object[] objects = collection.toArray();

for (int i = 0; i < collection.size() - 1; i++) {
stringBuilder.append(objects[i].toString()).append(separator);
}

if (collection.size() > 0) {
stringBuilder.append(objects[collection.size() - 1]);
}

return stringBuilder.toString();
}
}
2 changes: 1 addition & 1 deletion cmdb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<parent>
<artifactId>nacos-all</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>1.0.0-RC3</version>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC3</version>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC3</version>
<version>1.0.0</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion console/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC3</version>
<version>1.0.0</version>
</parent>
<artifactId>nacos-console</artifactId>
<!--<packaging>war</packaging>-->
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC3</version>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC3</version>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC3</version>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Loading

0 comments on commit 3041f0a

Please sign in to comment.