support ans

This commit is contained in:
pbting 2019-02-13 15:42:41 +08:00
parent 489efb3292
commit 1a6fa9e69c
10 changed files with 1203 additions and 904 deletions

View File

@ -22,24 +22,26 @@ package com.alibaba.nacos.api;
*/
public class PropertyKeyConst {
public final static String ENDPOINT = "endpoint";
public final static String WEB_CONTEXT = "webContext";
public final static String NAMESPACE = "namespace";
public final static String ENDPOINT = "endpoint";
public final static String ACCESS_KEY = "accessKey";
public final static String NAMESPACE = "namespace";
public final static String SECRET_KEY = "secretKey";
public final static String ACCESS_KEY = "accessKey";
public final static String SERVER_ADDR = "serverAddr";
public final static String SECRET_KEY = "secretKey";
public final static String CONTEXT_PATH = "contextPath";
public final static String SERVER_ADDR = "serverAddr";
public final static String CLUSTER_NAME = "clusterName";
public final static String CONTEXT_PATH = "contextPath";
public final static String ENCODE = "encode";
public final static String CLUSTER_NAME = "clusterName";
public final static String NAMING_LOAD_CACHE_AT_START = "namingLoadCacheAtStart";
public final static String NAMING_CLIENT_BEAT_THREAD_COUNT = "namingClientBeatThreadCount";
public final static String NAMING_POLLING_THREAD_COUNT = "namingPollingThreadCount";
public final static String ENCODE = "encode";
public final static String NAMING_LOAD_CACHE_AT_START = "namingLoadCacheAtStart";
public final static String NAMING_CLIENT_BEAT_THREAD_COUNT = "namingClientBeatThreadCount";
public final static String NAMING_POLLING_THREAD_COUNT = "namingPollingThreadCount";
}

View File

@ -21,26 +21,32 @@ package com.alibaba.nacos.client.identify;
* @author Nacos
*/
public class Constants {
public static final String ACCESS_KEY = "accessKey";
public static final String ACCESS_KEY = "accessKey";
public static final String SECRET_KEY = "secretKey";
public static final String SECRET_KEY = "secretKey";
public static final String PROPERTIES_FILENAME = "spas.properties";
public static final String TENANT_ID = "tenantId";
public static final String CREDENTIAL_PATH = "/home/admin/.spas_key/";
public static final String PROPERTIES_FILENAME = "spas.properties";
public static final String CREDENTIAL_DEFAULT = "default";
public static final String CREDENTIAL_PATH = "/home/admin/.spas_key/";
public static final String DOCKER_CREDENTIAL_PATH = "/etc/instanceInfo";
public static final String CREDENTIAL_DEFAULT = "default";
public static final String DOCKER_ACCESS_KEY = "env_spas_accessKey";
public static final String DOCKER_CREDENTIAL_PATH = "/etc/instanceInfo";
public static final String DOCKER_SECRET_KEY = "env_spas_secretKey";
public static final String DOCKER_ACCESS_KEY = "env_spas_accessKey";
public static final String ENV_ACCESS_KEY = "spas_accessKey";
public static final String DOCKER_SECRET_KEY = "env_spas_secretKey";
public static final String ENV_SECRET_KEY = "spas_secretKey";
public static final String DOCKER_TENANT_ID = "ebv_spas_tenantId";
public static final String NO_APP_NAME = "";
public static final String ENV_ACCESS_KEY = "spas_accessKey";
public static final String ENV_SECRET_KEY = "spas_secretKey";
public static final String ENV_TENANT_ID = "tenant.id";
public static final String NO_APP_NAME = "";
}

View File

@ -15,198 +15,224 @@
*/
package com.alibaba.nacos.client.identify;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import com.alibaba.nacos.client.config.utils.LogUtils;
import com.alibaba.nacos.client.logger.Logger;
import com.alibaba.nacos.client.utils.StringUtils;
import java.io.*;
import java.net.URL;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import com.alibaba.nacos.client.config.utils.LogUtils;
import com.alibaba.nacos.client.logger.Logger;
import com.alibaba.nacos.client.utils.StringUtils;
/**
* Credential Watcher
*
* @author Nacos
*/
public class CredentialWatcher {
static final public Logger SpasLogger = LogUtils.logger(CredentialWatcher.class);
private static final long REFRESH_INTERVAL = 10 * 1000;
static final public Logger SpasLogger = LogUtils.logger(CredentialWatcher.class);
private static final long REFRESH_INTERVAL = 10 * 1000;
private CredentialService serviceInstance;
private String appName;
private String propertyPath;
private TimerTask watcher;
private boolean stopped;
private CredentialService serviceInstance;
private String appName;
private String propertyPath;
private TimerTask watcher;
private boolean stopped;
@SuppressWarnings("PMD.AvoidUseTimerRule")
public CredentialWatcher(String appName, CredentialService serviceInstance) {
this.appName = appName;
this.serviceInstance = serviceInstance;
loadCredential(true);
watcher = new TimerTask() {
private Timer timer = new Timer(true);
private long modified = 0;
@SuppressWarnings("PMD.AvoidUseTimerRule")
public CredentialWatcher(String appName, CredentialService serviceInstance) {
this.appName = appName;
this.serviceInstance = serviceInstance;
loadCredential(true);
watcher = new TimerTask() {
private Timer timer = new Timer(true);
private long modified = 0;
{
timer.schedule(this, REFRESH_INTERVAL, REFRESH_INTERVAL);
}
{
timer.schedule(this, REFRESH_INTERVAL, REFRESH_INTERVAL);
}
@Override
public void run() {
synchronized (this) {
if (stopped) {
return;
}
boolean reload = false;
if (propertyPath == null) {
reload = true;
} else {
File file = new File(propertyPath);
long lastModified = file.lastModified();
if (modified != lastModified) {
reload = true;
modified = lastModified;
}
}
if (reload) {
loadCredential(false);
}
}
}
};
}
@Override
public void run() {
synchronized (this) {
if (stopped) {
return;
}
boolean reload = false;
if (propertyPath == null) {
reload = true;
}
else {
File file = new File(propertyPath);
long lastModified = file.lastModified();
if (modified != lastModified) {
reload = true;
modified = lastModified;
}
}
if (reload) {
loadCredential(false);
}
}
}
};
}
public void stop() {
if (stopped) {
return;
}
if (watcher != null) {
synchronized (watcher) {
watcher.cancel();
stopped = true;
}
}
SpasLogger.info(appName, this.getClass().getSimpleName() + " is stopped");
}
public void stop() {
if (stopped) {
return;
}
if (watcher != null) {
synchronized (watcher) {
watcher.cancel();
stopped = true;
}
}
SpasLogger.info(appName, this.getClass().getSimpleName() + " is stopped");
}
private void loadCredential(boolean init) {
boolean logWarn = init;
if (propertyPath == null) {
URL url = ClassLoader.getSystemResource(Constants.PROPERTIES_FILENAME);
if (url != null) {
propertyPath = url.getPath();
}
if (propertyPath == null || propertyPath.isEmpty()) {
private void loadCredential(boolean init) {
boolean logWarn = init;
if (propertyPath == null) {
URL url = ClassLoader.getSystemResource(Constants.PROPERTIES_FILENAME);
if (url != null) {
propertyPath = url.getPath();
}
if (propertyPath == null || propertyPath.isEmpty()) {
String value = System.getProperty("spas.identity");
if (StringUtils.isNotEmpty(value)) {
propertyPath = value;
}
if (propertyPath == null || propertyPath.isEmpty()) {
propertyPath = Constants.CREDENTIAL_PATH + (appName == null ? Constants.CREDENTIAL_DEFAULT
: appName);
} else {
if (logWarn) {
SpasLogger.info(appName, "Defined credential file: -D" + "spas.identity" + "=" + propertyPath);
}
}
} else {
if (logWarn) {
SpasLogger.info(appName, "Load credential file from classpath: " + Constants.PROPERTIES_FILENAME);
}
}
}
String value = System.getProperty("spas.identity");
if (StringUtils.isNotEmpty(value)) {
propertyPath = value;
}
if (propertyPath == null || propertyPath.isEmpty()) {
propertyPath = Constants.CREDENTIAL_PATH
+ (appName == null ? Constants.CREDENTIAL_DEFAULT : appName);
}
else {
if (logWarn) {
SpasLogger.info(appName, "Defined credential file: -D"
+ "spas.identity" + "=" + propertyPath);
}
}
}
else {
if (logWarn) {
SpasLogger.info(appName, "Load credential file from classpath: "
+ Constants.PROPERTIES_FILENAME);
}
}
}
InputStream propertiesIS = null;
do {
try {
propertiesIS = new FileInputStream(propertyPath);
} catch (FileNotFoundException e) {
if (appName != null && !appName.equals(Constants.CREDENTIAL_DEFAULT) && propertyPath.equals(
Constants.CREDENTIAL_PATH + appName)) {
propertyPath = Constants.CREDENTIAL_PATH + Constants.CREDENTIAL_DEFAULT;
continue;
}
if (!Constants.DOCKER_CREDENTIAL_PATH.equals(propertyPath)) {
propertyPath = Constants.DOCKER_CREDENTIAL_PATH;
continue;
}
}
break;
} while (true);
InputStream propertiesIS = null;
do {
try {
propertiesIS = new FileInputStream(propertyPath);
}
catch (FileNotFoundException e) {
if (appName != null && !appName.equals(Constants.CREDENTIAL_DEFAULT)
&& propertyPath.equals(Constants.CREDENTIAL_PATH + appName)) {
propertyPath = Constants.CREDENTIAL_PATH
+ Constants.CREDENTIAL_DEFAULT;
continue;
}
if (!Constants.DOCKER_CREDENTIAL_PATH.equals(propertyPath)) {
propertyPath = Constants.DOCKER_CREDENTIAL_PATH;
continue;
}
}
break;
}
while (true);
String accessKey = null;
String secretKey = null;
if (propertiesIS == null) {
propertyPath = null;
accessKey = System.getenv(Constants.ENV_ACCESS_KEY);
secretKey = System.getenv(Constants.ENV_SECRET_KEY);
if (accessKey == null && secretKey == null) {
if (logWarn) {
SpasLogger.info(appName, "No credential found");
}
return;
}
} else {
Properties properties = new Properties();
try {
properties.load(propertiesIS);
} catch (IOException e) {
SpasLogger.error("26", "Unable to load credential file, appName:" + appName
+ "Unable to load credential file " + propertyPath, e);
propertyPath = null;
return;
} finally {
try {
propertiesIS.close();
} catch (IOException e) {
SpasLogger.error("27", "Unable to close credential file, appName:" + appName
+ "Unable to close credential file " + propertyPath, e);
}
}
String accessKey = null;
String secretKey = null;
String tenantId = "";
if (propertiesIS == null) {
propertyPath = null;
accessKey = System.getenv(Constants.ENV_ACCESS_KEY);
secretKey = System.getenv(Constants.ENV_SECRET_KEY);
tenantId = System.getenv(Constants.ENV_TENANT_ID);
if (accessKey == null && secretKey == null) {
if (logWarn) {
SpasLogger.info(appName, "No credential found");
}
return;
}
}
else {
Properties properties = new Properties();
try {
properties.load(propertiesIS);
}
catch (IOException e) {
SpasLogger.error("26", "Unable to load credential file, appName:"
+ appName + "Unable to load credential file " + propertyPath, e);
propertyPath = null;
return;
}
finally {
try {
propertiesIS.close();
}
catch (IOException e) {
SpasLogger.error("27",
"Unable to close credential file, appName:" + appName
+ "Unable to close credential file " + propertyPath,
e);
}
}
if (logWarn) {
SpasLogger.info(appName, "Load credential file " + propertyPath);
}
if (logWarn) {
SpasLogger.info(appName, "Load credential file " + propertyPath);
}
if (!Constants.DOCKER_CREDENTIAL_PATH.equals(propertyPath)) {
if (properties.containsKey(Constants.ACCESS_KEY)) {
accessKey = properties.getProperty(Constants.ACCESS_KEY);
}
if (properties.containsKey(Constants.SECRET_KEY)) {
secretKey = properties.getProperty(Constants.SECRET_KEY);
}
} else {
if (properties.containsKey(Constants.DOCKER_ACCESS_KEY)) {
accessKey = properties.getProperty(Constants.DOCKER_ACCESS_KEY);
}
if (properties.containsKey(Constants.DOCKER_SECRET_KEY)) {
secretKey = properties.getProperty(Constants.DOCKER_SECRET_KEY);
}
}
}
if (!Constants.DOCKER_CREDENTIAL_PATH.equals(propertyPath)) {
if (properties.containsKey(Constants.ACCESS_KEY)) {
accessKey = properties.getProperty(Constants.ACCESS_KEY);
}
if (properties.containsKey(Constants.SECRET_KEY)) {
secretKey = properties.getProperty(Constants.SECRET_KEY);
}
if (properties.containsKey(Constants.TENANT_ID)) {
tenantId = properties.getProperty(Constants.TENANT_ID);
}
}
else {
if (properties.containsKey(Constants.DOCKER_ACCESS_KEY)) {
accessKey = properties.getProperty(Constants.DOCKER_ACCESS_KEY);
}
if (properties.containsKey(Constants.DOCKER_SECRET_KEY)) {
secretKey = properties.getProperty(Constants.DOCKER_SECRET_KEY);
}
if (accessKey != null) {
accessKey = accessKey.trim();
}
if (secretKey != null) {
secretKey = secretKey.trim();
}
if (properties.containsKey(Constants.DOCKER_TENANT_ID)) {
tenantId = properties.getProperty(Constants.DOCKER_TENANT_ID);
}
}
}
Credentials credential = new Credentials(accessKey, secretKey);
if (!credential.valid()) {
SpasLogger.warn("1", "Credential file missing required property" + appName + "Credential file missing "
+ Constants.ACCESS_KEY + " or " + Constants.SECRET_KEY);
propertyPath = null;
// return;
}
if (accessKey != null) {
accessKey = accessKey.trim();
}
if (secretKey != null) {
secretKey = secretKey.trim();
}
serviceInstance.setCredential(credential);
}
if (tenantId != null) {
tenantId = tenantId.trim();
}
Credentials credential = new Credentials(accessKey, secretKey, tenantId);
if (!credential.valid()) {
SpasLogger.warn("1",
"Credential file missing required property" + appName
+ "Credential file missing " + Constants.ACCESS_KEY + " or "
+ Constants.SECRET_KEY);
propertyPath = null;
// return;
}
serviceInstance.setCredential(credential);
}
}

View File

@ -22,45 +22,56 @@ package com.alibaba.nacos.client.identify;
*/
public class Credentials implements SpasCredential {
private volatile String accessKey;
private volatile String accessKey;
private volatile String secretKey;
private volatile String secretKey;
public Credentials(String accessKey, String secretKey) {
this.accessKey = accessKey;
this.secretKey = secretKey;
}
private volatile String tenantId;
public Credentials() {
this(null, null);
}
public Credentials(String accessKey, String secretKey, String tenantId) {
this.accessKey = accessKey;
this.secretKey = secretKey;
this.tenantId = tenantId;
}
public String getAccessKey() {
return accessKey;
}
public Credentials() {
this(null, null, null);
}
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}
public String getAccessKey() {
return accessKey;
}
public String getSecretKey() {
return secretKey;
}
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public String getSecretKey() {
return secretKey;
}
public boolean valid() {
return accessKey != null && !accessKey.isEmpty() && secretKey != null && !secretKey.isEmpty();
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public boolean identical(Credentials other) {
return this == other ||
(other != null &&
(accessKey == null && other.accessKey == null || accessKey != null && accessKey.equals(other.accessKey))
&&
(secretKey == null && other.secretKey == null || secretKey != null && secretKey.equals(
other.secretKey)));
}
public String getTenantId() {
return tenantId;
}
public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}
public boolean valid() {
return accessKey != null && !accessKey.isEmpty() && secretKey != null
&& !secretKey.isEmpty();
}
public boolean identical(Credentials other) {
return this == other || (other != null
&& (accessKey == null && other.accessKey == null
|| accessKey != null && accessKey.equals(other.accessKey))
&& (secretKey == null && other.secretKey == null
|| secretKey != null && secretKey.equals(other.secretKey)));
}
}

View File

@ -0,0 +1,49 @@
/*
* Copyright (C) 2019 the original author or authors.
*
* 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;
/**
* @author pbting
* @date 2019-01-22 10:21 PM
*/
public class CredentialsValue {
private volatile String accessKey;
private volatile String secretKey;
public CredentialsValue() {
}
public CredentialsValue(String accessKey, String secretKey) {
this.accessKey = accessKey;
this.secretKey = secretKey;
}
public String getAccessKey() {
return this.accessKey;
}
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}
public String getSecretKey() {
return this.secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
}

View File

@ -48,293 +48,338 @@ import java.util.Properties;
@SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule")
public class NacosNamingService implements NamingService {
/**
* Each Naming instance should have different namespace.
*/
private String namespace;
/**
* Each Naming instance should have different namespace.
*/
private String namespace;
private String endpoint;
private String endpoint;
private String serverList;
private String serverList;
private String cacheDir;
private String cacheDir;
private String logName;
private String logName;
private HostReactor hostReactor;
private HostReactor hostReactor;
private BeatReactor beatReactor;
private BeatReactor beatReactor;
private EventDispatcher eventDispatcher;
private EventDispatcher eventDispatcher;
private NamingProxy serverProxy;
private NamingProxy serverProxy;
private void init() {
private void init() {
namespace = System.getProperty(PropertyKeyConst.NAMESPACE);
namespace = System.getProperty(PropertyKeyConst.NAMESPACE);
if (StringUtils.isEmpty(namespace)) {
namespace = UtilAndComs.DEFAULT_NAMESPACE_ID;
}
if (StringUtils.isEmpty(namespace)) {
namespace = UtilAndComs.DEFAULT_NAMESPACE_ID;
}
logName = System.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME);
if (StringUtils.isEmpty(logName)) {
logName = "naming.log";
}
logName = System.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME);
if (StringUtils.isEmpty(logName)) {
logName = "naming.log";
}
String logLevel = System.getProperty(UtilAndComs.NACOS_NAMING_LOG_LEVEL);
if (StringUtils.isEmpty(logLevel)) {
logLevel = "INFO";
}
String logLevel = System.getProperty(UtilAndComs.NACOS_NAMING_LOG_LEVEL);
if (StringUtils.isEmpty(logLevel)) {
logLevel = "INFO";
}
LogUtils.setLogLevel(logLevel);
LogUtils.setLogLevel(logLevel);
cacheDir = System.getProperty("com.alibaba.nacos.naming.cache.dir");
if (StringUtils.isEmpty(cacheDir)) {
cacheDir = System.getProperty("user.home") + "/nacos/naming/" + namespace;
}
}
cacheDir = System.getProperty("com.alibaba.nacos.naming.cache.dir");
if (StringUtils.isEmpty(cacheDir)) {
cacheDir = System.getProperty("user.home") + "/nacos/naming/" + namespace;
}
}
public NacosNamingService(String serverList) {
public NacosNamingService(String serverList) {
this.serverList = serverList;
init();
eventDispatcher = new EventDispatcher();
serverProxy = new NamingProxy(namespace, endpoint, serverList);
beatReactor = new BeatReactor(serverProxy);
hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir);
}
this.serverList = serverList;
init();
eventDispatcher = new EventDispatcher();
serverProxy = new NamingProxy(namespace, endpoint, serverList);
beatReactor = new BeatReactor(serverProxy);
hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir);
}
public NacosNamingService(Properties properties) {
public NacosNamingService(Properties properties) {
init();
init();
serverList = properties.getProperty(PropertyKeyConst.SERVER_ADDR);
serverList = properties.getProperty(PropertyKeyConst.SERVER_ADDR);
if (StringUtils.isNotEmpty(properties.getProperty(PropertyKeyConst.NAMESPACE))) {
namespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
}
if (StringUtils.isNotEmpty(properties.getProperty(PropertyKeyConst.NAMESPACE))) {
namespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
}
if (StringUtils.isNotEmpty(properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME))) {
logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME);
}
if (StringUtils
.isNotEmpty(properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME))) {
logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME);
}
if (StringUtils.isNotEmpty(properties.getProperty(PropertyKeyConst.ENDPOINT))) {
endpoint = properties.getProperty(PropertyKeyConst.ENDPOINT) + ":" +
properties.getProperty("address.server.port", "8080");
}
if (StringUtils.isNotEmpty(properties.getProperty(PropertyKeyConst.ENDPOINT))) {
endpoint = properties.getProperty(PropertyKeyConst.ENDPOINT) + ":"
+ properties.getProperty("address.server.port", "8080");
}
cacheDir = System.getProperty("user.home") + "/nacos/naming/" + namespace;
if (StringUtils
.isNotEmpty(properties.getProperty(PropertyKeyConst.WEB_CONTEXT))) {
String tmpWebContext = properties.getProperty(PropertyKeyConst.WEB_CONTEXT);
UtilAndComs.WEB_CONTEXT = tmpWebContext.indexOf("/") > -1 ? tmpWebContext
: "/" + tmpWebContext;
boolean loadCacheAtStart = false;
if (StringUtils.isNotEmpty(properties.getProperty(PropertyKeyConst.NAMING_LOAD_CACHE_AT_START))) {
loadCacheAtStart = BooleanUtils.toBoolean(
properties.getProperty(PropertyKeyConst.NAMING_LOAD_CACHE_AT_START));
}
UtilAndComs.NACOS_URL_BASE = UtilAndComs.WEB_CONTEXT + "/v1/ns";
UtilAndComs.NACOS_URL_INSTANCE = UtilAndComs.NACOS_URL_BASE + "/instance";
}
int clientBeatThreadCount = NumberUtils.toInt(properties.getProperty(PropertyKeyConst.NAMING_CLIENT_BEAT_THREAD_COUNT),
UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT);
cacheDir = System.getProperty("user.home") + "/nacos/naming/" + namespace;
int pollingThreadCount = NumberUtils.toInt(properties.getProperty(PropertyKeyConst.NAMING_POLLING_THREAD_COUNT),
UtilAndComs.DEFAULT_POLLING_THREAD_COUNT);
boolean loadCacheAtStart = false;
if (StringUtils.isNotEmpty(
properties.getProperty(PropertyKeyConst.NAMING_LOAD_CACHE_AT_START))) {
loadCacheAtStart = BooleanUtils.toBoolean(
properties.getProperty(PropertyKeyConst.NAMING_LOAD_CACHE_AT_START));
}
eventDispatcher = new EventDispatcher();
serverProxy = new NamingProxy(namespace, endpoint, serverList);
beatReactor = new BeatReactor(serverProxy, clientBeatThreadCount);
hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir, loadCacheAtStart, pollingThreadCount);
int clientBeatThreadCount = NumberUtils.toInt(
properties.getProperty(PropertyKeyConst.NAMING_CLIENT_BEAT_THREAD_COUNT),
UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT);
}
int pollingThreadCount = NumberUtils.toInt(
properties.getProperty(PropertyKeyConst.NAMING_POLLING_THREAD_COUNT),
UtilAndComs.DEFAULT_POLLING_THREAD_COUNT);
@Override
public void registerInstance(String serviceName, String ip, int port) throws NacosException {
registerInstance(serviceName, ip, port, Constants.NAMING_DEFAULT_CLUSTER_NAME);
}
eventDispatcher = new EventDispatcher();
serverProxy = new NamingProxy(namespace, endpoint, serverList);
beatReactor = new BeatReactor(serverProxy, clientBeatThreadCount);
hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir,
loadCacheAtStart, pollingThreadCount);
@Override
public void registerInstance(String serviceName, String ip, int port, String clusterName) throws NacosException {
Instance instance = new Instance();
instance.setIp(ip);
instance.setPort(port);
instance.setWeight(1.0);
instance.setClusterName(clusterName);
}
registerInstance(serviceName, instance);
}
@Override
public void registerInstance(String serviceName, String ip, int port)
throws NacosException {
registerInstance(serviceName, ip, port, Constants.NAMING_DEFAULT_CLUSTER_NAME);
}
@Override
public void registerInstance(String serviceName, Instance instance) throws NacosException {
@Override
public void registerInstance(String serviceName, String ip, int port,
String clusterName) throws NacosException {
Instance instance = new Instance();
instance.setIp(ip);
instance.setPort(port);
instance.setWeight(1.0);
instance.setClusterName(clusterName);
BeatInfo beatInfo = new BeatInfo();
beatInfo.setServiceName(serviceName);
beatInfo.setIp(instance.getIp());
beatInfo.setPort(instance.getPort());
beatInfo.setCluster(instance.getClusterName());
beatInfo.setWeight(instance.getWeight());
beatInfo.setMetadata(instance.getMetadata());
beatInfo.setScheduled(false);
registerInstance(serviceName, instance);
}
beatReactor.addBeatInfo(serviceName, beatInfo);
@Override
public void registerInstance(String serviceName, Instance instance)
throws NacosException {
serverProxy.registerService(serviceName, instance);
}
BeatInfo beatInfo = new BeatInfo();
beatInfo.setServiceName(serviceName);
beatInfo.setIp(instance.getIp());
beatInfo.setPort(instance.getPort());
beatInfo.setCluster(instance.getClusterName());
beatInfo.setWeight(instance.getWeight());
beatInfo.setMetadata(instance.getMetadata());
beatInfo.setScheduled(false);
@Override
public void deregisterInstance(String serviceName, String ip, int port) throws NacosException {
deregisterInstance(serviceName, ip, port, Constants.NAMING_DEFAULT_CLUSTER_NAME);
}
beatReactor.addBeatInfo(serviceName, beatInfo);
@Override
public void deregisterInstance(String serviceName, String ip, int port, String clusterName) throws NacosException {
beatReactor.removeBeatInfo(serviceName, ip, port);
serverProxy.deregisterService(serviceName, ip, port, clusterName);
}
serverProxy.registerService(serviceName, instance);
}
@Override
public List<Instance> getAllInstances(String serviceName) throws NacosException {
return getAllInstances(serviceName, new ArrayList<String>());
}
@Override
public void deregisterInstance(String serviceName, String ip, int port)
throws NacosException {
deregisterInstance(serviceName, ip, port, Constants.NAMING_DEFAULT_CLUSTER_NAME);
}
@Override
public List<Instance> getAllInstances(String serviceName, boolean subscribe) throws NacosException {
return getAllInstances(serviceName, new ArrayList<String>(), subscribe);
}
@Override
public void deregisterInstance(String serviceName, String ip, int port,
String clusterName) throws NacosException {
beatReactor.removeBeatInfo(serviceName, ip, port);
serverProxy.deregisterService(serviceName, ip, port, clusterName);
}
@Override
public List<Instance> getAllInstances(String serviceName, List<String> clusters) throws NacosException {
return getAllInstances(serviceName, clusters, true);
}
@Override
public List<Instance> getAllInstances(String serviceName) throws NacosException {
return getAllInstances(serviceName, new ArrayList<String>());
}
@Override
public List<Instance> getAllInstances(String serviceName, List<String> clusters, boolean subscribe) throws NacosException {
@Override
public List<Instance> getAllInstances(String serviceName, boolean subscribe)
throws NacosException {
return getAllInstances(serviceName, new ArrayList<String>(), subscribe);
}
ServiceInfo serviceInfo;
if (subscribe) {
serviceInfo = hostReactor.getServiceInfo(serviceName, StringUtils.join(clusters, ","));
} else {
serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(serviceName, StringUtils.join(clusters, ","));
}
List<Instance> list;
if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) {
return new ArrayList<Instance>();
}
return list;
}
@Override
public List<Instance> getAllInstances(String serviceName, List<String> clusters)
throws NacosException {
return getAllInstances(serviceName, clusters, true);
}
@Override
public List<Instance> selectInstances(String serviceName, boolean healthy) throws NacosException {
return selectInstances(serviceName, new ArrayList<String>(), healthy);
}
@Override
public List<Instance> getAllInstances(String serviceName, List<String> clusters,
boolean subscribe) throws NacosException {
@Override
public List<Instance> selectInstances(String serviceName, boolean healthy, boolean subscribe) throws NacosException {
return selectInstances(serviceName, new ArrayList<String>(), healthy, subscribe);
}
ServiceInfo serviceInfo;
if (subscribe) {
serviceInfo = hostReactor.getServiceInfo(serviceName,
StringUtils.join(clusters, ","));
}
else {
serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(serviceName,
StringUtils.join(clusters, ","));
}
List<Instance> list;
if (serviceInfo == null
|| CollectionUtils.isEmpty(list = serviceInfo.getHosts())) {
return new ArrayList<Instance>();
}
return list;
}
@Override
public List<Instance> selectInstances(String serviceName, List<String> clusters, boolean healthy)
throws NacosException {
return selectInstances(serviceName, clusters, healthy, true);
}
@Override
public List<Instance> selectInstances(String serviceName, boolean healthy)
throws NacosException {
return selectInstances(serviceName, new ArrayList<String>(), healthy);
}
@Override
public List<Instance> selectInstances(String serviceName, List<String> clusters, boolean healthy,
boolean subscribe) throws NacosException {
ServiceInfo serviceInfo;
if (subscribe) {
serviceInfo = hostReactor.getServiceInfo(serviceName, StringUtils.join(clusters, ","));
} else {
serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(serviceName, StringUtils.join(clusters, ","));
}
return selectInstances(serviceInfo, healthy);
}
@Override
public List<Instance> selectInstances(String serviceName, boolean healthy,
boolean subscribe) throws NacosException {
return selectInstances(serviceName, new ArrayList<String>(), healthy, subscribe);
}
@Override
public Instance selectOneHealthyInstance(String serviceName) throws NacosException {
return selectOneHealthyInstance(serviceName, new ArrayList<String>());
}
@Override
public List<Instance> selectInstances(String serviceName, List<String> clusters,
boolean healthy) throws NacosException {
return selectInstances(serviceName, clusters, healthy, true);
}
@Override
public Instance selectOneHealthyInstance(String serviceName, boolean subscribe) throws NacosException {
return selectOneHealthyInstance(serviceName, new ArrayList<String>(), subscribe);
}
@Override
public List<Instance> selectInstances(String serviceName, List<String> clusters,
boolean healthy, boolean subscribe) throws NacosException {
ServiceInfo serviceInfo;
if (subscribe) {
serviceInfo = hostReactor.getServiceInfo(serviceName,
StringUtils.join(clusters, ","));
}
else {
serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(serviceName,
StringUtils.join(clusters, ","));
}
return selectInstances(serviceInfo, healthy);
}
@Override
public Instance selectOneHealthyInstance(String serviceName, List<String> clusters) throws NacosException {
return selectOneHealthyInstance(serviceName, clusters, true);
}
@Override
public Instance selectOneHealthyInstance(String serviceName) throws NacosException {
return selectOneHealthyInstance(serviceName, new ArrayList<String>());
}
@Override
public Instance selectOneHealthyInstance(String serviceName, List<String> clusters, boolean subscribe) throws NacosException {
@Override
public Instance selectOneHealthyInstance(String serviceName, boolean subscribe)
throws NacosException {
return selectOneHealthyInstance(serviceName, new ArrayList<String>(), subscribe);
}
if (subscribe) {
return Balancer.RandomByWeight.selectHost(
hostReactor.getServiceInfo(serviceName, StringUtils.join(clusters, ",")));
} else {
return Balancer.RandomByWeight.selectHost(
hostReactor.getServiceInfoDirectlyFromServer(serviceName, StringUtils.join(clusters, ",")));
}
}
@Override
public Instance selectOneHealthyInstance(String serviceName, List<String> clusters)
throws NacosException {
return selectOneHealthyInstance(serviceName, clusters, true);
}
@Override
public void subscribe(String service, EventListener listener) {
eventDispatcher.addListener(hostReactor.getServiceInfo(service, StringUtils.EMPTY), StringUtils.EMPTY,
listener);
}
@Override
public Instance selectOneHealthyInstance(String serviceName, List<String> clusters,
boolean subscribe) throws NacosException {
@Override
public void subscribe(String service, List<String> clusters, EventListener listener) {
eventDispatcher.addListener(hostReactor.getServiceInfo(service, StringUtils.join(clusters, ",")),
StringUtils.join(clusters, ","), listener);
}
if (subscribe) {
return Balancer.RandomByWeight.selectHost(hostReactor
.getServiceInfo(serviceName, StringUtils.join(clusters, ",")));
}
else {
return Balancer.RandomByWeight
.selectHost(hostReactor.getServiceInfoDirectlyFromServer(serviceName,
StringUtils.join(clusters, ",")));
}
}
@Override
public void unsubscribe(String service, EventListener listener) {
eventDispatcher.removeListener(service, StringUtils.EMPTY, listener);
}
@Override
public void subscribe(String service, EventListener listener) {
eventDispatcher.addListener(
hostReactor.getServiceInfo(service, StringUtils.EMPTY), StringUtils.EMPTY,
listener);
}
@Override
public void unsubscribe(String service, List<String> clusters, EventListener listener) {
eventDispatcher.removeListener(service, StringUtils.join(clusters, ","), listener);
}
@Override
public void subscribe(String service, List<String> clusters, EventListener listener) {
eventDispatcher.addListener(
hostReactor.getServiceInfo(service, StringUtils.join(clusters, ",")),
StringUtils.join(clusters, ","), listener);
}
@Override
public ListView<String> getServicesOfServer(int pageNo, int pageSize) throws NacosException {
return serverProxy.getServiceList(pageNo, pageSize);
}
@Override
public void unsubscribe(String service, EventListener listener) {
eventDispatcher.removeListener(service, StringUtils.EMPTY, listener);
}
@Override
public ListView<String> getServicesOfServer(int pageNo, int pageSize, AbstractSelector selector) throws NacosException {
return serverProxy.getServiceList(pageNo, pageSize, selector);
}
@Override
public void unsubscribe(String service, List<String> clusters,
EventListener listener) {
eventDispatcher.removeListener(service, StringUtils.join(clusters, ","),
listener);
}
@Override
public List<ServiceInfo> getSubscribeServices() {
return eventDispatcher.getSubscribeServices();
}
@Override
public ListView<String> getServicesOfServer(int pageNo, int pageSize)
throws NacosException {
return serverProxy.getServiceList(pageNo, pageSize);
}
@Override
public String getServerStatus() {
return serverProxy.serverHealthy() ? "UP" : "DOWN";
}
@Override
public ListView<String> getServicesOfServer(int pageNo, int pageSize,
AbstractSelector selector) throws NacosException {
return serverProxy.getServiceList(pageNo, pageSize, selector);
}
private List<Instance> selectInstances(ServiceInfo serviceInfo, boolean healthy) {
List<Instance> list;
if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) {
return new ArrayList<Instance>();
}
@Override
public List<ServiceInfo> getSubscribeServices() {
return eventDispatcher.getSubscribeServices();
}
Iterator<Instance> iterator = list.iterator();
while (iterator.hasNext()) {
Instance instance = iterator.next();
if (healthy != instance.isHealthy() || !instance.isEnabled() || instance.getWeight() <= 0) {
iterator.remove();
}
}
@Override
public String getServerStatus() {
return serverProxy.serverHealthy() ? "UP" : "DOWN";
}
return list;
}
private List<Instance> selectInstances(ServiceInfo serviceInfo, boolean healthy) {
List<Instance> list;
if (serviceInfo == null
|| CollectionUtils.isEmpty(list = serviceInfo.getHosts())) {
return new ArrayList<Instance>();
}
public BeatReactor getBeatReactor() {
return beatReactor;
}
Iterator<Instance> iterator = list.iterator();
while (iterator.hasNext()) {
Instance instance = iterator.next();
if (healthy != instance.isHealthy() || !instance.isEnabled()
|| instance.getWeight() <= 0) {
iterator.remove();
}
}
return list;
}
public BeatReactor getBeatReactor() {
return beatReactor;
}
}

View File

@ -0,0 +1,68 @@
/*
* Copyright (C) 2019 the original author or authors.
*
* 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;
import com.alibaba.nacos.client.identify.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
/**
* @author pbting
* @date 2019-01-22 10:20 PM
*/
public class SignUtil {
public static final Charset UTF8 = Charset.forName("UTF-8");
public SignUtil() {
}
public static String sign(String data, String key) throws Exception {
try {
byte[] signature = sign(data.getBytes(UTF8), key.getBytes(UTF8),
SignUtil.SigningAlgorithm.HmacSHA1);
return new String(Base64.encodeBase64(signature));
}
catch (Exception var3) {
throw new Exception(
"Unable to calculate a request signature: " + var3.getMessage(),
var3);
}
}
private static byte[] sign(byte[] data, byte[] key,
SignUtil.SigningAlgorithm algorithm) throws Exception {
try {
Mac mac = Mac.getInstance(algorithm.toString());
mac.init(new SecretKeySpec(key, algorithm.toString()));
return mac.doFinal(data);
}
catch (Exception var4) {
throw new Exception(
"Unable to calculate a request signature: " + var4.getMessage(),
var4);
}
}
public static enum SigningAlgorithm {
// Hmac SHA1 algorithm
HmacSHA1;
private SigningAlgorithm() {
}
}
}

View File

@ -35,159 +35,187 @@ import java.util.zip.GZIPInputStream;
*/
public class HttpClient {
public static final int TIME_OUT_MILLIS = Integer.getInteger("com.alibaba.nacos.client.naming.ctimeout", 50000);
public static final int CON_TIME_OUT_MILLIS = Integer.getInteger("com.alibaba.nacos.client.naming.ctimeout", 3000);
private static final boolean ENABLE_HTTPS = Boolean.getBoolean("com.alibaba.nacos.client.naming.tls.enable");
public static final int TIME_OUT_MILLIS = Integer
.getInteger("com.alibaba.nacos.client.naming.ctimeout", 50000);
public static final int CON_TIME_OUT_MILLIS = Integer
.getInteger("com.alibaba.nacos.client.naming.ctimeout", 3000);
private static final boolean ENABLE_HTTPS = Boolean
.getBoolean("com.alibaba.nacos.client.naming.tls.enable");
static {
// limit max redirection
System.setProperty("http.maxRedirects", "5");
}
private static final String POST = "POST";
private static final String PUT = "PUT";
public static String getPrefix() {
if (ENABLE_HTTPS) {
return "https://";
}
static {
// limit max redirection
System.setProperty("http.maxRedirects", "5");
}
return "http://";
public static String getPrefix() {
if (ENABLE_HTTPS) {
return "https://";
}
}
return "http://";
public static HttpResult httpGet(String url, List<String> headers, Map<String, String> paramValues, String encoding) {
return request(url, headers, paramValues, encoding, "GET");
}
}
public static HttpResult request(String url, List<String> headers, Map<String, String> paramValues, String encoding, String method) {
HttpURLConnection conn = null;
try {
String encodedContent = encodingParams(paramValues, encoding);
url += (null == encodedContent) ? "" : ("?" + encodedContent);
public static HttpResult httpGet(String url, List<String> headers,
Map<String, String> paramValues, String encoding) {
return request(url, headers, paramValues, encoding, "GET");
}
conn = (HttpURLConnection) new URL(url).openConnection();
public static HttpResult request(String url, List<String> headers,
Map<String, String> paramValues, String encoding, String method) {
HttpURLConnection conn = null;
try {
String encodedContent = encodingParams(paramValues, encoding);
url += (null == encodedContent) ? "" : ("?" + encodedContent);
conn.setConnectTimeout(CON_TIME_OUT_MILLIS);
conn.setReadTimeout(TIME_OUT_MILLIS);
conn.setRequestMethod(method);
setHeaders(conn, headers, encoding);
conn.connect();
LogUtils.LOG.debug("Request from server: " + url);
return getResult(conn);
} catch (Exception e) {
try {
if (conn != null) {
LogUtils.LOG.warn("failed to request " + conn.getURL() + " from "
+ InetAddress.getByName(conn.getURL().getHost()).getHostAddress());
}
} catch (Exception e1) {
LogUtils.LOG.error("NA", "failed to request ", e1);
//ignore
}
conn = (HttpURLConnection) new URL(url).openConnection();
LogUtils.LOG.error("NA", "failed to request ", e);
setHeaders(conn, headers, encoding);
conn.setConnectTimeout(CON_TIME_OUT_MILLIS);
conn.setReadTimeout(TIME_OUT_MILLIS);
conn.setRequestMethod(method);
conn.setDoOutput(true);
if (POST.equals(method) || PUT.equals(method)) {
// fix: apache http nio framework must set some content to request body
byte[] b = encodedContent.getBytes();
conn.setRequestProperty("Content-Length", String.valueOf(b.length));
conn.getOutputStream().write(b, 0, b.length);
conn.getOutputStream().flush();
conn.getOutputStream().close();
}
conn.connect();
LogUtils.LOG.debug("Request from server: " + url);
return getResult(conn);
}
catch (Exception e) {
try {
if (conn != null) {
LogUtils.LOG.warn(
"failed to request " + conn.getURL() + " from " + InetAddress
.getByName(conn.getURL().getHost()).getHostAddress());
}
}
catch (Exception e1) {
LogUtils.LOG.error("NA", "failed to request ", e1);
// ignore
}
return new HttpResult(500, e.toString(), Collections.<String, String>emptyMap());
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
LogUtils.LOG.error("NA", "failed to request ", e);
private static HttpResult getResult(HttpURLConnection conn) throws IOException {
int respCode = conn.getResponseCode();
return new HttpResult(500, e.toString(),
Collections.<String, String>emptyMap());
}
finally {
if (conn != null) {
conn.disconnect();
}
}
}
InputStream inputStream;
if (HttpURLConnection.HTTP_OK == respCode
|| HttpURLConnection.HTTP_NOT_MODIFIED == respCode) {
inputStream = conn.getInputStream();
} else {
inputStream = conn.getErrorStream();
}
private static HttpResult getResult(HttpURLConnection conn) throws IOException {
int respCode = conn.getResponseCode();
Map<String, String> respHeaders = new HashMap<String, String>(conn.getHeaderFields().size());
for (Map.Entry<String, List<String>> entry : conn.getHeaderFields().entrySet()) {
respHeaders.put(entry.getKey(), entry.getValue().get(0));
}
InputStream inputStream;
if (HttpURLConnection.HTTP_OK == respCode
|| HttpURLConnection.HTTP_NOT_MODIFIED == respCode) {
inputStream = conn.getInputStream();
}
else {
inputStream = conn.getErrorStream();
}
String encodingGzip = "gzip";
Map<String, String> respHeaders = new HashMap<String, String>(
conn.getHeaderFields().size());
for (Map.Entry<String, List<String>> entry : conn.getHeaderFields().entrySet()) {
respHeaders.put(entry.getKey(), entry.getValue().get(0));
}
if (encodingGzip.equals(respHeaders.get(HttpHeaders.CONTENT_ENCODING))) {
inputStream = new GZIPInputStream(inputStream);
}
String encodingGzip = "gzip";
return new HttpResult(respCode, IoUtils.toString(inputStream, getCharset(conn)), respHeaders);
}
if (encodingGzip.equals(respHeaders.get(HttpHeaders.CONTENT_ENCODING))) {
inputStream = new GZIPInputStream(inputStream);
}
private static String getCharset(HttpURLConnection conn) {
String contentType = conn.getContentType();
if (StringUtils.isEmpty(contentType)) {
return "UTF-8";
}
return new HttpResult(respCode, IoUtils.toString(inputStream, getCharset(conn)),
respHeaders);
}
String[] values = contentType.split(";");
if (values.length == 0) {
return "UTF-8";
}
private static String getCharset(HttpURLConnection conn) {
String contentType = conn.getContentType();
if (StringUtils.isEmpty(contentType)) {
return "UTF-8";
}
String charset = "UTF-8";
for (String value : values) {
value = value.trim();
String[] values = contentType.split(";");
if (values.length == 0) {
return "UTF-8";
}
if (value.toLowerCase().startsWith("charset=")) {
charset = value.substring("charset=".length());
}
}
String charset = "UTF-8";
for (String value : values) {
value = value.trim();
return charset;
}
if (value.toLowerCase().startsWith("charset=")) {
charset = value.substring("charset=".length());
}
}
private static void setHeaders(HttpURLConnection conn, List<String> headers, String encoding) {
if (null != headers) {
for (Iterator<String> iter = headers.iterator(); iter.hasNext(); ) {
conn.addRequestProperty(iter.next(), iter.next());
}
}
return charset;
}
conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset="
+ encoding);
conn.addRequestProperty("Accept-Charset", encoding);
}
private static void setHeaders(HttpURLConnection conn, List<String> headers,
String encoding) {
if (null != headers) {
for (Iterator<String> iter = headers.iterator(); iter.hasNext();) {
conn.addRequestProperty(iter.next(), iter.next());
}
}
private static String encodingParams(Map<String, String> params, String encoding)
throws UnsupportedEncodingException {
StringBuilder sb = new StringBuilder();
if (null == params || params.isEmpty()) {
return null;
}
conn.addRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=" + encoding);
conn.addRequestProperty("Accept-Charset", encoding);
}
params.put("encoding", encoding);
private static String encodingParams(Map<String, String> params, String encoding)
throws UnsupportedEncodingException {
if (null == params) {
return null;
}
params.put("encoding", encoding);
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
if (StringUtils.isEmpty(entry.getValue())) {
continue;
}
for (Map.Entry<String, String> entry : params.entrySet()) {
if (StringUtils.isEmpty(entry.getValue())) {
continue;
}
sb.append(entry.getKey()).append("=");
sb.append(URLEncoder.encode(entry.getValue(), encoding));
sb.append("&");
}
sb.append(entry.getKey()).append("=");
sb.append(URLEncoder.encode(entry.getValue(), encoding));
sb.append("&");
}
return sb.toString();
}
if (sb.length() > 0) {
sb = sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();
}
public static class HttpResult {
final public int code;
final public String content;
final private Map<String, String> respHeaders;
public static class HttpResult {
final public int code;
final public String content;
final private Map<String, String> respHeaders;
public HttpResult(int code, String content, Map<String, String> respHeaders) {
this.code = code;
this.content = content;
this.respHeaders = respHeaders;
}
public HttpResult(int code, String content, Map<String, String> respHeaders) {
this.code = code;
this.content = content;
this.respHeaders = respHeaders;
}
public String getHeader(String name) {
return respHeaders.get(name);
}
}
public String getHeader(String name) {
return respHeaders.get(name);
}
}
}

View File

@ -25,7 +25,9 @@ import com.alibaba.nacos.api.naming.pojo.ListView;
import com.alibaba.nacos.api.selector.AbstractSelector;
import com.alibaba.nacos.api.selector.ExpressionSelector;
import com.alibaba.nacos.api.selector.SelectorType;
import com.alibaba.nacos.client.identify.CredentialService;
import com.alibaba.nacos.client.monitor.MetricsMonitor;
import com.alibaba.nacos.client.naming.SignUtil;
import com.alibaba.nacos.client.naming.beat.BeatInfo;
import com.alibaba.nacos.client.naming.utils.*;
import com.alibaba.nacos.common.util.HttpMethod;
@ -45,340 +47,398 @@ import java.util.concurrent.TimeUnit;
*/
public class NamingProxy {
private static final int DEFAULT_SERVER_PORT = 8848;
private static final int DEFAULT_SERVER_PORT = 8848;
private String namespaceId;
private String namespaceId;
private String endpoint;
private String endpoint;
private String nacosDomain;
private String nacosDomain;
private List<String> serverList;
private List<String> serverList;
private List<String> serversFromEndpoint = new ArrayList<String>();
private List<String> serversFromEndpoint = new ArrayList<String>();
private long lastSrvRefTime = 0L;
private long lastSrvRefTime = 0L;
private long vipSrvRefInterMillis = TimeUnit.SECONDS.toMillis(30);
private long vipSrvRefInterMillis = TimeUnit.SECONDS.toMillis(30);
private ScheduledExecutorService executorService;
private CredentialService credentialService = CredentialService.getInstance();
public NamingProxy(String namespaceId, String endpoint, String serverList) {
private ScheduledExecutorService executorService;
this.namespaceId = namespaceId;
this.endpoint = endpoint;
if (StringUtils.isNotEmpty(serverList)) {
this.serverList = Arrays.asList(serverList.split(","));
if (this.serverList.size() == 1) {
this.nacosDomain = serverList;
}
}
public NamingProxy(String namespaceId, String endpoint, String serverList) {
executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("com.alibaba.nacos.client.naming.serverlist.updater");
t.setDaemon(true);
return t;
}
});
this.namespaceId = namespaceId;
this.endpoint = endpoint;
if (StringUtils.isNotEmpty(serverList)) {
this.serverList = Arrays.asList(serverList.split(","));
if (this.serverList.size() == 1) {
this.nacosDomain = serverList;
}
}
executorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
refreshSrvIfNeed();
}
}, 0, vipSrvRefInterMillis, TimeUnit.MILLISECONDS);
executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("com.alibaba.nacos.client.naming.serverlist.updater");
t.setDaemon(true);
return t;
}
});
refreshSrvIfNeed();
}
executorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
refreshSrvIfNeed();
}
}, 0, vipSrvRefInterMillis, TimeUnit.MILLISECONDS);
refreshSrvIfNeed();
}
public List<String> getServerListFromEndpoint() {
try {
String urlString = "http://" + endpoint + "/nacos/serverlist";
List<String> headers = Arrays.asList("Client-Version", UtilAndComs.VERSION,
"Accept-Encoding", "gzip,deflate,sdch", "Connection", "Keep-Alive",
"RequestId", UuidUtils.generateUuid());
HttpClient.HttpResult result = HttpClient.httpGet(urlString, headers, null,
UtilAndComs.ENCODING);
if (HttpURLConnection.HTTP_OK != result.code) {
throw new IOException("Error while requesting: " + urlString
+ "'. Server returned: " + result.code);
}
String content = result.content;
List<String> list = new ArrayList<String>();
for (String line : IoUtils.readLines(new StringReader(content))) {
if (!line.trim().isEmpty()) {
list.add(line.trim());
}
}
return list;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
private void refreshSrvIfNeed() {
try {
if (!CollectionUtils.isEmpty(serverList)) {
LogUtils.LOG.debug("server list provided by user: " + serverList);
return;
}
if (System.currentTimeMillis() - lastSrvRefTime < vipSrvRefInterMillis) {
return;
}
List<String> list = getServerListFromEndpoint();
if (CollectionUtils.isEmpty(list)) {
throw new Exception("Can not acquire Nacos list");
}
if (!CollectionUtils.isEqualCollection(list, serversFromEndpoint)) {
LogUtils.LOG.info("SERVER-LIST", "server list is updated: " + list);
}
serversFromEndpoint = list;
lastSrvRefTime = System.currentTimeMillis();
}
catch (Throwable e) {
LogUtils.LOG.warn("failed to update server list", e);
}
}
public void registerService(String serviceName, Instance instance)
throws NacosException {
LogUtils.LOG.info("REGISTER-SERVICE",
"{} registering service {} with instance: {}", namespaceId, serviceName,
instance);
final Map<String, String> params = new HashMap<String, String>(8);
params.put(Constants.REQUEST_PARAM_NAMESPACE_ID, namespaceId);
params.put("ip", instance.getIp());
params.put("port", String.valueOf(instance.getPort()));
params.put("weight", String.valueOf(instance.getWeight()));
params.put("enable", String.valueOf(instance.isEnabled()));
params.put("healthy", String.valueOf(instance.isHealthy()));
params.put("metadata", JSON.toJSONString(instance.getMetadata()));
params.put("serviceName", serviceName);
params.put("clusterName", instance.getClusterName());
reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.POST);
}
private void checkTenant(Map<String, String> params) {
String tenantId = credentialService.getCredential().getTenantId();
if (tenantId == null || tenantId.trim().length() == 0) {
return;
}
try {
String tenantApp = System.getProperty("project.name");
String tenantAk = credentialService.getCredential().getAccessKey();
String tenantSK = credentialService.getCredential().getSecretKey();
String signData = getSignData(params);
String signature = SignUtil.sign(signData, tenantSK);
params.put("signature", signature);
params.put("data", signData);
params.put("ak", tenantAk);
params.put("app", tenantApp);
params.put(Constants.REQUEST_PARAM_NAMESPACE_ID, tenantId);
}
catch (Exception e) {
e.printStackTrace();
}
}
private static String getSignData(Map<String, String> params) {
String data = "";
return params.containsKey("dom")
? System.currentTimeMillis() + "@@" + (String) params.get("dom")
: String.valueOf(System.currentTimeMillis());
}
public void deregisterService(String serviceName, String ip, int port, String cluster)
throws NacosException {
LogUtils.LOG.info("DEREGISTER-SERVICE",
"{} deregistering service {} with instance: {}:{}@{}", namespaceId,
serviceName, ip, port, cluster);
final Map<String, String> params = new HashMap<String, String>(8);
params.put(Constants.REQUEST_PARAM_NAMESPACE_ID, namespaceId);
params.put("ip", ip);
params.put("port", String.valueOf(port));
params.put("serviceName", serviceName);
params.put("cluster", cluster);
reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.DELETE);
}
public String queryList(String serviceName, String clusters, int udpPort,
boolean healthyOnly) throws NacosException {
final Map<String, String> params = new HashMap<String, String>(8);
params.put(Constants.REQUEST_PARAM_NAMESPACE_ID, namespaceId);
params.put("serviceName", serviceName);
params.put("clusters", clusters);
params.put("udpPort", String.valueOf(udpPort));
params.put("clientIP", NetUtils.localIP());
params.put("healthyOnly", String.valueOf(healthyOnly));
return reqAPI(UtilAndComs.NACOS_URL_BASE + "/instance/list", params,
HttpMethod.GET);
}
public long sendBeat(BeatInfo beatInfo) {
try {
LogUtils.LOG.info("BEAT", "{} sending beat to server: {}", namespaceId,
beatInfo.toString());
Map<String, String> params = new HashMap<String, String>(4);
params.put("beat", JSON.toJSONString(beatInfo));
params.put(Constants.REQUEST_PARAM_NAMESPACE_ID, namespaceId);
params.put("serviceName", beatInfo.getServiceName());
String result = reqAPI(UtilAndComs.NACOS_URL_BASE + "/instance/beat", params,
HttpMethod.PUT);
JSONObject jsonObject = JSON.parseObject(result);
if (jsonObject != null) {
return jsonObject.getLong("clientBeatInterval");
}
}
catch (Exception e) {
LogUtils.LOG.error("CLIENT-BEAT",
"failed to send beat: " + JSON.toJSONString(beatInfo), e);
}
return 0L;
}
public boolean serverHealthy() {
try {
reqAPI(UtilAndComs.NACOS_URL_BASE + "/api/hello",
new HashMap<String, String>(2));
}
catch (Exception e) {
return false;
}
return true;
}
public ListView<String> getServiceList(int pageNo, int pageSize)
throws NacosException {
return getServiceList(pageNo, pageSize, null);
}
public ListView<String> getServiceList(int pageNo, int pageSize,
AbstractSelector selector) throws NacosException {
Map<String, String> params = new HashMap<String, String>(4);
params.put("pageNo", String.valueOf(pageNo));
params.put("pageSize", String.valueOf(pageSize));
params.put(Constants.REQUEST_PARAM_NAMESPACE_ID, namespaceId);
if (selector != null) {
switch (SelectorType.valueOf(selector.getType())) {
case none:
break;
case label:
ExpressionSelector expressionSelector = (ExpressionSelector) selector;
params.put("selector", JSON.toJSONString(expressionSelector));
break;
default:
break;
}
}
String result = reqAPI(UtilAndComs.NACOS_URL_BASE + "/service/list", params);
JSONObject json = JSON.parseObject(result);
ListView<String> listView = new ListView<String>();
listView.setCount(json.getInteger("count"));
listView.setData(JSON.parseObject(json.getString("doms"),
new TypeReference<List<String>>() {
}));
return listView;
}
public String reqAPI(String api, Map<String, String> params) throws NacosException {
List<String> snapshot = serversFromEndpoint;
if (!CollectionUtils.isEmpty(serverList)) {
snapshot = serverList;
}
return reqAPI(api, params, snapshot);
}
public String reqAPI(String api, Map<String, String> params, String method)
throws NacosException {
List<String> snapshot = serversFromEndpoint;
if (!CollectionUtils.isEmpty(serverList)) {
snapshot = serverList;
}
return reqAPI(api, params, snapshot, method);
}
public String callServer(String api, Map<String, String> params, String curServer)
throws NacosException {
return callServer(api, params, curServer, HttpMethod.GET);
}
public String callServer(String api, Map<String, String> params, String curServer,
String method) throws NacosException {
long start = System.currentTimeMillis();
long end = 0;
List<String> headers = Arrays.asList("Client-Version", UtilAndComs.VERSION,
"Accept-Encoding", "gzip,deflate,sdch", "Connection", "Keep-Alive",
"RequestId", UuidUtils.generateUuid());
String url;
if (!curServer.contains(UtilAndComs.SERVER_ADDR_IP_SPLITER)) {
curServer = curServer + UtilAndComs.SERVER_ADDR_IP_SPLITER
+ DEFAULT_SERVER_PORT;
}
url = HttpClient.getPrefix() + curServer + api;
HttpClient.HttpResult result = HttpClient.request(url, headers, params,
UtilAndComs.ENCODING, method);
end = System.currentTimeMillis();
MetricsMonitor.getNamingRequestMonitor(method, url, String.valueOf(result.code))
.record(end - start, TimeUnit.MILLISECONDS);
if (HttpURLConnection.HTTP_OK == result.code) {
return result.content;
}
if (HttpURLConnection.HTTP_NOT_MODIFIED == result.code) {
return StringUtils.EMPTY;
}
LogUtils.LOG.error("CALL-SERVER", "failed to req API:" + HttpClient.getPrefix()
+ curServer + api + ". code:" + result.code + " msg: " + result.content);
throw new NacosException(NacosException.SERVER_ERROR,
"failed to req API:" + HttpClient.getPrefix() + curServer + api
+ ". code:" + result.code + " msg: " + result.content);
}
public String reqAPI(String api, Map<String, String> params, List<String> servers) {
return reqAPI(api, params, servers, HttpMethod.GET);
}
public String reqAPI(String api, Map<String, String> params, List<String> servers,
String method) {
params.put(Constants.REQUEST_PARAM_NAMESPACE_ID, getNamespaceId());
checkTenant(params);
if (CollectionUtils.isEmpty(servers) && StringUtils.isEmpty(nacosDomain)) {
throw new IllegalArgumentException("no server available");
}
if (servers != null && !servers.isEmpty()) {
public List<String> getServerListFromEndpoint() {
Random random = new Random(System.currentTimeMillis());
int index = random.nextInt(servers.size());
try {
String urlString = "http://" + endpoint + "/nacos/serverlist";
for (int i = 0; i < servers.size(); i++) {
String server = servers.get(index);
try {
return callServer(api, params, server, method);
}
catch (Exception e) {
LogUtils.LOG.error("NA",
"req api:" + api + " failed, server(" + server, e);
}
List<String> headers = Arrays.asList("Client-Version", UtilAndComs.VERSION,
"Accept-Encoding", "gzip,deflate,sdch",
"Connection", "Keep-Alive",
"RequestId", UuidUtils.generateUuid());
index = (index + 1) % servers.size();
}
HttpClient.HttpResult result = HttpClient.httpGet(urlString, headers, null, UtilAndComs.ENCODING);
if (HttpURLConnection.HTTP_OK != result.code) {
throw new IOException("Error while requesting: " + urlString + "'. Server returned: "
+ result.code);
}
throw new IllegalStateException("failed to req API:" + api
+ " after all servers(" + servers + ") tried");
}
String content = result.content;
List<String> list = new ArrayList<String>();
for (String line : IoUtils.readLines(new StringReader(content))) {
if (!line.trim().isEmpty()) {
list.add(line.trim());
}
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private void refreshSrvIfNeed() {
try {
if (!CollectionUtils.isEmpty(serverList)) {
LogUtils.LOG.debug("server list provided by user: " + serverList);
return;
}
if (System.currentTimeMillis() - lastSrvRefTime < vipSrvRefInterMillis) {
return;
}
List<String> list = getServerListFromEndpoint();
if (CollectionUtils.isEmpty(list)) {
throw new Exception("Can not acquire Nacos list");
}
if (!CollectionUtils.isEqualCollection(list, serversFromEndpoint)) {
LogUtils.LOG.info("SERVER-LIST", "server list is updated: " + list);
}
serversFromEndpoint = list;
lastSrvRefTime = System.currentTimeMillis();
} catch (Throwable e) {
LogUtils.LOG.warn("failed to update server list", e);
}
}
public void registerService(String serviceName, Instance instance) throws NacosException {
LogUtils.LOG.info("REGISTER-SERVICE", "{} registering service {} with instance: {}",
namespaceId, serviceName, instance);
final Map<String, String> params = new HashMap<String, String>(8);
params.put(Constants.REQUEST_PARAM_NAMESPACE_ID, namespaceId);
params.put("ip", instance.getIp());
params.put("port", String.valueOf(instance.getPort()));
params.put("weight", String.valueOf(instance.getWeight()));
params.put("enable", String.valueOf(instance.isEnabled()));
params.put("healthy", String.valueOf(instance.isHealthy()));
params.put("metadata", JSON.toJSONString(instance.getMetadata()));
params.put("serviceName", serviceName);
params.put("clusterName", instance.getClusterName());
reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.POST);
}
public void deregisterService(String serviceName, String ip, int port, String cluster) throws NacosException {
LogUtils.LOG.info("DEREGISTER-SERVICE", "{} deregistering service {} with instance: {}:{}@{}",
namespaceId, serviceName, ip, port, cluster);
final Map<String, String> params = new HashMap<String, String>(8);
params.put(Constants.REQUEST_PARAM_NAMESPACE_ID, namespaceId);
params.put("ip", ip);
params.put("port", String.valueOf(port));
params.put("serviceName", serviceName);
params.put("cluster", cluster);
reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.DELETE);
}
public String queryList(String serviceName, String clusters, int udpPort, boolean healthyOnly) throws NacosException {
final Map<String, String> params = new HashMap<String, String>(8);
params.put(Constants.REQUEST_PARAM_NAMESPACE_ID, namespaceId);
params.put("serviceName", serviceName);
params.put("clusters", clusters);
params.put("udpPort", String.valueOf(udpPort));
params.put("clientIP", NetUtils.localIP());
params.put("healthyOnly", String.valueOf(healthyOnly));
return reqAPI(UtilAndComs.NACOS_URL_BASE + "/instance/list", params, HttpMethod.GET);
}
public long sendBeat(BeatInfo beatInfo) {
try {
LogUtils.LOG.info("BEAT", "{} sending beat to server: {}", namespaceId, beatInfo.toString());
Map<String, String> params = new HashMap<String, String>(4);
params.put("beat", JSON.toJSONString(beatInfo));
params.put(Constants.REQUEST_PARAM_NAMESPACE_ID, namespaceId);
params.put("serviceName", beatInfo.getServiceName());
String result = reqAPI(UtilAndComs.NACOS_URL_BASE + "/instance/beat", params, HttpMethod.PUT);
JSONObject jsonObject = JSON.parseObject(result);
if (jsonObject != null) {
return jsonObject.getLong("clientBeatInterval");
}
} catch (Exception e) {
LogUtils.LOG.error("CLIENT-BEAT", "failed to send beat: " + JSON.toJSONString(beatInfo), e);
}
return 0L;
}
public boolean serverHealthy() {
try {
reqAPI(UtilAndComs.NACOS_URL_BASE + "/api/hello", new HashMap<String, String>(2));
} catch (Exception e) {
return false;
}
return true;
}
public ListView<String> getServiceList(int pageNo, int pageSize) throws NacosException {
return getServiceList(pageNo, pageSize, null);
}
public ListView<String> getServiceList(int pageNo, int pageSize, AbstractSelector selector) throws NacosException {
Map<String, String> params = new HashMap<String, String>(4);
params.put("pageNo", String.valueOf(pageNo));
params.put("pageSize", String.valueOf(pageSize));
params.put(Constants.REQUEST_PARAM_NAMESPACE_ID, namespaceId);
if (selector != null) {
switch (SelectorType.valueOf(selector.getType())) {
case none:
break;
case label:
ExpressionSelector expressionSelector = (ExpressionSelector) selector;
params.put("selector", JSON.toJSONString(expressionSelector));
break;
default:
break;
}
}
String result = reqAPI(UtilAndComs.NACOS_URL_BASE + "/service/list", params);
JSONObject json = JSON.parseObject(result);
ListView<String> listView = new ListView<String>();
listView.setCount(json.getInteger("count"));
listView.setData(JSON.parseObject(json.getString("doms"), new TypeReference<List<String>>() {
}));
for (int i = 0; i < UtilAndComs.REQUEST_DOMAIN_RETRY_COUNT; i++) {
try {
return callServer(api, params, nacosDomain);
}
catch (Exception e) {
LogUtils.LOG.error("NA",
"req api:" + api + " failed, server(" + nacosDomain, e);
}
}
return listView;
}
throw new IllegalStateException("failed to req API:/api/" + api
+ " after all servers(" + servers + ") tried");
public String reqAPI(String api, Map<String, String> params) throws NacosException {
}
List<String> snapshot = serversFromEndpoint;
if (!CollectionUtils.isEmpty(serverList)) {
snapshot = serverList;
}
return reqAPI(api, params, snapshot);
}
public String reqAPI(String api, Map<String, String> params, String method) throws NacosException {
List<String> snapshot = serversFromEndpoint;
if (!CollectionUtils.isEmpty(serverList)) {
snapshot = serverList;
}
return reqAPI(api, params, snapshot, method);
}
public String callServer(String api, Map<String, String> params, String curServer) throws NacosException {
return callServer(api, params, curServer, HttpMethod.GET);
}
public String callServer(String api, Map<String, String> params, String curServer, String method)
throws NacosException {
long start = System.currentTimeMillis();
long end = 0;
List<String> headers = Arrays.asList("Client-Version", UtilAndComs.VERSION,
"Accept-Encoding", "gzip,deflate,sdch",
"Connection", "Keep-Alive",
"RequestId", UuidUtils.generateUuid());
String url;
if (!curServer.contains(UtilAndComs.SERVER_ADDR_IP_SPLITER)) {
curServer = curServer + UtilAndComs.SERVER_ADDR_IP_SPLITER + DEFAULT_SERVER_PORT;
}
url = HttpClient.getPrefix() + curServer + api;
HttpClient.HttpResult result = HttpClient.request(url, headers, params, UtilAndComs.ENCODING, method);
end = System.currentTimeMillis();
MetricsMonitor.getNamingRequestMonitor(method, url, String.valueOf(result.code))
.record(end - start, TimeUnit.MILLISECONDS);
if (HttpURLConnection.HTTP_OK == result.code) {
return result.content;
}
if (HttpURLConnection.HTTP_NOT_MODIFIED == result.code) {
return StringUtils.EMPTY;
}
LogUtils.LOG.error("CALL-SERVER", "failed to req API:" + HttpClient.getPrefix() + curServer
+ api + ". code:"
+ result.code + " msg: " + result.content);
throw new NacosException(NacosException.SERVER_ERROR, "failed to req API:" + HttpClient.getPrefix() + curServer
+ api + ". code:"
+ result.code + " msg: " + result.content);
}
public String reqAPI(String api, Map<String, String> params, List<String> servers) {
return reqAPI(api, params, servers, HttpMethod.GET);
}
public String reqAPI(String api, Map<String, String> params, List<String> servers, String method) {
params.put(Constants.REQUEST_PARAM_NAMESPACE_ID, getNamespaceId());
if (CollectionUtils.isEmpty(servers) && StringUtils.isEmpty(nacosDomain)) {
throw new IllegalArgumentException("no server available");
}
if (servers != null && !servers.isEmpty()) {
Random random = new Random(System.currentTimeMillis());
int index = random.nextInt(servers.size());
for (int i = 0; i < servers.size(); i++) {
String server = servers.get(index);
try {
return callServer(api, params, server, method);
} catch (Exception e) {
LogUtils.LOG.error("NA", "req api:" + api + " failed, server(" + server, e);
}
index = (index + 1) % servers.size();
}
throw new IllegalStateException("failed to req API:" + api + " after all servers(" + servers + ") tried");
}
for (int i = 0; i < UtilAndComs.REQUEST_DOMAIN_RETRY_COUNT; i++) {
try {
return callServer(api, params, nacosDomain);
} catch (Exception e) {
LogUtils.LOG.error("NA", "req api:" + api + " failed, server(" + nacosDomain, e);
}
}
throw new IllegalStateException("failed to req API:/api/" + api + " after all servers(" + servers + ") tried");
}
public String getNamespaceId() {
return namespaceId;
}
public String getNamespaceId() {
return namespaceId;
}
}

View File

@ -20,35 +20,39 @@ package com.alibaba.nacos.client.naming.utils;
*/
public class UtilAndComs {
public static final String VERSION = "Nacos-Java-Client:v0.2.1";
public static String WEB_CONTEXT = "/nacos";
public static final String ENCODING = "UTF-8";
public static String NACOS_URL_BASE = WEB_CONTEXT + "/v1/ns";
public static final String ENV_LIST_KEY = "envList";
public static String NACOS_URL_INSTANCE = NACOS_URL_BASE + "/instance";
public static final String ALL_IPS = "000--00-ALL_IPS--00--000";
public static final String VERSION = "Nacos-Java-Client:v0.2.1";
public static final String FAILOVER_SWITCH = "00-00---000-VIPSRV_FAILOVER_SWITCH-000---00-00";
public static final String ENCODING = "UTF-8";
public static final String NACOS_URL_BASE = "/nacos/v1/ns";
public static final String ENV_LIST_KEY = "envList";
public static final String NACOS_URL_INSTANCE = NACOS_URL_BASE + "/instance";
public static final String ALL_IPS = "000--00-ALL_IPS--00--000";
public static final String DEFAULT_NAMESPACE_ID = "public";
public static final String FAILOVER_SWITCH = "00-00---000-VIPSRV_FAILOVER_SWITCH-000---00-00";
public static final int REQUEST_DOMAIN_RETRY_COUNT = 3;
public static final String DEFAULT_NAMESPACE_ID = "public";
public static final String DEFAULT_NAMING_ID = "default";
public static final int REQUEST_DOMAIN_RETRY_COUNT = 3;
public static final String NACOS_NAMING_LOG_NAME = "com.alibaba.nacos.naming.log.filename";
public static final String DEFAULT_NAMING_ID = "default";
public static final String NACOS_NAMING_LOG_LEVEL = "com.alibaba.nacos.naming.log.level";
public static final String NACOS_NAMING_LOG_NAME = "com.alibaba.nacos.naming.log.filename";
public static final String SERVER_ADDR_IP_SPLITER = ":";
public static final String NACOS_NAMING_LOG_LEVEL = "com.alibaba.nacos.naming.log.level";
public static final int DEFAULT_CLIENT_BEAT_THREAD_COUNT = Runtime.getRuntime().availableProcessors() > 1 ?
Runtime.getRuntime().availableProcessors() / 2 : 1;
public static final String SERVER_ADDR_IP_SPLITER = ":";
public static final int DEFAULT_POLLING_THREAD_COUNT = Runtime.getRuntime().availableProcessors() > 1 ?
Runtime.getRuntime().availableProcessors() / 2 : 1;
public static final int DEFAULT_CLIENT_BEAT_THREAD_COUNT = Runtime.getRuntime()
.availableProcessors() > 1 ? Runtime.getRuntime().availableProcessors() / 2
: 1;
public static final int DEFAULT_POLLING_THREAD_COUNT = Runtime.getRuntime()
.availableProcessors() > 1 ? Runtime.getRuntime().availableProcessors() / 2
: 1;
}