Merge pull request #1307 from pbting/namespace_unified

Namespace And Endpoint Support Multi Enviroment Switch
This commit is contained in:
Fury Zhu 2019-06-04 17:15:26 +08:00 committed by GitHub
commit 9319b76d22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
34 changed files with 291 additions and 280 deletions

View File

@ -16,7 +16,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -22,6 +22,8 @@ package com.alibaba.nacos.api;
*/
public class PropertyKeyConst {
public final static String IS_USE_CLOUD_NAMESPACE_PARSING = "isUseCloudNamespaceParsing";
public final static String IS_USE_ENDPOINT_PARSING_RULE = "isUseEndpointParsingRule";
public final static String ENDPOINT = "endpoint";
@ -50,6 +52,9 @@ public class PropertyKeyConst {
public final static String NAMING_POLLING_THREAD_COUNT = "namingPollingThreadCount";
/**
* Get the key value of some variable value from the system property
*/
public static class SystemEnv {
public static final String ALIBABA_ALIWARE_ENDPOINT_PORT = "ALIBABA_ALIWARE_ENDPOINT_PORT";
@ -58,4 +63,5 @@ public class PropertyKeyConst {
public static final String ALIBABA_ALIWARE_ENDPOINT_URL = "ALIBABA_ALIWARE_ENDPOINT_URL";
}
}

View File

@ -16,6 +16,9 @@
package com.alibaba.nacos.api;
/**
* <p>
* Support for reading the value of the specified variable from the -D parameter
* </P>
* Properties that are preferred to which in {@link PropertyKeyConst}
*
* @author pbting
@ -27,5 +30,23 @@ public interface SystemPropertyKeyConst {
String NAMING_WEB_CONTEXT = "nacos.naming.web.context";
String NACOS_NAMING_REQUEST_MODULE = "nacos.naming.request.module";
/**
* In the cloud (Alibaba Cloud or other cloud vendors) environment,
* whether to enable namespace resolution in the cloud environment.
* <p>
* The default is on.
* </p>
*/
String IS_USE_CLOUD_NAMESPACE_PARSING = "nacos.use.cloud.namespace.parsing";
/**
* In the cloud environment, if the process level requires a globally uniform namespace,
* it can be specified with the -D parameter.
*/
String ANS_NAMESPACE = "ans.namespace";
/**
* It is also supported by the -D parameter.
*/
String IS_USE_ENDPOINT_PARSING_RULE = "nacos.use.endpoint.parsing.rule";
}

View File

@ -105,9 +105,9 @@ public class Constants {
public static final float DEFAULT_PROTECT_THRESHOLD = 0.0F;
public static final String LINE_SEPARATOR = Character.toString((char)1);
public static final String LINE_SEPARATOR = Character.toString((char) 1);
public static final String WORD_SEPARATOR = Character.toString((char)2);
public static final String WORD_SEPARATOR = Character.toString((char) 2);
public static final String LONGPOLLING_LINE_SEPARATOR = "\r\n";
@ -125,6 +125,8 @@ public class Constants {
public static final String DEFAULT_NAMESPACE_ID = "public";
public static final boolean DEFAULT_USE_CLOUD_NAMESPACE_PARSING = true;
public static final int WRITE_REDIRECT_CODE = 307;
public static final String SERVICE_INFO_SPLITER = "@@";

View File

@ -16,7 +16,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

View File

@ -16,6 +16,7 @@
package com.alibaba.nacos.client.config;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.SystemPropertyKeyConst;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
@ -31,10 +32,10 @@ import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult;
import com.alibaba.nacos.client.config.impl.LocalConfigInfoProcessor;
import com.alibaba.nacos.client.config.utils.ContentUtils;
import com.alibaba.nacos.client.config.utils.ParamUtils;
import com.alibaba.nacos.client.config.utils.TenantUtil;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.StringUtils;
import com.alibaba.nacos.client.utils.TemplateUtils;
import com.alibaba.nacos.client.utils.TenantUtil;
import org.slf4j.Logger;
import java.io.IOException;
@ -87,20 +88,27 @@ public class NacosConfigService implements ConfigService {
private void initNamespace(Properties properties) {
String namespaceTmp = null;
namespaceTmp = TemplateUtils.stringBlankAndThenExecute(namespaceTmp, new Callable<String>() {
@Override
public String call() {
return TenantUtil.getUserTenant();
}
});
String isUseCloudNamespaceParsing =
properties.getProperty(PropertyKeyConst.IS_USE_CLOUD_NAMESPACE_PARSING,
System.getProperty(SystemPropertyKeyConst.IS_USE_CLOUD_NAMESPACE_PARSING,
String.valueOf(Constants.DEFAULT_USE_CLOUD_NAMESPACE_PARSING)));
namespaceTmp = TemplateUtils.stringBlankAndThenExecute(namespaceTmp, new Callable<String>() {
@Override
public String call() {
String namespace = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_NAMESPACE);
return StringUtils.isNotBlank(namespace) ? namespace : EMPTY;
}
});
if (Boolean.valueOf(isUseCloudNamespaceParsing)) {
namespaceTmp = TemplateUtils.stringBlankAndThenExecute(namespaceTmp, new Callable<String>() {
@Override
public String call() {
return TenantUtil.getUserTenantForAcm();
}
});
namespaceTmp = TemplateUtils.stringBlankAndThenExecute(namespaceTmp, new Callable<String>() {
@Override
public String call() {
String namespace = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_NAMESPACE);
return StringUtils.isNotBlank(namespace) ? namespace : EMPTY;
}
});
}
if (StringUtils.isBlank(namespaceTmp)) {
namespaceTmp = properties.getProperty(PropertyKeyConst.NAMESPACE);

View File

@ -22,8 +22,8 @@ import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager;
import com.alibaba.nacos.client.config.filter.impl.ConfigResponse;
import com.alibaba.nacos.client.config.utils.MD5;
import com.alibaba.nacos.client.config.utils.TenantUtil;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.TenantUtil;
import org.slf4j.Logger;
import java.util.ArrayList;
@ -145,7 +145,7 @@ public class CacheData {
if (this == obj) {
return true;
}
CacheData other = (CacheData)obj;
CacheData other = (CacheData) obj;
return dataId.equals(other.dataId) && group.equals(other.group);
}
@ -172,7 +172,7 @@ public class CacheData {
ClassLoader appClassLoader = listener.getClass().getClassLoader();
try {
if (listener instanceof AbstractSharedListener) {
AbstractSharedListener adapter = (AbstractSharedListener)listener;
AbstractSharedListener adapter = (AbstractSharedListener) listener;
adapter.fillContext(dataId, group);
LOGGER.info("[{}] [notify-context] dataId={}, group={}, md5={}", name, dataId, group, md5);
}
@ -236,7 +236,7 @@ public class CacheData {
this.configFilterChainManager = configFilterChainManager;
this.dataId = dataId;
this.group = group;
this.tenant = TenantUtil.getUserTenant();
this.tenant = TenantUtil.getUserTenantForAcm();
listeners = new CopyOnWriteArrayList<ManagerListenerWrap>();
this.isInitializing = true;
this.content = loadCacheContentFromDiskLocal(name, dataId, group, tenant);
@ -298,7 +298,7 @@ class ManagerListenerWrap {
if (obj == this) {
return true;
}
ManagerListenerWrap other = (ManagerListenerWrap)obj;
ManagerListenerWrap other = (ManagerListenerWrap) obj;
return listener.equals(other.listener);
}

View File

@ -24,29 +24,19 @@ import com.alibaba.nacos.client.config.http.HttpAgent;
import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult;
import com.alibaba.nacos.client.config.utils.ContentUtils;
import com.alibaba.nacos.client.config.utils.MD5;
import com.alibaba.nacos.client.config.utils.TenantUtil;
import com.alibaba.nacos.client.monitor.MetricsMonitor;
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.TenantUtil;
import org.slf4j.Logger;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
import static com.alibaba.nacos.api.common.Constants.LINE_SEPARATOR;
@ -143,7 +133,7 @@ public class ClientWorker {
//reset so that server not hang this check
cache.setInitializing(true);
} else {
int taskId = cacheMap.get().size() / (int)ParamUtil.getPerTaskConfigSize();
int taskId = cacheMap.get().size() / (int) ParamUtil.getPerTaskConfigSize();
cache.setTaskId(taskId);
}
@ -189,7 +179,7 @@ public class ClientWorker {
}
public CacheData getCache(String dataId, String group) {
return getCache(dataId, group, TenantUtil.getUserTenant());
return getCache(dataId, group, TenantUtil.getUserTenantForAcm());
}
public CacheData getCache(String dataId, String group, String tenant) {
@ -299,9 +289,9 @@ public class ClientWorker {
// 分任务
int listenerSize = cacheMap.get().size();
// 向上取整为批数
int longingTaskCount = (int)Math.ceil(listenerSize / ParamUtil.getPerTaskConfigSize());
int longingTaskCount = (int) Math.ceil(listenerSize / ParamUtil.getPerTaskConfigSize());
if (longingTaskCount > currentLongingTaskCount) {
for (int i = (int)currentLongingTaskCount; i < longingTaskCount; i++) {
for (int i = (int) currentLongingTaskCount; i < longingTaskCount; i++) {
// 要判断任务是否在执行 这块需要好好想想 任务列表现在是无序的变化过程可能有问题
executorService.execute(new LongPollingRunnable(i));
}

View File

@ -16,6 +16,7 @@
package com.alibaba.nacos.client.config.impl;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.SystemPropertyKeyConst;
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;
@ -180,7 +181,13 @@ public class ServerListManager {
}
String endpointTmp = properties.getProperty(PropertyKeyConst.ENDPOINT);
if (Boolean.valueOf(properties.getProperty(PropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE, ParamUtil.USE_ENDPOINT_PARSING_RULE_DEFAULT_VALUE))) {
// Whether to enable domain name resolution rules
String isUseEndpointRuleParsing =
properties.getProperty(PropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE,
System.getProperty(SystemPropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE,
String.valueOf(ParamUtil.USE_ENDPOINT_PARSING_RULE_DEFAULT_VALUE)));
if (Boolean.valueOf(isUseEndpointRuleParsing)) {
String endpointUrl = ParamUtil.parsingEndpointRule(endpointTmp);
if (StringUtils.isNotBlank(endpointUrl)) {
serverAddrsStr = "";
@ -352,7 +359,7 @@ public class ServerListManager {
}
/**
* 不同环境的名称
* The name of the different environment
*/
private String name;
private String namespace = "";
@ -362,7 +369,7 @@ public class ServerListManager {
static public final String FIXED_NAME = "fixed";
private int initServerlistRetryTimes = 5;
/**
* 和其他server的连接超时和socket超时
* Connection timeout and socket timeout with other servers
*/
static final int TIMEOUT = 5000;
@ -384,7 +391,7 @@ public class ServerListManager {
}
/**
* 对地址列表排序同机房优先
* Sort the address list, with the same room priority.
*/
class ServerAddressIterator implements Iterator<String> {

View File

@ -60,7 +60,7 @@ public class NacosNamingMaintainService implements NamingMaintainService {
}
private void init(Properties properties) {
namespace = InitUtils.initNamespace(properties);
namespace = InitUtils.initNamespaceForNaming(properties);
initServerAddr(properties);
InitUtils.initWebRootContext();

View File

@ -79,7 +79,7 @@ public class NacosNamingService implements NamingService {
}
private void init(Properties properties) {
namespace = InitUtils.initNamespace(properties);
namespace = InitUtils.initNamespaceForNaming(properties);
initServerAddr(properties);
InitUtils.initWebRootContext();
initCacheDir();

View File

@ -21,8 +21,8 @@ import com.alibaba.nacos.client.naming.cache.ConcurrentDiskUtil;
import com.alibaba.nacos.client.naming.cache.DiskCache;
import com.alibaba.nacos.client.naming.core.HostReactor;
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.StringUtils;
import java.io.BufferedReader;
import java.io.File;

View File

@ -20,7 +20,7 @@ import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.StringUtils;
import com.alibaba.nacos.client.utils.StringUtils;
import java.io.BufferedReader;
import java.io.File;

View File

@ -23,8 +23,8 @@ import com.alibaba.nacos.client.monitor.MetricsMonitor;
import com.alibaba.nacos.client.naming.backups.FailoverReactor;
import com.alibaba.nacos.client.naming.cache.DiskCache;
import com.alibaba.nacos.client.naming.net.NamingProxy;
import com.alibaba.nacos.client.naming.utils.StringUtils;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.client.utils.StringUtils;
import java.util.*;
import java.util.concurrent.*;

View File

@ -17,7 +17,7 @@ package com.alibaba.nacos.client.naming.core;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.client.naming.utils.IoUtils;
import com.alibaba.nacos.client.naming.utils.StringUtils;
import com.alibaba.nacos.client.utils.StringUtils;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

View File

@ -17,7 +17,7 @@ package com.alibaba.nacos.client.naming.net;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.client.naming.utils.IoUtils;
import com.alibaba.nacos.client.naming.utils.StringUtils;
import com.alibaba.nacos.client.utils.StringUtils;
import com.google.common.net.HttpHeaders;
import java.io.IOException;

View File

@ -32,6 +32,7 @@ import com.alibaba.nacos.client.config.impl.SpasAdapter;
import com.alibaba.nacos.client.monitor.MetricsMonitor;
import com.alibaba.nacos.client.naming.beat.BeatInfo;
import com.alibaba.nacos.client.naming.utils.*;
import com.alibaba.nacos.client.utils.StringUtils;
import com.alibaba.nacos.client.utils.TemplateUtils;
import com.alibaba.nacos.common.util.HttpMethod;
import com.alibaba.nacos.common.util.UuidUtils;

View File

@ -18,24 +18,56 @@ package com.alibaba.nacos.client.naming.utils;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.SystemPropertyKeyConst;
import com.alibaba.nacos.client.identify.CredentialService;
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 com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.client.utils.*;
import java.util.Properties;
import java.util.concurrent.Callable;
/**
* @author liaochuntao
* @author deshao
*/
public class InitUtils {
public static final String initNamespace(Properties properties) {
/**
* Add a difference to the name naming. This method simply initializes the namespace for Naming.
* Config initialization is not the same, so it cannot be reused directly.
*
* @param properties
* @return
*/
public static final String initNamespaceForNaming(Properties properties) {
String tmpNamespace = null;
String isUseCloudNamespaceParsing =
properties.getProperty(PropertyKeyConst.IS_USE_CLOUD_NAMESPACE_PARSING,
System.getProperty(SystemPropertyKeyConst.IS_USE_CLOUD_NAMESPACE_PARSING,
String.valueOf(Constants.DEFAULT_USE_CLOUD_NAMESPACE_PARSING)));
if (Boolean.valueOf(isUseCloudNamespaceParsing)) {
tmpNamespace = TenantUtil.getUserTenantForAns();
tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable<String>() {
@Override
public String call() {
String namespace = System.getProperty(SystemPropertyKeyConst.ANS_NAMESPACE);
LogUtils.NAMING_LOGGER.info("initializer namespace from System Property :" + namespace);
return namespace;
}
});
tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable<String>() {
@Override
public String call() {
String namespace = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_NAMESPACE);
LogUtils.NAMING_LOGGER.info("initializer namespace from System Environment :" + namespace);
return namespace;
}
});
}
tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable<String>() {
@Override
public String call() {
@ -45,26 +77,7 @@ public class InitUtils {
}
});
tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable<String>() {
@Override
public String call() {
String namespace = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_NAMESPACE);
LogUtils.NAMING_LOGGER.info("initializer namespace from System Environment :" + namespace);
return namespace;
}
});
tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable<String>() {
@Override
public String call() {
String namespace = CredentialService.getInstance().getCredential().getTenantId();
LogUtils.NAMING_LOGGER.info("initializer namespace from Credential Module " + namespace);
return namespace;
}
});
if (com.alibaba.nacos.client.utils.StringUtils.isEmpty(tmpNamespace) && properties != null) {
if (StringUtils.isEmpty(tmpNamespace) && properties != null) {
tmpNamespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
}
@ -97,13 +110,18 @@ public class InitUtils {
return "";
}
// 是否开启域名解析规则
boolean isUseEndpointParsingRule = Boolean.valueOf(properties.getProperty(PropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE, ParamUtil.USE_ENDPOINT_PARSING_RULE_DEFAULT_VALUE));
// Whether to enable domain name resolution rules
String isUseEndpointRuleParsing =
properties.getProperty(PropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE,
System.getProperty(SystemPropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE,
String.valueOf(ParamUtil.USE_ENDPOINT_PARSING_RULE_DEFAULT_VALUE)));
boolean isUseEndpointParsingRule = Boolean.valueOf(isUseEndpointRuleParsing);
String endpointUrl;
if (isUseEndpointParsingRule) {
// 获取设置的域名信息
// Get the set domain name information
endpointUrl = ParamUtil.parsingEndpointRule(properties.getProperty(PropertyKeyConst.ENDPOINT));
if (com.alibaba.nacos.client.utils.StringUtils.isNotBlank(endpointUrl)) {
if (com.alibaba.nacos.client.utils.StringUtils.isBlank(endpointUrl)) {
return "";
}
} else {

View File

@ -16,6 +16,8 @@
package com.alibaba.nacos.client.naming.utils;
import com.alibaba.nacos.client.utils.StringUtils;
import java.io.*;
import java.nio.channels.FileChannel;
import java.util.ArrayList;

View File

@ -15,6 +15,8 @@
*/
package com.alibaba.nacos.client.naming.utils;
import com.alibaba.nacos.client.utils.StringUtils;
import java.net.InetAddress;
import java.net.UnknownHostException;

View File

@ -1,163 +0,0 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.client.naming.utils;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.Locale;
/**
* @author nkorange
*/
public class StringUtils {
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
public static final String EMPTY = "";
public static boolean equals(String str1, String str2) {
return str1 == null ? str2 == null : str1.equals(str2);
}
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();
}
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
public static String escapeJavaScript(String str) {
return escapeJavaStyleString(str, true, true);
}
private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes, boolean escapeForwardSlash) {
if (str == null) {
return null;
}
try {
StringWriter writer = new StringWriter(str.length() * 2);
escapeJavaStyleString(writer, str, escapeSingleQuotes, escapeForwardSlash);
return writer.toString();
} catch (IOException ioe) {
// this should never ever happen while writing to a StringWriter
return null;
}
}
private static String hex(char ch) {
return Integer.toHexString(ch).toUpperCase(Locale.ENGLISH);
}
private static void escapeJavaStyleString(Writer out, String str, boolean escapeSingleQuote,
boolean escapeForwardSlash) throws IOException {
if (out == null) {
throw new IllegalArgumentException("The Writer must not be null");
}
if (str == null) {
return;
}
int sz;
sz = str.length();
for (int i = 0; i < sz; i++) {
char ch = str.charAt(i);
// handle unicode
if (ch > 0xfff) {
out.write("\\u" + hex(ch));
} else if (ch > 0xff) {
out.write("\\u0" + hex(ch));
} else if (ch > 0x7f) {
out.write("\\u00" + hex(ch));
} else if (ch < 32) {
switch (ch) {
case '\b':
out.write('\\');
out.write('b');
break;
case '\n':
out.write('\\');
out.write('n');
break;
case '\t':
out.write('\\');
out.write('t');
break;
case '\f':
out.write('\\');
out.write('f');
break;
case '\r':
out.write('\\');
out.write('r');
break;
default:
if (ch > 0xf) {
out.write("\\u00" + hex(ch));
} else {
out.write("\\u000" + hex(ch));
}
break;
}
} else {
switch (ch) {
case '\'':
if (escapeSingleQuote) {
out.write('\\');
}
out.write('\'');
break;
case '"':
out.write('\\');
out.write('"');
break;
case '\\':
out.write('\\');
out.write('\\');
break;
case '/':
if (escapeForwardSlash) {
out.write('\\');
}
out.write('/');
break;
default:
out.write(ch);
break;
}
}
}
}
}

View File

@ -33,7 +33,7 @@ public class ParamUtil {
private final static Logger LOGGER = LogUtils.logger(ParamUtil.class);
public final static String USE_ENDPOINT_PARSING_RULE_DEFAULT_VALUE = "true";
public final static boolean USE_ENDPOINT_PARSING_RULE_DEFAULT_VALUE = true;
private static final Pattern PATTERN = Pattern.compile("\\$\\{[^}]+\\}");
private static String defaultContextPath = "nacos";

View File

@ -15,7 +15,11 @@
*/
package com.alibaba.nacos.client.utils;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.Locale;
/**
* string util
@ -26,6 +30,8 @@ public class StringUtils {
public static final int INDEX_NOT_FOUND = -1;
public static final String EMPTY = "";
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
@ -92,4 +98,106 @@ public class StringUtils {
return stringBuilder.toString();
}
public static String escapeJavaScript(String str) {
return escapeJavaStyleString(str, true, true);
}
private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes, boolean escapeForwardSlash) {
if (str == null) {
return null;
}
try {
StringWriter writer = new StringWriter(str.length() * 2);
escapeJavaStyleString(writer, str, escapeSingleQuotes, escapeForwardSlash);
return writer.toString();
} catch (IOException ioe) {
// this should never ever happen while writing to a StringWriter
return null;
}
}
private static String hex(char ch) {
return Integer.toHexString(ch).toUpperCase(Locale.ENGLISH);
}
private static void escapeJavaStyleString(Writer out, String str, boolean escapeSingleQuote,
boolean escapeForwardSlash) throws IOException {
if (out == null) {
throw new IllegalArgumentException("The Writer must not be null");
}
if (str == null) {
return;
}
int sz;
sz = str.length();
for (int i = 0; i < sz; i++) {
char ch = str.charAt(i);
// handle unicode
if (ch > 0xfff) {
out.write("\\u" + hex(ch));
} else if (ch > 0xff) {
out.write("\\u0" + hex(ch));
} else if (ch > 0x7f) {
out.write("\\u00" + hex(ch));
} else if (ch < 32) {
switch (ch) {
case '\b':
out.write('\\');
out.write('b');
break;
case '\n':
out.write('\\');
out.write('n');
break;
case '\t':
out.write('\\');
out.write('t');
break;
case '\f':
out.write('\\');
out.write('f');
break;
case '\r':
out.write('\\');
out.write('r');
break;
default:
if (ch > 0xf) {
out.write("\\u00" + hex(ch));
} else {
out.write("\\u000" + hex(ch));
}
break;
}
} else {
switch (ch) {
case '\'':
if (escapeSingleQuote) {
out.write('\\');
}
out.write('\'');
break;
case '"':
out.write('\\');
out.write('"');
break;
case '\\':
out.write('\\');
out.write('\\');
break;
case '/':
if (escapeForwardSlash) {
out.write('\\');
}
out.write('/');
break;
default:
out.write(ch);
break;
}
}
}
}
}

View File

@ -13,9 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.client.config.utils;
import com.alibaba.nacos.client.utils.StringUtils;
package com.alibaba.nacos.client.utils;
/**
* Tenant Util
@ -24,20 +22,42 @@ import com.alibaba.nacos.client.utils.StringUtils;
*/
public class TenantUtil {
private static String userTenant = "";
private static String userTenant;
static {
userTenant = System.getProperty("tenant.id", "");
}
/**
* Adapt the way ACM gets tenant on the cloud.
* <p>
* Note the difference between getting and getting ANS.
* Since the processing logic on the server side is different, the default value returns differently.
* </p>
*
* @return
*/
public static String getUserTenantForAcm() {
String tmp = userTenant;
if (StringUtils.isBlank(userTenant)) {
userTenant = System.getProperty("acm.namespace", "");
tmp = System.getProperty("acm.namespace", "");
}
return tmp;
}
public static String getUserTenant() {
return userTenant;
}
/**
* Adapt the way ANS gets tenant on the cloud.
*
* @return
*/
public static String getUserTenantForAns() {
String tmp = userTenant;
public static void setUserTenant(String userTenant) {
TenantUtil.userTenant = userTenant;
if (StringUtils.isBlank(userTenant)) {
tmp = System.getProperty("ans.namespace");
}
return tmp;
}
}

View File

@ -18,7 +18,7 @@
<parent>
<artifactId>nacos-all</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>1.0.0</version>
<version>1.0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -17,7 +17,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
</parent>
<artifactId>nacos-console</artifactId>
<!--<packaging>war</packaging>-->

View File

@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

View File

@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

View File

@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

View File

@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

17
pom.xml
View File

@ -21,7 +21,7 @@
<inceptionYear>2018</inceptionYear>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<packaging>pom</packaging>
<name>Alibaba NACOS ${project.version}</name>
@ -36,7 +36,7 @@
<url>git@github.com:alibaba/nacos.git</url>
<connection>scm:git@github.com:alibaba/nacos.git</connection>
<developerConnection>scm:git@github.com:alibaba/nacos.git</developerConnection>
<tag>nacos-all-1.0.0</tag>
<tag>nacos-all-1.0.1</tag>
</scm>
<mailingLists>
@ -378,18 +378,6 @@
</plugin>
</plugins>
</build>
<distributionManagement>
<snapshotRepository>
<id>sona</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>sona</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
</distributionManagement>
</profile>
<profile>
<id>it-test</id>
@ -738,5 +726,6 @@
</dependencies>
</dependencyManagement>
</project>

View File

@ -17,7 +17,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>