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 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 ENCODE = "encode";
public final static String NAMING_CLIENT_BEAT_THREAD_COUNT = "namingClientBeatThreadCount";
public final static String NAMING_POLLING_THREAD_COUNT = "namingPollingThreadCount"; 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 * @author Nacos
*/ */
public class Constants { 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; package com.alibaba.nacos.client.identify;
import java.io.File; import com.alibaba.nacos.client.config.utils.LogUtils;
import java.io.FileInputStream; import com.alibaba.nacos.client.logger.Logger;
import java.io.FileNotFoundException; import com.alibaba.nacos.client.utils.StringUtils;
import java.io.IOException;
import java.io.InputStream; import java.io.*;
import java.net.URL; import java.net.URL;
import java.util.Properties; import java.util.Properties;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; 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 * Credential Watcher
* *
* @author Nacos * @author Nacos
*/ */
public class CredentialWatcher { public class CredentialWatcher {
static final public Logger SpasLogger = LogUtils.logger(CredentialWatcher.class); static final public Logger SpasLogger = LogUtils.logger(CredentialWatcher.class);
private static final long REFRESH_INTERVAL = 10 * 1000; private static final long REFRESH_INTERVAL = 10 * 1000;
private CredentialService serviceInstance; private CredentialService serviceInstance;
private String appName; private String appName;
private String propertyPath; private String propertyPath;
private TimerTask watcher; private TimerTask watcher;
private boolean stopped; private boolean stopped;
@SuppressWarnings("PMD.AvoidUseTimerRule") @SuppressWarnings("PMD.AvoidUseTimerRule")
public CredentialWatcher(String appName, CredentialService serviceInstance) { public CredentialWatcher(String appName, CredentialService serviceInstance) {
this.appName = appName; this.appName = appName;
this.serviceInstance = serviceInstance; this.serviceInstance = serviceInstance;
loadCredential(true); loadCredential(true);
watcher = new TimerTask() { watcher = new TimerTask() {
private Timer timer = new Timer(true); private Timer timer = new Timer(true);
private long modified = 0; private long modified = 0;
{ {
timer.schedule(this, REFRESH_INTERVAL, REFRESH_INTERVAL); timer.schedule(this, REFRESH_INTERVAL, REFRESH_INTERVAL);
} }
@Override @Override
public void run() { public void run() {
synchronized (this) { synchronized (this) {
if (stopped) { if (stopped) {
return; return;
} }
boolean reload = false; boolean reload = false;
if (propertyPath == null) { if (propertyPath == null) {
reload = true; reload = true;
} else { }
File file = new File(propertyPath); else {
long lastModified = file.lastModified(); File file = new File(propertyPath);
if (modified != lastModified) { long lastModified = file.lastModified();
reload = true; if (modified != lastModified) {
modified = lastModified; reload = true;
} modified = lastModified;
} }
if (reload) { }
loadCredential(false); if (reload) {
} loadCredential(false);
} }
} }
}; }
} };
}
public void stop() { public void stop() {
if (stopped) { if (stopped) {
return; return;
} }
if (watcher != null) { if (watcher != null) {
synchronized (watcher) { synchronized (watcher) {
watcher.cancel(); watcher.cancel();
stopped = true; stopped = true;
} }
} }
SpasLogger.info(appName, this.getClass().getSimpleName() + " is stopped"); SpasLogger.info(appName, this.getClass().getSimpleName() + " is stopped");
} }
private void loadCredential(boolean init) { private void loadCredential(boolean init) {
boolean logWarn = init; boolean logWarn = init;
if (propertyPath == null) { if (propertyPath == null) {
URL url = ClassLoader.getSystemResource(Constants.PROPERTIES_FILENAME); URL url = ClassLoader.getSystemResource(Constants.PROPERTIES_FILENAME);
if (url != null) { if (url != null) {
propertyPath = url.getPath(); propertyPath = url.getPath();
} }
if (propertyPath == null || propertyPath.isEmpty()) { if (propertyPath == null || propertyPath.isEmpty()) {
String value = System.getProperty("spas.identity"); String value = System.getProperty("spas.identity");
if (StringUtils.isNotEmpty(value)) { if (StringUtils.isNotEmpty(value)) {
propertyPath = value; propertyPath = value;
} }
if (propertyPath == null || propertyPath.isEmpty()) { if (propertyPath == null || propertyPath.isEmpty()) {
propertyPath = Constants.CREDENTIAL_PATH + (appName == null ? Constants.CREDENTIAL_DEFAULT propertyPath = Constants.CREDENTIAL_PATH
: appName); + (appName == null ? Constants.CREDENTIAL_DEFAULT : appName);
} else { }
if (logWarn) { else {
SpasLogger.info(appName, "Defined credential file: -D" + "spas.identity" + "=" + propertyPath); 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); }
} else {
} if (logWarn) {
} SpasLogger.info(appName, "Load credential file from classpath: "
+ Constants.PROPERTIES_FILENAME);
}
}
}
InputStream propertiesIS = null; InputStream propertiesIS = null;
do { do {
try { try {
propertiesIS = new FileInputStream(propertyPath); propertiesIS = new FileInputStream(propertyPath);
} catch (FileNotFoundException e) { }
if (appName != null && !appName.equals(Constants.CREDENTIAL_DEFAULT) && propertyPath.equals( catch (FileNotFoundException e) {
Constants.CREDENTIAL_PATH + appName)) { if (appName != null && !appName.equals(Constants.CREDENTIAL_DEFAULT)
propertyPath = Constants.CREDENTIAL_PATH + Constants.CREDENTIAL_DEFAULT; && propertyPath.equals(Constants.CREDENTIAL_PATH + appName)) {
continue; propertyPath = Constants.CREDENTIAL_PATH
} + Constants.CREDENTIAL_DEFAULT;
if (!Constants.DOCKER_CREDENTIAL_PATH.equals(propertyPath)) { continue;
propertyPath = Constants.DOCKER_CREDENTIAL_PATH; }
continue; if (!Constants.DOCKER_CREDENTIAL_PATH.equals(propertyPath)) {
} propertyPath = Constants.DOCKER_CREDENTIAL_PATH;
} continue;
break; }
} while (true); }
break;
}
while (true);
String accessKey = null; String accessKey = null;
String secretKey = null; String secretKey = null;
if (propertiesIS == null) { String tenantId = "";
propertyPath = null; if (propertiesIS == null) {
accessKey = System.getenv(Constants.ENV_ACCESS_KEY); propertyPath = null;
secretKey = System.getenv(Constants.ENV_SECRET_KEY); accessKey = System.getenv(Constants.ENV_ACCESS_KEY);
if (accessKey == null && secretKey == null) { secretKey = System.getenv(Constants.ENV_SECRET_KEY);
if (logWarn) { tenantId = System.getenv(Constants.ENV_TENANT_ID);
SpasLogger.info(appName, "No credential found"); if (accessKey == null && secretKey == null) {
} if (logWarn) {
return; SpasLogger.info(appName, "No credential found");
} }
} else { return;
Properties properties = new Properties(); }
try { }
properties.load(propertiesIS); else {
} catch (IOException e) { Properties properties = new Properties();
SpasLogger.error("26", "Unable to load credential file, appName:" + appName try {
+ "Unable to load credential file " + propertyPath, e); properties.load(propertiesIS);
propertyPath = null; }
return; catch (IOException e) {
} finally { SpasLogger.error("26", "Unable to load credential file, appName:"
try { + appName + "Unable to load credential file " + propertyPath, e);
propertiesIS.close(); propertyPath = null;
} catch (IOException e) { return;
SpasLogger.error("27", "Unable to close credential file, appName:" + appName }
+ "Unable to close credential file " + propertyPath, e); 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) { if (logWarn) {
SpasLogger.info(appName, "Load credential file " + propertyPath); SpasLogger.info(appName, "Load credential file " + propertyPath);
} }
if (!Constants.DOCKER_CREDENTIAL_PATH.equals(propertyPath)) { if (!Constants.DOCKER_CREDENTIAL_PATH.equals(propertyPath)) {
if (properties.containsKey(Constants.ACCESS_KEY)) { if (properties.containsKey(Constants.ACCESS_KEY)) {
accessKey = properties.getProperty(Constants.ACCESS_KEY); accessKey = properties.getProperty(Constants.ACCESS_KEY);
} }
if (properties.containsKey(Constants.SECRET_KEY)) { if (properties.containsKey(Constants.SECRET_KEY)) {
secretKey = properties.getProperty(Constants.SECRET_KEY); secretKey = properties.getProperty(Constants.SECRET_KEY);
} }
} else { if (properties.containsKey(Constants.TENANT_ID)) {
if (properties.containsKey(Constants.DOCKER_ACCESS_KEY)) { tenantId = properties.getProperty(Constants.TENANT_ID);
accessKey = properties.getProperty(Constants.DOCKER_ACCESS_KEY); }
} }
if (properties.containsKey(Constants.DOCKER_SECRET_KEY)) { else {
secretKey = properties.getProperty(Constants.DOCKER_SECRET_KEY); 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) { if (properties.containsKey(Constants.DOCKER_TENANT_ID)) {
accessKey = accessKey.trim(); tenantId = properties.getProperty(Constants.DOCKER_TENANT_ID);
} }
if (secretKey != null) { }
secretKey = secretKey.trim(); }
}
Credentials credential = new Credentials(accessKey, secretKey); if (accessKey != null) {
if (!credential.valid()) { accessKey = accessKey.trim();
SpasLogger.warn("1", "Credential file missing required property" + appName + "Credential file missing " }
+ Constants.ACCESS_KEY + " or " + Constants.SECRET_KEY); if (secretKey != null) {
propertyPath = null; secretKey = secretKey.trim();
// return; }
}
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 { 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) { private volatile String tenantId;
this.accessKey = accessKey;
this.secretKey = secretKey;
}
public Credentials() { public Credentials(String accessKey, String secretKey, String tenantId) {
this(null, null); this.accessKey = accessKey;
} this.secretKey = secretKey;
this.tenantId = tenantId;
}
public String getAccessKey() { public Credentials() {
return accessKey; this(null, null, null);
} }
public void setAccessKey(String accessKey) { public String getAccessKey() {
this.accessKey = accessKey; return accessKey;
} }
public String getSecretKey() { public void setAccessKey(String accessKey) {
return secretKey; this.accessKey = accessKey;
} }
public void setSecretKey(String secretKey) { public String getSecretKey() {
this.secretKey = secretKey; return secretKey;
} }
public boolean valid() { public void setSecretKey(String secretKey) {
return accessKey != null && !accessKey.isEmpty() && secretKey != null && !secretKey.isEmpty(); this.secretKey = secretKey;
} }
public boolean identical(Credentials other) { public String getTenantId() {
return this == other || return tenantId;
(other != null && }
(accessKey == null && other.accessKey == null || accessKey != null && accessKey.equals(other.accessKey))
&& public void setTenantId(String tenantId) {
(secretKey == null && other.secretKey == null || secretKey != null && secretKey.equals( this.tenantId = tenantId;
other.secretKey))); }
}
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") @SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule")
public class NacosNamingService implements NamingService { public class NacosNamingService implements NamingService {
/** /**
* Each Naming instance should have different namespace. * Each Naming instance should have different namespace.
*/ */
private String 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)) { if (StringUtils.isEmpty(namespace)) {
namespace = UtilAndComs.DEFAULT_NAMESPACE_ID; namespace = UtilAndComs.DEFAULT_NAMESPACE_ID;
} }
logName = System.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME); logName = System.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME);
if (StringUtils.isEmpty(logName)) { if (StringUtils.isEmpty(logName)) {
logName = "naming.log"; logName = "naming.log";
} }
String logLevel = System.getProperty(UtilAndComs.NACOS_NAMING_LOG_LEVEL); String logLevel = System.getProperty(UtilAndComs.NACOS_NAMING_LOG_LEVEL);
if (StringUtils.isEmpty(logLevel)) { if (StringUtils.isEmpty(logLevel)) {
logLevel = "INFO"; logLevel = "INFO";
} }
LogUtils.setLogLevel(logLevel); LogUtils.setLogLevel(logLevel);
cacheDir = System.getProperty("com.alibaba.nacos.naming.cache.dir"); cacheDir = System.getProperty("com.alibaba.nacos.naming.cache.dir");
if (StringUtils.isEmpty(cacheDir)) { if (StringUtils.isEmpty(cacheDir)) {
cacheDir = System.getProperty("user.home") + "/nacos/naming/" + namespace; cacheDir = System.getProperty("user.home") + "/nacos/naming/" + namespace;
} }
} }
public NacosNamingService(String serverList) { public NacosNamingService(String serverList) {
this.serverList = serverList; this.serverList = serverList;
init(); init();
eventDispatcher = new EventDispatcher(); eventDispatcher = new EventDispatcher();
serverProxy = new NamingProxy(namespace, endpoint, serverList); serverProxy = new NamingProxy(namespace, endpoint, serverList);
beatReactor = new BeatReactor(serverProxy); beatReactor = new BeatReactor(serverProxy);
hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir); 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))) { if (StringUtils.isNotEmpty(properties.getProperty(PropertyKeyConst.NAMESPACE))) {
namespace = properties.getProperty(PropertyKeyConst.NAMESPACE); namespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
} }
if (StringUtils.isNotEmpty(properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME))) { if (StringUtils
logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME); .isNotEmpty(properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME))) {
} logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME);
}
if (StringUtils.isNotEmpty(properties.getProperty(PropertyKeyConst.ENDPOINT))) { if (StringUtils.isNotEmpty(properties.getProperty(PropertyKeyConst.ENDPOINT))) {
endpoint = properties.getProperty(PropertyKeyConst.ENDPOINT) + ":" + endpoint = properties.getProperty(PropertyKeyConst.ENDPOINT) + ":"
properties.getProperty("address.server.port", "8080"); + 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; UtilAndComs.NACOS_URL_BASE = UtilAndComs.WEB_CONTEXT + "/v1/ns";
if (StringUtils.isNotEmpty(properties.getProperty(PropertyKeyConst.NAMING_LOAD_CACHE_AT_START))) { UtilAndComs.NACOS_URL_INSTANCE = UtilAndComs.NACOS_URL_BASE + "/instance";
loadCacheAtStart = BooleanUtils.toBoolean( }
properties.getProperty(PropertyKeyConst.NAMING_LOAD_CACHE_AT_START));
}
int clientBeatThreadCount = NumberUtils.toInt(properties.getProperty(PropertyKeyConst.NAMING_CLIENT_BEAT_THREAD_COUNT), cacheDir = System.getProperty("user.home") + "/nacos/naming/" + namespace;
UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT);
int pollingThreadCount = NumberUtils.toInt(properties.getProperty(PropertyKeyConst.NAMING_POLLING_THREAD_COUNT), boolean loadCacheAtStart = false;
UtilAndComs.DEFAULT_POLLING_THREAD_COUNT); 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(); int clientBeatThreadCount = NumberUtils.toInt(
serverProxy = new NamingProxy(namespace, endpoint, serverList); properties.getProperty(PropertyKeyConst.NAMING_CLIENT_BEAT_THREAD_COUNT),
beatReactor = new BeatReactor(serverProxy, clientBeatThreadCount); UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT);
hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir, loadCacheAtStart, pollingThreadCount);
} int pollingThreadCount = NumberUtils.toInt(
properties.getProperty(PropertyKeyConst.NAMING_POLLING_THREAD_COUNT),
UtilAndComs.DEFAULT_POLLING_THREAD_COUNT);
@Override eventDispatcher = new EventDispatcher();
public void registerInstance(String serviceName, String ip, int port) throws NacosException { serverProxy = new NamingProxy(namespace, endpoint, serverList);
registerInstance(serviceName, ip, port, Constants.NAMING_DEFAULT_CLUSTER_NAME); 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 @Override
public void registerInstance(String serviceName, Instance instance) throws NacosException { 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(); registerInstance(serviceName, instance);
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);
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 beatReactor.addBeatInfo(serviceName, beatInfo);
public void deregisterInstance(String serviceName, String ip, int port) throws NacosException {
deregisterInstance(serviceName, ip, port, Constants.NAMING_DEFAULT_CLUSTER_NAME);
}
@Override serverProxy.registerService(serviceName, instance);
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 @Override
public List<Instance> getAllInstances(String serviceName) throws NacosException { public void deregisterInstance(String serviceName, String ip, int port)
return getAllInstances(serviceName, new ArrayList<String>()); throws NacosException {
} deregisterInstance(serviceName, ip, port, Constants.NAMING_DEFAULT_CLUSTER_NAME);
}
@Override @Override
public List<Instance> getAllInstances(String serviceName, boolean subscribe) throws NacosException { public void deregisterInstance(String serviceName, String ip, int port,
return getAllInstances(serviceName, new ArrayList<String>(), subscribe); String clusterName) throws NacosException {
} beatReactor.removeBeatInfo(serviceName, ip, port);
serverProxy.deregisterService(serviceName, ip, port, clusterName);
}
@Override @Override
public List<Instance> getAllInstances(String serviceName, List<String> clusters) throws NacosException { public List<Instance> getAllInstances(String serviceName) throws NacosException {
return getAllInstances(serviceName, clusters, true); return getAllInstances(serviceName, new ArrayList<String>());
} }
@Override @Override
public List<Instance> getAllInstances(String serviceName, List<String> clusters, boolean subscribe) throws NacosException { public List<Instance> getAllInstances(String serviceName, boolean subscribe)
throws NacosException {
return getAllInstances(serviceName, new ArrayList<String>(), subscribe);
}
ServiceInfo serviceInfo; @Override
if (subscribe) { public List<Instance> getAllInstances(String serviceName, List<String> clusters)
serviceInfo = hostReactor.getServiceInfo(serviceName, StringUtils.join(clusters, ",")); throws NacosException {
} else { return getAllInstances(serviceName, clusters, true);
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 @Override
public List<Instance> selectInstances(String serviceName, boolean healthy) throws NacosException { public List<Instance> getAllInstances(String serviceName, List<String> clusters,
return selectInstances(serviceName, new ArrayList<String>(), healthy); boolean subscribe) throws NacosException {
}
@Override ServiceInfo serviceInfo;
public List<Instance> selectInstances(String serviceName, boolean healthy, boolean subscribe) throws NacosException { if (subscribe) {
return selectInstances(serviceName, new ArrayList<String>(), healthy, 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 @Override
public List<Instance> selectInstances(String serviceName, List<String> clusters, boolean healthy) public List<Instance> selectInstances(String serviceName, boolean healthy)
throws NacosException { throws NacosException {
return selectInstances(serviceName, clusters, healthy, true); return selectInstances(serviceName, new ArrayList<String>(), healthy);
} }
@Override @Override
public List<Instance> selectInstances(String serviceName, List<String> clusters, boolean healthy, public List<Instance> selectInstances(String serviceName, boolean healthy,
boolean subscribe) throws NacosException { boolean subscribe) throws NacosException {
ServiceInfo serviceInfo; return selectInstances(serviceName, new ArrayList<String>(), healthy, subscribe);
if (subscribe) { }
serviceInfo = hostReactor.getServiceInfo(serviceName, StringUtils.join(clusters, ","));
} else {
serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(serviceName, StringUtils.join(clusters, ","));
}
return selectInstances(serviceInfo, healthy);
}
@Override @Override
public Instance selectOneHealthyInstance(String serviceName) throws NacosException { public List<Instance> selectInstances(String serviceName, List<String> clusters,
return selectOneHealthyInstance(serviceName, new ArrayList<String>()); boolean healthy) throws NacosException {
} return selectInstances(serviceName, clusters, healthy, true);
}
@Override @Override
public Instance selectOneHealthyInstance(String serviceName, boolean subscribe) throws NacosException { public List<Instance> selectInstances(String serviceName, List<String> clusters,
return selectOneHealthyInstance(serviceName, new ArrayList<String>(), subscribe); 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 @Override
public Instance selectOneHealthyInstance(String serviceName, List<String> clusters) throws NacosException { public Instance selectOneHealthyInstance(String serviceName) throws NacosException {
return selectOneHealthyInstance(serviceName, clusters, true); return selectOneHealthyInstance(serviceName, new ArrayList<String>());
} }
@Override @Override
public Instance selectOneHealthyInstance(String serviceName, List<String> clusters, boolean subscribe) throws NacosException { public Instance selectOneHealthyInstance(String serviceName, boolean subscribe)
throws NacosException {
return selectOneHealthyInstance(serviceName, new ArrayList<String>(), subscribe);
}
if (subscribe) { @Override
return Balancer.RandomByWeight.selectHost( public Instance selectOneHealthyInstance(String serviceName, List<String> clusters)
hostReactor.getServiceInfo(serviceName, StringUtils.join(clusters, ","))); throws NacosException {
} else { return selectOneHealthyInstance(serviceName, clusters, true);
return Balancer.RandomByWeight.selectHost( }
hostReactor.getServiceInfoDirectlyFromServer(serviceName, StringUtils.join(clusters, ",")));
}
}
@Override @Override
public void subscribe(String service, EventListener listener) { public Instance selectOneHealthyInstance(String serviceName, List<String> clusters,
eventDispatcher.addListener(hostReactor.getServiceInfo(service, StringUtils.EMPTY), StringUtils.EMPTY, boolean subscribe) throws NacosException {
listener);
}
@Override if (subscribe) {
public void subscribe(String service, List<String> clusters, EventListener listener) { return Balancer.RandomByWeight.selectHost(hostReactor
eventDispatcher.addListener(hostReactor.getServiceInfo(service, StringUtils.join(clusters, ",")), .getServiceInfo(serviceName, StringUtils.join(clusters, ",")));
StringUtils.join(clusters, ","), listener); }
} else {
return Balancer.RandomByWeight
.selectHost(hostReactor.getServiceInfoDirectlyFromServer(serviceName,
StringUtils.join(clusters, ",")));
}
}
@Override @Override
public void unsubscribe(String service, EventListener listener) { public void subscribe(String service, EventListener listener) {
eventDispatcher.removeListener(service, StringUtils.EMPTY, listener); eventDispatcher.addListener(
} hostReactor.getServiceInfo(service, StringUtils.EMPTY), StringUtils.EMPTY,
listener);
}
@Override @Override
public void unsubscribe(String service, List<String> clusters, EventListener listener) { public void subscribe(String service, List<String> clusters, EventListener listener) {
eventDispatcher.removeListener(service, StringUtils.join(clusters, ","), listener); eventDispatcher.addListener(
} hostReactor.getServiceInfo(service, StringUtils.join(clusters, ",")),
StringUtils.join(clusters, ","), listener);
}
@Override @Override
public ListView<String> getServicesOfServer(int pageNo, int pageSize) throws NacosException { public void unsubscribe(String service, EventListener listener) {
return serverProxy.getServiceList(pageNo, pageSize); eventDispatcher.removeListener(service, StringUtils.EMPTY, listener);
} }
@Override @Override
public ListView<String> getServicesOfServer(int pageNo, int pageSize, AbstractSelector selector) throws NacosException { public void unsubscribe(String service, List<String> clusters,
return serverProxy.getServiceList(pageNo, pageSize, selector); EventListener listener) {
} eventDispatcher.removeListener(service, StringUtils.join(clusters, ","),
listener);
}
@Override @Override
public List<ServiceInfo> getSubscribeServices() { public ListView<String> getServicesOfServer(int pageNo, int pageSize)
return eventDispatcher.getSubscribeServices(); throws NacosException {
} return serverProxy.getServiceList(pageNo, pageSize);
}
@Override @Override
public String getServerStatus() { public ListView<String> getServicesOfServer(int pageNo, int pageSize,
return serverProxy.serverHealthy() ? "UP" : "DOWN"; AbstractSelector selector) throws NacosException {
} return serverProxy.getServiceList(pageNo, pageSize, selector);
}
private List<Instance> selectInstances(ServiceInfo serviceInfo, boolean healthy) { @Override
List<Instance> list; public List<ServiceInfo> getSubscribeServices() {
if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) { return eventDispatcher.getSubscribeServices();
return new ArrayList<Instance>(); }
}
Iterator<Instance> iterator = list.iterator(); @Override
while (iterator.hasNext()) { public String getServerStatus() {
Instance instance = iterator.next(); return serverProxy.serverHealthy() ? "UP" : "DOWN";
if (healthy != instance.isHealthy() || !instance.isEnabled() || instance.getWeight() <= 0) { }
iterator.remove();
}
}
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() { Iterator<Instance> iterator = list.iterator();
return beatReactor; 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 class HttpClient {
public static final int TIME_OUT_MILLIS = Integer.getInteger("com.alibaba.nacos.client.naming.ctimeout", 50000); public static final int TIME_OUT_MILLIS = Integer
public static final int CON_TIME_OUT_MILLIS = Integer.getInteger("com.alibaba.nacos.client.naming.ctimeout", 3000); .getInteger("com.alibaba.nacos.client.naming.ctimeout", 50000);
private static final boolean ENABLE_HTTPS = Boolean.getBoolean("com.alibaba.nacos.client.naming.tls.enable"); 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 { private static final String POST = "POST";
// limit max redirection private static final String PUT = "PUT";
System.setProperty("http.maxRedirects", "5");
}
public static String getPrefix() { static {
if (ENABLE_HTTPS) { // limit max redirection
return "https://"; 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) { public static HttpResult httpGet(String url, List<String> headers,
HttpURLConnection conn = null; Map<String, String> paramValues, String encoding) {
try { return request(url, headers, paramValues, encoding, "GET");
String encodedContent = encodingParams(paramValues, encoding); }
url += (null == encodedContent) ? "" : ("?" + encodedContent);
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 = (HttpURLConnection) new URL(url).openConnection();
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
}
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()); LogUtils.LOG.error("NA", "failed to request ", e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
private static HttpResult getResult(HttpURLConnection conn) throws IOException { return new HttpResult(500, e.toString(),
int respCode = conn.getResponseCode(); Collections.<String, String>emptyMap());
}
finally {
if (conn != null) {
conn.disconnect();
}
}
}
InputStream inputStream; private static HttpResult getResult(HttpURLConnection conn) throws IOException {
if (HttpURLConnection.HTTP_OK == respCode int respCode = conn.getResponseCode();
|| HttpURLConnection.HTTP_NOT_MODIFIED == respCode) {
inputStream = conn.getInputStream();
} else {
inputStream = conn.getErrorStream();
}
Map<String, String> respHeaders = new HashMap<String, String>(conn.getHeaderFields().size()); InputStream inputStream;
for (Map.Entry<String, List<String>> entry : conn.getHeaderFields().entrySet()) { if (HttpURLConnection.HTTP_OK == respCode
respHeaders.put(entry.getKey(), entry.getValue().get(0)); || 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))) { String encodingGzip = "gzip";
inputStream = new GZIPInputStream(inputStream);
}
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) { return new HttpResult(respCode, IoUtils.toString(inputStream, getCharset(conn)),
String contentType = conn.getContentType(); respHeaders);
if (StringUtils.isEmpty(contentType)) { }
return "UTF-8";
}
String[] values = contentType.split(";"); private static String getCharset(HttpURLConnection conn) {
if (values.length == 0) { String contentType = conn.getContentType();
return "UTF-8"; if (StringUtils.isEmpty(contentType)) {
} return "UTF-8";
}
String charset = "UTF-8"; String[] values = contentType.split(";");
for (String value : values) { if (values.length == 0) {
value = value.trim(); return "UTF-8";
}
if (value.toLowerCase().startsWith("charset=")) { String charset = "UTF-8";
charset = value.substring("charset=".length()); 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) { return charset;
if (null != headers) { }
for (Iterator<String> iter = headers.iterator(); iter.hasNext(); ) {
conn.addRequestProperty(iter.next(), iter.next());
}
}
conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" private static void setHeaders(HttpURLConnection conn, List<String> headers,
+ encoding); String encoding) {
conn.addRequestProperty("Accept-Charset", 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) conn.addRequestProperty("Content-Type",
throws UnsupportedEncodingException { "application/x-www-form-urlencoded;charset=" + encoding);
StringBuilder sb = new StringBuilder(); conn.addRequestProperty("Accept-Charset", encoding);
if (null == params || params.isEmpty()) { }
return null;
}
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()) { for (Map.Entry<String, String> entry : params.entrySet()) {
if (StringUtils.isEmpty(entry.getValue())) { if (StringUtils.isEmpty(entry.getValue())) {
continue; continue;
} }
sb.append(entry.getKey()).append("="); sb.append(entry.getKey()).append("=");
sb.append(URLEncoder.encode(entry.getValue(), encoding)); sb.append(URLEncoder.encode(entry.getValue(), encoding));
sb.append("&"); sb.append("&");
} }
return sb.toString(); if (sb.length() > 0) {
} sb = sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();
}
public static class HttpResult { public static class HttpResult {
final public int code; final public int code;
final public String content; final public String content;
final private Map<String, String> respHeaders; final private Map<String, String> respHeaders;
public HttpResult(int code, String content, Map<String, String> respHeaders) { public HttpResult(int code, String content, Map<String, String> respHeaders) {
this.code = code; this.code = code;
this.content = content; this.content = content;
this.respHeaders = respHeaders; this.respHeaders = respHeaders;
} }
public String getHeader(String name) { public String getHeader(String name) {
return respHeaders.get(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.AbstractSelector;
import com.alibaba.nacos.api.selector.ExpressionSelector; import com.alibaba.nacos.api.selector.ExpressionSelector;
import com.alibaba.nacos.api.selector.SelectorType; 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.monitor.MetricsMonitor;
import com.alibaba.nacos.client.naming.SignUtil;
import com.alibaba.nacos.client.naming.beat.BeatInfo; import com.alibaba.nacos.client.naming.beat.BeatInfo;
import com.alibaba.nacos.client.naming.utils.*; import com.alibaba.nacos.client.naming.utils.*;
import com.alibaba.nacos.common.util.HttpMethod; import com.alibaba.nacos.common.util.HttpMethod;
@ -45,340 +47,398 @@ import java.util.concurrent.TimeUnit;
*/ */
public class NamingProxy { 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; public NamingProxy(String namespaceId, String endpoint, String serverList) {
this.endpoint = endpoint;
if (StringUtils.isNotEmpty(serverList)) {
this.serverList = Arrays.asList(serverList.split(","));
if (this.serverList.size() == 1) {
this.nacosDomain = serverList;
}
}
executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() { this.namespaceId = namespaceId;
@Override this.endpoint = endpoint;
public Thread newThread(Runnable r) { if (StringUtils.isNotEmpty(serverList)) {
Thread t = new Thread(r); this.serverList = Arrays.asList(serverList.split(","));
t.setName("com.alibaba.nacos.client.naming.serverlist.updater"); if (this.serverList.size() == 1) {
t.setDaemon(true); this.nacosDomain = serverList;
return t; }
} }
});
executorService.scheduleWithFixedDelay(new Runnable() { executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
@Override @Override
public void run() { public Thread newThread(Runnable r) {
refreshSrvIfNeed(); Thread t = new Thread(r);
} t.setName("com.alibaba.nacos.client.naming.serverlist.updater");
}, 0, vipSrvRefInterMillis, TimeUnit.MILLISECONDS); 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 { for (int i = 0; i < servers.size(); i++) {
String urlString = "http://" + endpoint + "/nacos/serverlist"; 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, index = (index + 1) % servers.size();
"Accept-Encoding", "gzip,deflate,sdch", }
"Connection", "Keep-Alive",
"RequestId", UuidUtils.generateUuid());
HttpClient.HttpResult result = HttpClient.httpGet(urlString, headers, null, UtilAndComs.ENCODING); throw new IllegalStateException("failed to req API:" + api
if (HttpURLConnection.HTTP_OK != result.code) { + " after all servers(" + servers + ") tried");
throw new IOException("Error while requesting: " + urlString + "'. Server returned: " }
+ result.code);
}
String content = result.content; for (int i = 0; i < UtilAndComs.REQUEST_DOMAIN_RETRY_COUNT; i++) {
List<String> list = new ArrayList<String>(); try {
for (String line : IoUtils.readLines(new StringReader(content))) { return callServer(api, params, nacosDomain);
if (!line.trim().isEmpty()) { }
list.add(line.trim()); catch (Exception e) {
} LogUtils.LOG.error("NA",
} "req api:" + api + " failed, server(" + nacosDomain, e);
}
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>>() {
}));
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 { }
public String getNamespaceId() {
List<String> snapshot = serversFromEndpoint; return namespaceId;
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;
}
} }

View File

@ -20,35 +20,39 @@ package com.alibaba.nacos.client.naming.utils;
*/ */
public class UtilAndComs { 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 ? public static final String SERVER_ADDR_IP_SPLITER = ":";
Runtime.getRuntime().availableProcessors() / 2 : 1;
public static final int DEFAULT_POLLING_THREAD_COUNT = Runtime.getRuntime().availableProcessors() > 1 ? public static final int DEFAULT_CLIENT_BEAT_THREAD_COUNT = Runtime.getRuntime()
Runtime.getRuntime().availableProcessors() / 2 : 1; .availableProcessors() > 1 ? Runtime.getRuntime().availableProcessors() / 2
: 1;
public static final int DEFAULT_POLLING_THREAD_COUNT = Runtime.getRuntime()
.availableProcessors() > 1 ? Runtime.getRuntime().availableProcessors() / 2
: 1;
} }