Merge branch 'feature_naming_group' of https://github.com/alibaba/nacos into feature_naming_group

This commit is contained in:
xiaochun.xxc 2019-03-07 21:37:52 +08:00
commit 895431d8dc
45 changed files with 1037 additions and 583 deletions

View File

@ -24,6 +24,8 @@ public class PropertyKeyConst {
public final static String ENDPOINT = "endpoint";
public final static String ENDPOINT_PORT = "endpointPort";
public final static String NAMESPACE = "namespace";
public final static String ACCESS_KEY = "accessKey";
@ -39,7 +41,17 @@ public class PropertyKeyConst {
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";
public static class SystemEnv {
public static final String ALIBABA_ALIWARE_ENDPOINT_PORT = "ALIBABA_ALIWARE_ENDPOINT_PORT";
public static final String ALIBABA_ALIWARE_NAMESPACE = "ALIBABA_ALIWARE_NAMESPACE";
public static final String ALIBABA_ALIWARE_ENDPOINT_URL = "ALIBABA_ALIWARE_ENDPOINT_URL";
}
}

View File

@ -0,0 +1,31 @@
/*
* 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.api;
/**
* Properties that are preferred to which in {@link PropertyKeyConst}
*
* @author pbting
* @date 2019-02-22 3:38 PM
*/
public interface SystemPropertyKeyConst {
String NAMING_SERVER_PORT = "nacos.naming.exposed.port";
String NAMING_WEB_CONTEXT = "nacos.naming.web.context";
String NACOS_NAMING_REQUEST_MODULE = "nacos.naming.request.module";
}

View File

@ -126,4 +126,6 @@ public class Constants {
public static final int WRITE_REDIRECT_CODE = 307;
public static final String SERVICE_INFO_SPLITER = "@@";
public static final String NULL_STRING = "null";
}

View File

@ -34,6 +34,7 @@ import com.alibaba.nacos.client.config.utils.ParamUtils;
import com.alibaba.nacos.client.config.utils.TenantUtil;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.StringUtils;
import com.alibaba.nacos.client.utils.TemplateUtils;
import org.slf4j.Logger;
import java.io.IOException;
@ -42,6 +43,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Callable;
/**
* Config Impl
@ -73,19 +75,33 @@ public class NacosConfigService implements ConfigService {
} else {
encode = encodeTmp.trim();
}
String namespaceTmp = properties.getProperty(PropertyKeyConst.NAMESPACE);
if (StringUtils.isBlank(namespaceTmp)) {
namespace = TenantUtil.getUserTenant();
properties.put(PropertyKeyConst.NAMESPACE, namespace);
} else {
namespace = namespaceTmp;
properties.put(PropertyKeyConst.NAMESPACE, namespace);
}
initNamespace(properties);
agent = new MetricsHttpAgent(new ServerHttpAgent(properties));
agent.start();
worker = new ClientWorker(agent, configFilterChainManager);
}
private void initNamespace(Properties properties) {
String namespaceTmp = properties.getProperty(PropertyKeyConst.NAMESPACE);
namespaceTmp = TemplateUtils.stringBlankAndThenExecute(namespaceTmp, new Callable<String>() {
@Override
public String call() {
return TenantUtil.getUserTenant();
}
});
namespaceTmp = TemplateUtils.stringBlankAndThenExecute(namespaceTmp, new Callable<String>() {
@Override
public String call() {
String namespace = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_NAMESPACE);
return StringUtils.isNotBlank(namespace) ? namespace : "";
}
});
namespace = namespaceTmp;
properties.put(PropertyKeyConst.NAMESPACE, namespace);
}
@Override
public String getConfig(String dataId, String group, long timeoutMs) throws NacosException {
return getConfigInner(namespace, dataId, group, timeoutMs);

View File

@ -24,6 +24,7 @@ import com.alibaba.nacos.client.config.impl.ServerListManager;
import com.alibaba.nacos.client.config.impl.SpasAdapter;
import com.alibaba.nacos.client.config.utils.IOUtils;
import com.alibaba.nacos.client.identify.STSConfig;
import com.alibaba.nacos.client.utils.TemplateUtils;
import com.alibaba.nacos.client.utils.JSONUtils;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
@ -41,6 +42,7 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Callable;
/**
* Server Agent
@ -197,29 +199,29 @@ public class ServerHttpAgent implements HttpAgent {
public ServerHttpAgent(ServerListManager mgr, Properties properties) {
serverListMgr = mgr;
String ak = properties.getProperty(PropertyKeyConst.ACCESS_KEY);
if (StringUtils.isBlank(ak)) {
accessKey = SpasAdapter.getAk();
} else {
accessKey = ak;
}
String sk = properties.getProperty(PropertyKeyConst.SECRET_KEY);
if (StringUtils.isBlank(sk)) {
secretKey = SpasAdapter.getSk();
} else {
secretKey = sk;
}
init(properties);
}
public ServerHttpAgent(Properties properties) throws NacosException {
String encodeTmp = properties.getProperty(PropertyKeyConst.ENCODE);
if (StringUtils.isBlank(encodeTmp)) {
encode = Constants.ENCODE;
} else {
encode = encodeTmp.trim();
}
serverListMgr = new ServerListManager(properties);
init(properties);
}
private void init(Properties properties) {
initEncode(properties);
initAkSk(properties);
}
private void initEncode(Properties properties) {
encode = TemplateUtils.stringEmptyAndThenExecute(properties.getProperty(PropertyKeyConst.ENCODE), new Callable<String>() {
@Override
public String call() throws Exception {
return Constants.ENCODE;
}
});
}
private void initAkSk(Properties properties) {
String ak = properties.getProperty(PropertyKeyConst.ACCESS_KEY);
if (StringUtils.isBlank(ak)) {
accessKey = SpasAdapter.getAk();
@ -273,8 +275,9 @@ public class ServerHttpAgent implements HttpAgent {
}
}
String stsResponse = getSTSResponse();
STSCredential stsCredentialTmp = (STSCredential)JSONUtils.deserializeObject(stsResponse,
new TypeReference<STSCredential>() {});
STSCredential stsCredentialTmp = (STSCredential) JSONUtils.deserializeObject(stsResponse,
new TypeReference<STSCredential>() {
});
sTSCredential = stsCredentialTmp;
LOGGER.info("[getSTSCredential] code:{}, accessKeyId:{}, lastUpdated:{}, expiration:{}", sTSCredential.getCode(),
sTSCredential.getAccessKeyId(), sTSCredential.getLastUpdated(), sTSCredential.getExpiration());
@ -291,7 +294,7 @@ public class ServerHttpAgent implements HttpAgent {
int respCode;
String response;
try {
conn = (HttpURLConnection)new URL(securityCredentialsUrl).openConnection();
conn = (HttpURLConnection) new URL(securityCredentialsUrl).openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(ParamUtil.getConnectTimeout() > 100 ? ParamUtil.getConnectTimeout() : 100);
conn.setReadTimeout(1000);

View File

@ -20,22 +20,15 @@ import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.impl.EventDispatcher.ServerlistChangeEvent;
import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult;
import com.alibaba.nacos.client.config.utils.IOUtils;
import com.alibaba.nacos.client.utils.EnvUtil;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.client.utils.StringUtils;
import com.alibaba.nacos.client.utils.*;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.slf4j.Logger;
import java.io.IOException;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
/**
@ -93,6 +86,8 @@ public class ServerListManager {
public ServerListManager(String endpoint, String namespace) throws NacosException {
isFixed = false;
isStarted = false;
endpoint = initEndpoint(endpoint);
if (StringUtils.isBlank(endpoint)) {
throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "endpoint is blank");
}
@ -158,10 +153,8 @@ public class ServerListManager {
}
private void initParam(Properties properties) {
String endpointTmp = properties.getProperty(PropertyKeyConst.ENDPOINT);
if (!StringUtils.isBlank(endpointTmp)) {
endpoint = endpointTmp;
}
endpoint = initEndpoint(properties.getProperty(PropertyKeyConst.ENDPOINT));
String contentPathTmp = properties.getProperty(PropertyKeyConst.CONTEXT_PATH);
if (!StringUtils.isBlank(contentPathTmp)) {
contentPath = contentPathTmp;
@ -172,6 +165,21 @@ public class ServerListManager {
}
}
private String initEndpoint(String endpointTmp) {
String endpointPortTmp = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT);
if (StringUtils.isNotBlank(endpointPortTmp)) {
endpointPort = Integer.parseInt(endpointPortTmp);
}
return TemplateUtils.stringBlankAndThenExecute(endpointTmp, new Callable<String>() {
@Override
public String call() {
String endpointUrl = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_URL);
return StringUtils.isNotBlank(endpointUrl) ? endpointUrl : "";
}
});
}
public synchronized void start() throws NacosException {
if (isStarted || isFixed) {

View File

@ -25,6 +25,8 @@ public class Constants {
public static final String SECRET_KEY = "secretKey";
public static final String TENANT_ID = "tenantId";
public static final String PROPERTIES_FILENAME = "spas.properties";
public static final String CREDENTIAL_PATH = "/home/admin/.spas_key/";
@ -37,10 +39,14 @@ public class Constants {
public static final String DOCKER_SECRET_KEY = "env_spas_secretKey";
public static final String DOCKER_TENANT_ID = "ebv_spas_tenantId";
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

@ -19,11 +19,7 @@ import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.StringUtils;
import org.slf4j.Logger;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.URL;
import java.util.Properties;
import java.util.Timer;
@ -144,6 +140,7 @@ public class CredentialWatcher {
String accessKey = null;
String secretKey = null;
String tenantId = null;
if (propertiesIS == null) {
propertyPath = null;
accessKey = System.getenv(Constants.ENV_ACCESS_KEY);
@ -183,6 +180,9 @@ public class CredentialWatcher {
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);
@ -190,6 +190,10 @@ public class CredentialWatcher {
if (properties.containsKey(Constants.DOCKER_SECRET_KEY)) {
secretKey = properties.getProperty(Constants.DOCKER_SECRET_KEY);
}
if (properties.containsKey(Constants.DOCKER_TENANT_ID)) {
tenantId = properties.getProperty(Constants.DOCKER_TENANT_ID);
}
}
}
@ -200,7 +204,11 @@ public class CredentialWatcher {
secretKey = secretKey.trim();
}
Credentials credential = new Credentials(accessKey, secretKey);
if (tenantId != null) {
tenantId = tenantId.trim();
}
Credentials credential = new Credentials(accessKey, secretKey, tenantId);
if (!credential.valid()) {
SpasLogger.warn("[1] Credential file missing required property {} Credential file missing {} or {}",
appName, Constants.ACCESS_KEY, Constants.SECRET_KEY);

View File

@ -26,13 +26,16 @@ public class Credentials implements SpasCredential {
private volatile String secretKey;
public Credentials(String accessKey, String secretKey) {
private volatile String tenantId;
public Credentials(String accessKey, String secretKey, String tenantId) {
this.accessKey = accessKey;
this.secretKey = secretKey;
this.tenantId = tenantId;
}
public Credentials() {
this(null, null);
this(null, null, null);
}
public String getAccessKey() {
@ -51,16 +54,24 @@ public class Credentials implements SpasCredential {
this.secretKey = 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();
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)));
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

@ -26,16 +26,22 @@ import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.composite.CompositeConfiguration;
import org.apache.logging.log4j.core.lookup.Interpolator;
import org.apache.logging.log4j.core.lookup.StrSubstitutor;
import org.apache.logging.log4j.util.PropertiesUtil;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.apache.logging.log4j.core.config.ConfigurationFactory.CONFIGURATION_FILE_PROPERTY;
/**
* Support for Log4j version 2.7 or higher
*
@ -52,6 +58,8 @@ public class Log4J2NacosLogging extends AbstractNacosLogging {
private static final String JSON_PARSER_CLASS_NAME = "com.fasterxml.jackson.databind.ObjectMapper";
private final StrSubstitutor strSubstitutor = new StrSubstitutor(new Interpolator());
private Set<String> locationList = new HashSet<String>();
public Log4J2NacosLogging() {
@ -63,9 +71,13 @@ public class Log4J2NacosLogging extends AbstractNacosLogging {
@Override
public void loadConfiguration() {
String config = findConfig(getCurrentlySupportedConfigLocations());
if (config != null) {
locationList.add(config);
if (locationList.isEmpty()) {
return;
}
List<String> configList = findConfig(getCurrentlySupportedConfigLocations());
if (configList != null) {
locationList.addAll(configList);
}
final List<AbstractConfiguration> configurations = new ArrayList<AbstractConfiguration>();
@ -112,22 +124,35 @@ public class Log4J2NacosLogging extends AbstractNacosLogging {
List<String> supportedConfigLocations = new ArrayList<String>();
if (ClassUtils.isPresent(YAML_PARSER_CLASS_NAME)) {
Collections.addAll(supportedConfigLocations, "log4j2.yaml", "log4j2.yml");
Collections.addAll(supportedConfigLocations, "log4j2.yaml", "log4j2.yml", "log4j2-test.yaml",
"log4j2-test.yml");
}
if (ClassUtils.isPresent(JSON_PARSER_CLASS_NAME)) {
Collections.addAll(supportedConfigLocations, "log4j2.json", "log4j2.jsn");
Collections.addAll(supportedConfigLocations, "log4j2.json", "log4j2.jsn", "log4j2-test.json",
"log4j2-test.jsn");
}
supportedConfigLocations.add("log4j2.xml");
supportedConfigLocations.add("log4j2-test.xml");
return supportedConfigLocations.toArray(new String[supportedConfigLocations.size()]);
}
private String findConfig(String[] locations) {
private List<String> findConfig(String[] locations) {
final String configLocationStr = this.strSubstitutor.replace(PropertiesUtil.getProperties()
.getStringProperty(CONFIGURATION_FILE_PROPERTY));
if (configLocationStr != null) {
return Arrays.asList(configLocationStr.split(","));
}
for (String location : locations) {
ClassLoader defaultClassLoader = ClassUtils.getDefaultClassLoader();
if (defaultClassLoader != null && defaultClassLoader.getResource(location) != null) {
return "classpath:" + location;
List<String> list = new ArrayList<String>();
list.add("classpath:" + location);
return list;
}
}
return null;

View File

@ -16,6 +16,7 @@
package com.alibaba.nacos.client.naming;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.SystemPropertyKeyConst;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
@ -24,6 +25,7 @@ import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.api.selector.AbstractSelector;
import com.alibaba.nacos.client.identify.CredentialService;
import com.alibaba.nacos.client.naming.beat.BeatInfo;
import com.alibaba.nacos.client.naming.beat.BeatReactor;
import com.alibaba.nacos.client.naming.core.Balancer;
@ -33,6 +35,8 @@ import com.alibaba.nacos.client.naming.net.NamingProxy;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.StringUtils;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.TemplateUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.math.NumberUtils;
@ -40,13 +44,13 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Callable;
/**
* @author nkorange
*/
@SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule")
public class NacosNamingService implements NamingService {
/**
* Each Naming instance should have different namespace.
*/
@ -68,74 +72,179 @@ public class NacosNamingService implements NamingService {
private NamingProxy serverProxy;
private void init() {
public NacosNamingService(String serverList) {
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverList);
namespace = System.getProperty(PropertyKeyConst.NAMESPACE);
init(properties);
}
if (StringUtils.isEmpty(namespace)) {
namespace = UtilAndComs.DEFAULT_NAMESPACE_ID;
public NacosNamingService(Properties properties) {
init(properties);
}
private void init(Properties properties) {
serverList = properties.getProperty(PropertyKeyConst.SERVER_ADDR);
initNamespace(properties);
initEndpoint(properties);
initWebRootContext();
initCacheDir();
initLogName(properties);
eventDispatcher = new EventDispatcher();
serverProxy = new NamingProxy(namespace, endpoint, serverList);
serverProxy.setProperties(properties);
beatReactor = new BeatReactor(serverProxy, initClientBeatThreadCount(properties));
hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir, isLoadCacheAtStart(properties), initPollingThreadCount(properties));
}
private int initClientBeatThreadCount(Properties properties) {
if (properties == null) {
return UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT;
}
int clientBeatThreadCount = NumberUtils.toInt(properties.getProperty(PropertyKeyConst.NAMING_CLIENT_BEAT_THREAD_COUNT),
UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT);
return clientBeatThreadCount;
}
private int initPollingThreadCount(Properties properties) {
if (properties == null) {
return UtilAndComs.DEFAULT_POLLING_THREAD_COUNT;
}
int pollingThreadCount = NumberUtils.toInt(properties.getProperty(PropertyKeyConst.NAMING_POLLING_THREAD_COUNT),
UtilAndComs.DEFAULT_POLLING_THREAD_COUNT);
return pollingThreadCount;
}
private boolean isLoadCacheAtStart(Properties properties) {
boolean loadCacheAtStart = false;
if (properties != null && StringUtils.isNotEmpty(properties.getProperty(PropertyKeyConst.NAMING_LOAD_CACHE_AT_START))) {
loadCacheAtStart = BooleanUtils.toBoolean(
properties.getProperty(PropertyKeyConst.NAMING_LOAD_CACHE_AT_START));
}
return loadCacheAtStart;
}
private void initLogName(Properties properties) {
logName = System.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME);
if (StringUtils.isEmpty(logName)) {
logName = "naming.log";
}
if (properties != null && StringUtils.isNotEmpty(properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME))) {
logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME);
} else {
logName = "naming.log";
}
}
}
private void initCacheDir() {
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) {
private void initEndpoint(Properties properties) {
if (properties == null) {
this.serverList = serverList;
init();
eventDispatcher = new EventDispatcher();
serverProxy = new NamingProxy(namespace, endpoint, serverList);
beatReactor = new BeatReactor(serverProxy);
hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir);
return;
}
String endpointUrl = TemplateUtils.stringEmptyAndThenExecute(properties.getProperty(PropertyKeyConst.ENDPOINT), new Callable<String>() {
@Override
public String call() {
return System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_URL);
}
});
if (com.alibaba.nacos.client.utils.StringUtils.isNotBlank(endpointUrl)) {
return;
}
String endpointPort = TemplateUtils.stringEmptyAndThenExecute(properties.getProperty(PropertyKeyConst.ENDPOINT_PORT), new Callable<String>() {
@Override
public String call() {
return System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT);
}
});
endpointPort = TemplateUtils.stringEmptyAndThenExecute(endpointPort, new Callable<String>() {
@Override
public String call() {
return "8080";
}
});
endpoint = endpointUrl + ":" + endpointPort;
}
public NacosNamingService(Properties properties) {
private void initNamespace(Properties properties) {
String tmpNamespace = null;
init();
serverList = properties.getProperty(PropertyKeyConst.SERVER_ADDR);
if (StringUtils.isNotEmpty(properties.getProperty(PropertyKeyConst.NAMESPACE))) {
namespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
if (properties != null) {
tmpNamespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
}
if (StringUtils.isNotEmpty(properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME))) {
logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME);
}
tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable<String>() {
@Override
public String call() {
String namespace = System.getProperty(PropertyKeyConst.NAMESPACE);
LogUtils.NAMING_LOGGER.info("initializer namespace from System Property :" + namespace);
return namespace;
}
});
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;
tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable<String>() {
@Override
public String call() {
String namespace = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_NAMESPACE);
LogUtils.NAMING_LOGGER.info("initializer namespace from System Environment :" + namespace);
return namespace;
}
});
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));
}
tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable<String>() {
@Override
public String call() {
String namespace = CredentialService.getInstance().getCredential().getTenantId();
LogUtils.NAMING_LOGGER.info("initializer namespace from Credential Module " + namespace);
return namespace;
}
});
int clientBeatThreadCount = NumberUtils.toInt(
properties.getProperty(PropertyKeyConst.NAMING_CLIENT_BEAT_THREAD_COUNT),
UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT);
tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable<String>() {
@Override
public String call() {
return UtilAndComs.DEFAULT_NAMESPACE_ID;
}
});
namespace = tmpNamespace;
}
int pollingThreadCount = NumberUtils.toInt(properties.getProperty(PropertyKeyConst.NAMING_POLLING_THREAD_COUNT),
UtilAndComs.DEFAULT_POLLING_THREAD_COUNT);
eventDispatcher = new EventDispatcher();
serverProxy = new NamingProxy(namespace, endpoint, serverList);
beatReactor = new BeatReactor(serverProxy, clientBeatThreadCount);
hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir, loadCacheAtStart, pollingThreadCount);
private void initWebRootContext() {
// support the web context with ali-yun if the app deploy by EDAS
final String webContext = System.getProperty(SystemPropertyKeyConst.NAMING_WEB_CONTEXT);
TemplateUtils.stringNotEmptyAndThenExecute(webContext, new Runnable() {
@Override
public void run() {
UtilAndComs.WEB_CONTEXT = webContext.indexOf("/") > -1 ? webContext
: "/" + webContext;
UtilAndComs.NACOS_URL_BASE = UtilAndComs.WEB_CONTEXT + "/v1/ns";
UtilAndComs.NACOS_URL_INSTANCE = UtilAndComs.NACOS_URL_BASE + "/instance";
}
});
}
@Override

View File

@ -30,7 +30,7 @@ public class BeatInfo {
private String serviceName;
private String cluster;
private Map<String, String> metadata;
private boolean scheduled;
private volatile boolean scheduled;
@Override
public String toString() {

View File

@ -30,16 +30,22 @@ import java.net.URLEncoder;
import java.util.*;
import java.util.zip.GZIPInputStream;
import static com.alibaba.nacos.client.utils.LogUtils.*;
import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER;
/**
* @author nkorange
*/
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");
private static final String POST = "POST";
private static final String PUT = "PUT";
static {
// limit max redirection
@ -67,10 +73,19 @@ public class HttpClient {
conn = (HttpURLConnection) new URL(url).openConnection();
setHeaders(conn, headers, encoding);
conn.setConnectTimeout(CON_TIME_OUT_MILLIS);
conn.setReadTimeout(TIME_OUT_MILLIS);
conn.setRequestMethod(method);
setHeaders(conn, headers, encoding);
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();
NAMING_LOGGER.debug("Request from server: " + url);
return getResult(conn);
@ -78,7 +93,7 @@ public class HttpClient {
try {
if (conn != null) {
NAMING_LOGGER.warn("failed to request " + conn.getURL() + " from "
+ InetAddress.getByName(conn.getURL().getHost()).getHostAddress());
+ InetAddress.getByName(conn.getURL().getHost()).getHostAddress());
}
} catch (Exception e1) {
NAMING_LOGGER.error("[NA] failed to request ", e1);
@ -158,12 +173,12 @@ public class HttpClient {
private static String encodingParams(Map<String, String> params, String encoding)
throws UnsupportedEncodingException {
StringBuilder sb = new StringBuilder();
if (null == params || params.isEmpty()) {
return null;
}
params.put("encoding", encoding);
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
if (StringUtils.isEmpty(entry.getValue())) {
@ -175,6 +190,9 @@ public class HttpClient {
sb.append("&");
}
if (sb.length() > 0) {
sb = sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();
}

View File

@ -18,6 +18,9 @@ package com.alibaba.nacos.client.naming.net;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.SystemPropertyKeyConst;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.api.naming.pojo.Instance;
@ -25,9 +28,12 @@ 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.config.impl.SpasAdapter;
import com.alibaba.nacos.client.monitor.MetricsMonitor;
import com.alibaba.nacos.client.naming.beat.BeatInfo;
import com.alibaba.nacos.client.naming.utils.*;
import com.alibaba.nacos.client.naming.utils.*;
import com.alibaba.nacos.client.utils.TemplateUtils;
import com.alibaba.nacos.common.util.HttpMethod;
import com.alibaba.nacos.common.util.UuidUtils;
@ -35,10 +41,7 @@ import java.io.IOException;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.util.*;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER;
@ -49,6 +52,8 @@ public class NamingProxy {
private static final int DEFAULT_SERVER_PORT = 8848;
private int serverPort = DEFAULT_SERVER_PORT;
private String namespaceId;
private String endpoint;
@ -63,7 +68,7 @@ public class NamingProxy {
private long vipSrvRefInterMillis = TimeUnit.SECONDS.toMillis(30);
private ScheduledExecutorService executorService;
private Properties properties;
public NamingProxy(String namespaceId, String endpoint, String serverList) {
@ -76,7 +81,15 @@ public class NamingProxy {
}
}
executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
initRefreshSrvIfNeed();
}
private void initRefreshSrvIfNeed() {
if (StringUtils.isEmpty(endpoint)) {
return;
}
ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
@ -100,12 +113,7 @@ public class NamingProxy {
try {
String urlString = "http://" + endpoint + "/nacos/serverlist";
List<String> headers = Arrays.asList("Client-Version", UtilAndComs.VERSION,
"User-Agent", UtilAndComs.VERSION,
"Accept-Encoding", "gzip,deflate,sdch",
"Connection", "Keep-Alive",
"RequestId", UuidUtils.generateUuid());
List<String> headers = builderHeaders();
HttpClient.HttpResult result = HttpClient.httpGet(urlString, headers, null, UtilAndComs.ENCODING);
if (HttpURLConnection.HTTP_OK != result.code) {
@ -305,17 +313,13 @@ public class NamingProxy {
throws NacosException {
long start = System.currentTimeMillis();
long end = 0;
List<String> headers = Arrays.asList("Client-Version", UtilAndComs.VERSION,
"User-Agent", UtilAndComs.VERSION,
"Accept-Encoding", "gzip,deflate,sdch",
"Connection", "Keep-Alive",
"RequestId", UuidUtils.generateUuid());
checkSignature(params);
List<String> headers = builderHeaders();
String url;
if (!curServer.contains(UtilAndComs.SERVER_ADDR_IP_SPLITER)) {
curServer = curServer + UtilAndComs.SERVER_ADDR_IP_SPLITER + DEFAULT_SERVER_PORT;
curServer = curServer + UtilAndComs.SERVER_ADDR_IP_SPLITER + serverPort;
}
url = HttpClient.getPrefix() + curServer + api;
@ -384,8 +388,87 @@ public class NamingProxy {
}
private void checkSignature(Map<String, String> params) {
String ak = getAccessKey();
String sk = getSecretKey();
if (StringUtils.isEmpty(ak) && StringUtils.isEmpty(sk)) {
return;
}
try {
String app = System.getProperty("project.name");
String signData = getSignData(params.get("serviceName"));
String signature = SignUtil.sign(signData, sk);
params.put("signature", signature);
params.put("data", signData);
params.put("ak", ak);
params.put("app", app);
} catch (Exception e) {
e.printStackTrace();
}
}
public List<String> builderHeaders() {
List<String> headers = Arrays.asList("Client-Version", UtilAndComs.VERSION,
"User-Agent", UtilAndComs.VERSION,
"Accept-Encoding", "gzip,deflate,sdch",
"Connection", "Keep-Alive",
"RequestId", UuidUtils.generateUuid(), "Request-Module", "Naming");
return headers;
}
private static String getSignData(String serviceName) {
return StringUtils.isNotEmpty(serviceName)
? System.currentTimeMillis() + "@@" + serviceName
: String.valueOf(System.currentTimeMillis());
}
public String getAccessKey() {
if (properties == null) {
return SpasAdapter.getAk();
}
return TemplateUtils.stringEmptyAndThenExecute(properties.getProperty(PropertyKeyConst.ACCESS_KEY), new Callable<String>() {
@Override
public String call() {
return SpasAdapter.getAk();
}
});
}
public String getSecretKey() {
if (properties == null) {
return SpasAdapter.getSk();
}
return TemplateUtils.stringEmptyAndThenExecute(properties.getProperty(PropertyKeyConst.SECRET_KEY), new Callable<String>() {
@Override
public String call() throws Exception {
return SpasAdapter.getSk();
}
});
}
public void setProperties(Properties properties) {
this.properties = properties;
setServerPort(DEFAULT_SERVER_PORT);
}
public String getNamespaceId() {
return namespaceId;
}
public void setServerPort(int serverPort) {
this.serverPort = serverPort;
String sp = System.getProperty(SystemPropertyKeyConst.NAMING_SERVER_PORT);
if (com.alibaba.nacos.client.utils.StringUtils.isNotBlank(sp)) {
this.serverPort = Integer.parseInt(sp);
}
}
}

View File

@ -0,0 +1,66 @@
/*
* 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.utils;
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 enum SigningAlgorithm {
// Hmac SHA1 algorithm
HmacSHA1;
SigningAlgorithm() {
}
}
}

View File

@ -22,6 +22,12 @@ public class UtilAndComs {
public static final String VERSION = "Nacos-Java-Client:v1.0.0";
public static String WEB_CONTEXT = "/nacos";
public static String NACOS_URL_BASE = WEB_CONTEXT + "/v1/ns";
public static String NACOS_URL_INSTANCE = NACOS_URL_BASE + "/instance";
public static final String ENCODING = "UTF-8";
public static final String ENV_LIST_KEY = "envList";
@ -30,10 +36,6 @@ public class UtilAndComs {
public static final String FAILOVER_SWITCH = "00-00---000-VIPSRV_FAILOVER_SWITCH-000---00-00";
public static final String NACOS_URL_BASE = "/nacos/v1/ns";
public static final String NACOS_URL_INSTANCE = NACOS_URL_BASE + "/instance";
public static final String DEFAULT_NAMESPACE_ID = "public";
public static final int REQUEST_DOMAIN_RETRY_COUNT = 3;
@ -44,9 +46,11 @@ public class UtilAndComs {
public static final String SERVER_ADDR_IP_SPLITER = ":";
public static final int DEFAULT_CLIENT_BEAT_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;
public static final int DEFAULT_POLLING_THREAD_COUNT = Runtime.getRuntime()
.availableProcessors() > 1 ? Runtime.getRuntime().availableProcessors() / 2
: 1;
}

View File

@ -37,6 +37,11 @@ public class StringUtils {
return true;
}
public static boolean isNotBlank(String str) {
return !isBlank(str);
}
public static boolean isNotEmpty(String str) {
return !StringUtils.isEmpty(str);
}

View File

@ -0,0 +1,65 @@
/*
* 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.utils;
import java.util.concurrent.Callable;
/**
* @author pbting
* @date 2019-03-04 1:31 PM
*/
public class TemplateUtils {
public static final void stringNotEmptyAndThenExecute(String source, Runnable runnable) {
if (StringUtils.isNotEmpty(source)) {
try {
runnable.run();
} catch (Exception e) {
LogUtils.NAMING_LOGGER.error("string empty and then execute cause an exception.", e);
}
}
}
public static final String stringEmptyAndThenExecute(String source, Callable<String> callable) {
if (StringUtils.isEmpty(source)) {
try {
return callable.call();
} catch (Exception e) {
LogUtils.NAMING_LOGGER.error("string empty and then execute cause an exception.", e);
}
}
return source.trim();
}
public static final String stringBlankAndThenExecute(String source, Callable<String> callable) {
if (StringUtils.isBlank(source)) {
try {
return callable.call();
} catch (Exception e) {
LogUtils.NAMING_LOGGER.error("string empty and then execute cause an exception.", e);
}
}
return source.trim();
}
}

View File

@ -74,7 +74,7 @@ public class ServerListManager {
GlobalExecutor.registerServerStatusReporter(new ServerStatusReporter(), 5000);
}
private List<Server> refreshServerList() {
public List<Server> refreshServerList() {
List<Server> result = new ArrayList<>();

View File

@ -77,24 +77,6 @@ public interface ConsistencyService {
*/
void unlisten(String key, RecordListener listener) throws NacosException;
/**
* Is the local server responsible for a data.
* <p>
* Any write operation to a data in a server not responsible for the data is refused.
*
* @param key key of data
* @return true if the local server is responsible for the data
*/
boolean isResponsible(String key);
/**
* Get the responsible server for a data
*
* @param key key of data
* @return responsible server for the data
*/
String getResponsibleServer(String key);
/**
* Tell the status of this consistency service
*

View File

@ -18,13 +18,12 @@ package com.alibaba.nacos.naming.consistency;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.naming.consistency.ephemeral.EphemeralConsistencyService;
import com.alibaba.nacos.naming.consistency.persistent.PersistentConsistencyService;
import com.alibaba.nacos.naming.core.DistroMapper;
import com.alibaba.nacos.naming.pojo.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Publish execution delegate
* Consistency delegate
*
* @author nkorange
* @since 1.0.0
@ -35,9 +34,6 @@ public class DelegateConsistencyServiceImpl implements ConsistencyService {
@Autowired
private PersistentConsistencyService persistentConsistencyService;
@Autowired
private DistroMapper distroMapper;
@Autowired
private EphemeralConsistencyService ephemeralConsistencyService;
@ -58,6 +54,14 @@ public class DelegateConsistencyServiceImpl implements ConsistencyService {
@Override
public void listen(String key, RecordListener listener) throws NacosException {
// this special key is listened by both:
if (KeyBuilder.SERVICE_META_KEY_PREFIX.equals(key)) {
persistentConsistencyService.listen(key, listener);
ephemeralConsistencyService.listen(key, listener);
return;
}
mapConsistencyService(key).listen(key, listener);
}
@ -66,16 +70,6 @@ public class DelegateConsistencyServiceImpl implements ConsistencyService {
mapConsistencyService(key).unlisten(key, listener);
}
@Override
public boolean isResponsible(String key) {
return distroMapper.responsible(KeyBuilder.getServiceName(key));
}
@Override
public String getResponsibleServer(String key) {
return distroMapper.mapSrv(KeyBuilder.getServiceName(key));
}
@Override
public boolean isAvailable() {
return ephemeralConsistencyService.isAvailable() && persistentConsistencyService.isAvailable();

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.naming.consistency.ephemeral.partition;
package com.alibaba.nacos.naming.consistency.ephemeral.distro;
import com.alibaba.nacos.naming.consistency.Datum;
import com.alibaba.nacos.naming.core.Instances;
@ -80,4 +80,8 @@ public class DataStore {
}
return count;
}
public Map<String, Datum> getDataMap() {
return dataMap;
}
}

View File

@ -13,9 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.naming.consistency.ephemeral.partition;
package com.alibaba.nacos.naming.consistency.ephemeral.distro;
import com.alibaba.nacos.common.util.IoUtils;
import com.alibaba.nacos.naming.cluster.ServerListManager;
import com.alibaba.nacos.naming.cluster.servers.Server;
import com.alibaba.nacos.naming.cluster.servers.ServerChangeListener;
@ -30,18 +29,12 @@ import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static org.apache.commons.lang3.CharEncoding.UTF_8;
/**
* Data replicator
*
@ -71,8 +64,6 @@ public class DataSyncer implements ServerChangeListener {
private List<Server> servers;
private boolean initialized = false;
@PostConstruct
public void init() {
serverListManager.listen(this);
@ -175,33 +166,6 @@ public class DataSyncer implements ServerChangeListener {
@Override
public void run() {
try {
File metaFile = new File(UtilsAndCommons.DATA_BASE_DIR + File.separator + "ephemeral.properties");
if (initialized) {
// write the current instance count to disk:
IoUtils.writeStringToFile(metaFile, "instanceCount=" + dataStore.getInstanceCount(), "UTF-8");
} else {
// check if most of the data are loaded:
List<String> lines = IoUtils.readLines(new InputStreamReader(new FileInputStream(metaFile), UTF_8));
if (lines == null || lines.isEmpty()) {
initialized = true;
} else {
int desiredInstanceCount = Integer.parseInt(lines.get(0).split("=")[1]);
if (desiredInstanceCount <= 0 ||
desiredInstanceCount * partitionConfig.getInitDataRatio() < dataStore.keys().size()) {
initialized = true;
}
}
}
} catch (IOException ioe) {
initialized = true;
Loggers.EPHEMERAL.error("operate on meta file failed.", ioe);
} catch (Exception e) {
Loggers.EPHEMERAL.error("operate on meta file failed.", e);
}
try {
// send local timestamps to other servers:
Map<String, String> keyChecksums = new HashMap<>(64);
@ -225,7 +189,7 @@ public class DataSyncer implements ServerChangeListener {
if (NetUtils.localServer().equals(member.getKey())) {
continue;
}
NamingProxy.syncTimestamps(keyChecksums, member.getKey());
NamingProxy.syncChecksums(keyChecksums, member.getKey());
}
} catch (Exception e) {
Loggers.EPHEMERAL.error("timed sync task failed.", e);
@ -241,10 +205,6 @@ public class DataSyncer implements ServerChangeListener {
return key + UtilsAndCommons.CACHE_KEY_SPLITER + targetServer;
}
public boolean isInitialized() {
return initialized;
}
@Override
public void onChangeServerList(List<Server> latestMembers) {

View File

@ -13,22 +13,28 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.naming.consistency.ephemeral.partition;
package com.alibaba.nacos.naming.consistency.ephemeral.distro;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.core.utils.SystemUtils;
import com.alibaba.nacos.naming.cluster.ServerListManager;
import com.alibaba.nacos.naming.cluster.ServerMode;
import com.alibaba.nacos.naming.cluster.ServerStatus;
import com.alibaba.nacos.naming.cluster.servers.Server;
import com.alibaba.nacos.naming.cluster.transport.Serializer;
import com.alibaba.nacos.naming.consistency.RecordListener;
import com.alibaba.nacos.naming.consistency.Datum;
import com.alibaba.nacos.naming.consistency.KeyBuilder;
import com.alibaba.nacos.naming.consistency.RecordListener;
import com.alibaba.nacos.naming.consistency.ephemeral.EphemeralConsistencyService;
import com.alibaba.nacos.naming.core.DistroMapper;
import com.alibaba.nacos.naming.core.Instances;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.NamingProxy;
import com.alibaba.nacos.naming.core.Service;
import com.alibaba.nacos.naming.misc.*;
import com.alibaba.nacos.naming.pojo.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -37,7 +43,7 @@ import java.util.concurrent.ConcurrentHashMap;
/**
* A consistency protocol algorithm called <b>Partition</b>
* <p>
* Use a partition algorithm to divide data into many blocks. Each Nacos server node takes
* Use a distro algorithm to divide data into many blocks. Each Nacos server node takes
* responsibility for exactly one block of data. Each block of data is generated, removed
* and synchronized by its responsible server. So every Nacos server only handles writings
* for a subset of the total service data.
@ -48,8 +54,8 @@ import java.util.concurrent.ConcurrentHashMap;
* @author nkorange
* @since 1.0.0
*/
@Service("partitionConsistencyService")
public class PartitionConsistencyServiceImpl implements EphemeralConsistencyService {
@org.springframework.stereotype.Service("distroConsistencyService")
public class DistroConsistencyServiceImpl implements EphemeralConsistencyService {
@Autowired
private DistroMapper distroMapper;
@ -66,8 +72,52 @@ public class PartitionConsistencyServiceImpl implements EphemeralConsistencyServ
@Autowired
private Serializer serializer;
@Autowired
private ServerListManager serverListManager;
@Autowired
private SwitchDomain switchDomain;
private boolean initialized = false;
private volatile Map<String, List<RecordListener>> listeners = new ConcurrentHashMap<>();
@PostConstruct
public void init() throws Exception {
GlobalExecutor.submit(new Runnable() {
@Override
public void run() {
try {
load();
} catch (Exception e) {
Loggers.EPHEMERAL.error("load data failed.", e);
}
}
});
}
public void load() throws Exception {
if (SystemUtils.STANDALONE_MODE) {
initialized = true;
return;
}
while (serverListManager.getHealthyServers().isEmpty()) {
Thread.sleep(1000L);
Loggers.EPHEMERAL.info("waiting server list init...");
}
for (Server server : serverListManager.getHealthyServers()) {
if (NetUtils.localServer().equals(server.getKey())) {
continue;
}
// try sync data from remote server:
if (syncAllDataFromRemote(server)) {
initialized = true;
return;
}
}
}
@Override
public void put(String key, Record value) throws NacosException {
onPut(key, value);
@ -122,12 +172,12 @@ public class PartitionConsistencyServiceImpl implements EphemeralConsistencyServ
}
}
public void onReceiveTimestamps(Map<String, String> timestamps, String server) {
public void onReceiveChecksums(Map<String, String> checksumMap, String server) {
List<String> toUpdateKeys = new ArrayList<>();
List<String> toRemoveKeys = new ArrayList<>();
for (Map.Entry<String, String> entry : timestamps.entrySet()) {
if (isResponsible(entry.getKey())) {
for (Map.Entry<String, String> entry : checksumMap.entrySet()) {
if (distroMapper.responsible(KeyBuilder.getServiceName(entry.getKey()))) {
// this key should not be sent from remote server:
Loggers.EPHEMERAL.error("receive responsible key timestamp of " + entry.getKey() + " from " + server);
// abort the procedure:
@ -146,7 +196,7 @@ public class PartitionConsistencyServiceImpl implements EphemeralConsistencyServ
continue;
}
if (!timestamps.containsKey(key)) {
if (!checksumMap.containsKey(key)) {
toRemoveKeys.add(key);
}
}
@ -163,31 +213,71 @@ public class PartitionConsistencyServiceImpl implements EphemeralConsistencyServ
try {
byte[] result = NamingProxy.getData(toUpdateKeys, server);
if (result.length > 0) {
Map<String, Datum<Instances>> datumMap =
serializer.deserializeMap(result, Instances.class);
for (Map.Entry<String, Datum<Instances>> entry : datumMap.entrySet()) {
dataStore.put(entry.getKey(), entry.getValue());
if (!listeners.containsKey(entry.getKey())) {
return;
}
for (RecordListener listener : listeners.get(entry.getKey())) {
try {
listener.onChange(entry.getKey(), entry.getValue().value);
} catch (Exception e) {
Loggers.EPHEMERAL.error("notify " + listener + ", key: " + entry.getKey() + " failed.", e);
}
}
}
}
processData(result);
} catch (Exception e) {
Loggers.EPHEMERAL.error("get data from " + server + " failed!", e);
}
}
public boolean syncAllDataFromRemote(Server server) {
try {
byte[] data = NamingProxy.getAllData(server.getKey());
processData(data);
return true;
} catch (Exception e) {
Loggers.EPHEMERAL.error("sync full data from " + server + " failed!");
return false;
}
}
public void processData(byte[] data) throws Exception {
if (data.length > 0) {
Map<String, Datum<Instances>> datumMap =
serializer.deserializeMap(data, Instances.class);
for (Map.Entry<String, Datum<Instances>> entry : datumMap.entrySet()) {
dataStore.put(entry.getKey(), entry.getValue());
if (!listeners.containsKey(entry.getKey())) {
// pretty sure the service not exist:
if (ServerMode.AP.name().equals(switchDomain.getServerMode())) {
// create empty service
Service service = new Service();
String serviceName = KeyBuilder.getServiceName(entry.getKey());
String namespaceId = KeyBuilder.getNamespace(entry.getKey());
service.setName(serviceName);
service.setNamespaceId(namespaceId);
service.setGroupName(Constants.DEFAULT_GROUP);
// now validate the service. if failed, exception will be thrown
service.setLastModifiedMillis(System.currentTimeMillis());
service.recalculateChecksum();
listeners.get(KeyBuilder.SERVICE_META_KEY_PREFIX).get(0)
.onChange(KeyBuilder.buildServiceMetaKey(namespaceId, serviceName), service);
}
}
}
for (Map.Entry<String, Datum<Instances>> entry : datumMap.entrySet()) {
dataStore.put(entry.getKey(), entry.getValue());
if (!listeners.containsKey(entry.getKey())) {
Loggers.EPHEMERAL.warn("listener not found: {}", entry.getKey());
continue;
}
for (RecordListener listener : listeners.get(entry.getKey())) {
try {
listener.onChange(entry.getKey(), entry.getValue().value);
} catch (Exception e) {
Loggers.EPHEMERAL.error("notify " + listener + ", key: " + entry.getKey() + " failed.", e);
}
}
}
}
}
@Override
public void listen(String key, RecordListener listener) throws NacosException {
if (!listeners.containsKey(key)) {
@ -209,18 +299,8 @@ public class PartitionConsistencyServiceImpl implements EphemeralConsistencyServ
}
}
@Override
public boolean isResponsible(String key) {
return distroMapper.responsible(KeyBuilder.getServiceName(key));
}
@Override
public String getResponsibleServer(String key) {
return distroMapper.mapSrv(KeyBuilder.getServiceName(key));
}
@Override
public boolean isAvailable() {
return dataSyncer.isInitialized();
return initialized || ServerStatus.UP.name().equals(switchDomain.getOverriddenServerStatus());
}
}

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.naming.consistency.ephemeral.partition;
package com.alibaba.nacos.naming.consistency.ephemeral.distro;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@ -27,19 +27,19 @@ import org.springframework.stereotype.Component;
@Component
public class PartitionConfig {
@Value("${nacos.naming.partition.taskDispatchPeriod}")
@Value("${nacos.naming.distro.taskDispatchPeriod}")
private int taskDispatchPeriod = 2000;
@Value("${nacos.naming.partition.batchSyncKeyCount}")
@Value("${nacos.naming.distro.batchSyncKeyCount}")
private int batchSyncKeyCount = 1000;
@Value("${nacos.naming.partition.initDataRatio}")
@Value("${nacos.naming.distro.initDataRatio}")
private float initDataRatio = 0.9F;
@Value("${nacos.naming.partition.syncRetryDelay}")
@Value("${nacos.naming.distro.syncRetryDelay}")
private long syncRetryDelay = 5000L;
@Value("${nacos.naming.partition.taskDispatchThreadCount}")
@Value("${nacos.naming.distro.taskDispatchThreadCount}")
private int taskDispatchThreadCount = Runtime.getRuntime().availableProcessors();
public int getTaskDispatchPeriod() {

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.naming.consistency.ephemeral.partition;
package com.alibaba.nacos.naming.consistency.ephemeral.distro;
import java.util.List;

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.naming.consistency.ephemeral.partition;
package com.alibaba.nacos.naming.consistency.ephemeral.distro;
import com.alibaba.nacos.naming.cluster.servers.Server;
import com.alibaba.nacos.naming.misc.GlobalExecutor;

View File

@ -16,10 +16,12 @@
package com.alibaba.nacos.naming.consistency.persistent.raft;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.naming.consistency.RecordListener;
import com.alibaba.nacos.naming.cluster.ServerStatus;
import com.alibaba.nacos.naming.consistency.Datum;
import com.alibaba.nacos.naming.consistency.RecordListener;
import com.alibaba.nacos.naming.consistency.persistent.PersistentConsistencyService;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.naming.pojo.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -36,6 +38,9 @@ public class RaftConsistencyServiceImpl implements PersistentConsistencyService
@Autowired
private RaftCore raftCore;
@Autowired
private SwitchDomain switchDomain;
@Override
public void put(String key, Record value) throws NacosException {
try {
@ -71,19 +76,9 @@ public class RaftConsistencyServiceImpl implements PersistentConsistencyService
raftCore.unlisten(key, listener);
}
@Override
public boolean isResponsible(String key) {
return false;
}
@Override
public String getResponsibleServer(String key) {
return null;
}
@Override
public boolean isAvailable() {
return raftCore.isInitialized();
return raftCore.isInitialized() || ServerStatus.UP.name().equals(switchDomain.getOverriddenServerStatus());
}
public void onPut(Datum datum, RaftPeer source) throws NacosException {

View File

@ -35,6 +35,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.javatuples.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@ -62,17 +63,17 @@ public class RaftCore {
public static final String API_BEAT = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/beat";
public static final String API_PUB = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/publish";
public static final String API_PUB = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/datum";
public static final String API_DEL = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/delete";
public static final String API_DEL = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/datum";
public static final String API_GET = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/get";
public static final String API_GET = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/datum";
public static final String API_ON_PUB = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/onPublish";
public static final String API_ON_PUB = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/datum/commit";
public static final String API_ON_DEL = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/onDelete";
public static final String API_ON_DEL = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/datum/commit";
public static final String API_GET_PEER = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/getPeer";
public static final String API_GET_PEER = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/peer";
private ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
@Override
@ -228,8 +229,7 @@ public class RaftCore {
if (!isLeader()) {
Map<String, String> params = new HashMap<>(1);
params.put("key", URLEncoder.encode(key, "UTF-8"));
raftProxy.proxyGET(getLeader().ip, API_DEL, params);
raftProxy.proxy(getLeader().ip, API_DEL, params, HttpMethod.DELETE);
return;
}
@ -244,7 +244,7 @@ public class RaftCore {
for (final String server : peers.allServersWithoutMySelf()) {
String url = buildURL(server, API_ON_DEL);
HttpClient.asyncHttpPostLarge(url, null, JSON.toJSONString(json)
HttpClient.asyncHttpDeleteLarge(url, null, JSON.toJSONString(json)
, new AsyncCompletionHandler<Integer>() {
@Override
public Integer onCompleted(Response response) throws Exception {

View File

@ -172,7 +172,7 @@ public class RaftPeerSet implements ServerChangeListener {
if (!Objects.equals(peer, candidate) && peer.state == RaftPeer.State.LEADER) {
try {
String url = RaftCore.buildURL(peer.ip, RaftCore.API_GET_PEER);
HttpClient.asyncHttpPost(url, null, params, new AsyncCompletionHandler<Integer>() {
HttpClient.asyncHttpGet(url, null, params, new AsyncCompletionHandler<Integer>() {
@Override
public Integer onCompleted(Response response) throws Exception {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {

View File

@ -15,9 +15,11 @@
*/
package com.alibaba.nacos.naming.consistency.persistent.raft;
import com.alibaba.nacos.api.naming.pojo.AbstractHealthChecker;
import com.alibaba.nacos.naming.boot.RunningConfig;
import com.alibaba.nacos.naming.misc.HttpClient;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import java.net.HttpURLConnection;
@ -42,6 +44,32 @@ public class RaftProxy {
}
}
public void proxy(String server, String api, Map<String, String> params, HttpMethod method) throws Exception {
// do proxy
if (!server.contains(UtilsAndCommons.IP_PORT_SPLITER)) {
server = server + UtilsAndCommons.IP_PORT_SPLITER + RunningConfig.getServerPort();
}
String url = "http://" + server + RunningConfig.getContextPath() + api;
HttpClient.HttpResult result;
switch (method) {
case GET:
result = HttpClient.httpGet(url, null, params);
break;
case POST:
result = HttpClient.httpPost(url, null, params);
break;
case DELETE:
result = HttpClient.httpDelete(url, null, params);
break;
default:
throw new RuntimeException("unsupported method:" + method);
}
if (result.code != HttpURLConnection.HTTP_OK) {
throw new IllegalStateException("leader failed, caused by: " + result.content);
}
}
public void proxyPostLarge(String server, String api, String content, Map<String, String> headers) throws Exception {
// do proxy
if (!server.contains(UtilsAndCommons.IP_PORT_SPLITER)) {

View File

@ -32,6 +32,7 @@ import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@ -49,54 +50,7 @@ public class CatalogController {
@Autowired
protected ServiceManager serviceManager;
@RequestMapping(value = "/serviceList")
public JSONObject serviceList(HttpServletRequest request) throws Exception {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
Constants.DEFAULT_NAMESPACE_ID);
JSONObject result = new JSONObject();
int page = Integer.parseInt(WebUtils.required(request, "startPg"));
int pageSize = Integer.parseInt(WebUtils.required(request, "pgSize"));
String keyword = WebUtils.optional(request, "keyword", StringUtils.EMPTY);
List<Service> services = new ArrayList<>();
int total = serviceManager.getPagedService(namespaceId, page - 1, pageSize, keyword, services);
if (CollectionUtils.isEmpty(services)) {
result.put("serviceList", Collections.emptyList());
result.put("count", 0);
return result;
}
JSONArray serviceJsonArray = new JSONArray();
for (Service service : services) {
ServiceView serviceView = new ServiceView();
serviceView.setName(UtilsAndCommons.getServiceName(service.getName()));
serviceView.setGroupName(UtilsAndCommons.getGroupName(service.getName()));
serviceView.setClusterCount(service.getClusterMap().size());
serviceView.setIpCount(service.allIPs().size());
// FIXME should be optimized:
int validCount = 0;
for (Instance instance : service.allIPs()) {
if (instance.isHealthy()) {
validCount++;
}
}
serviceView.setHealthyInstanceCount(validCount);
serviceJsonArray.add(serviceView);
}
result.put("serviceList", serviceJsonArray);
result.put("count", total);
return result;
}
@RequestMapping(value = "/serviceDetail")
@RequestMapping(value = "/service")
public ServiceDetailView serviceDetail(HttpServletRequest request) throws Exception {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
@ -130,15 +84,15 @@ public class CatalogController {
return detailView;
}
@RequestMapping(value = "/instanceList")
@RequestMapping(value = "/instances")
public JSONObject instanceList(HttpServletRequest request) throws Exception {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
Constants.DEFAULT_NAMESPACE_ID);
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
String clusterName = WebUtils.required(request, CommonParams.CLUSTER_NAME);
int page = Integer.parseInt(WebUtils.required(request, "startPg"));
int pageSize = Integer.parseInt(WebUtils.required(request, "pgSize"));
int page = Integer.parseInt(WebUtils.required(request, "pageNo"));
int pageSize = Integer.parseInt(WebUtils.required(request, "pageSize"));
Service service = serviceManager.getService(namespaceId, serviceName);
if (service == null) {
@ -174,37 +128,43 @@ public class CatalogController {
}
@RequestMapping(value = "/services", method = RequestMethod.GET)
public List<ServiceDetailInfo> listDetail(HttpServletRequest request) {
public Object listDetail(HttpServletRequest request) throws Exception {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
Constants.DEFAULT_NAMESPACE_ID);
List<ServiceDetailInfo> serviceDetailInfoList = new ArrayList<>();
boolean withInstances = Boolean.parseBoolean(WebUtils.optional(request, "withInstances", "true"));
serviceManager
.getServiceMap(namespaceId)
.forEach(
(serviceName, service) -> {
if (withInstances) {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
Constants.DEFAULT_NAMESPACE_ID);
List<ServiceDetailInfo> serviceDetailInfoList = new ArrayList<>();
ServiceDetailInfo serviceDetailInfo = new ServiceDetailInfo();
serviceDetailInfo.setServiceName(UtilsAndCommons.getServiceName(serviceName));
serviceDetailInfo.setGroupName(UtilsAndCommons.getGroupName(serviceName));
serviceDetailInfo.setMetadata(service.getMetadata());
serviceManager
.getServiceMap(namespaceId)
.forEach(
(serviceName, service) -> {
Map<String, ClusterInfo> clusterInfoMap = getStringClusterInfoMap(service);
serviceDetailInfo.setClusterMap(clusterInfoMap);
ServiceDetailInfo serviceDetailInfo = new ServiceDetailInfo();
serviceDetailInfo.setServiceName(UtilsAndCommons.getServiceName(serviceName));
serviceDetailInfo.setGroupName(UtilsAndCommons.getGroupName(serviceName));
serviceDetailInfo.setMetadata(service.getMetadata());
serviceDetailInfoList.add(serviceDetailInfo);
});
Map<String, ClusterInfo> clusterInfoMap = getStringClusterInfoMap(service);
serviceDetailInfo.setClusterMap(clusterInfoMap);
return serviceDetailInfoList;
serviceDetailInfoList.add(serviceDetailInfo);
});
return serviceDetailInfoList;
} else {
return serviceList(request);
}
}
@RequestMapping("/rt4Service")
@RequestMapping("/rt/service")
public JSONObject rt4Service(HttpServletRequest request) {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
Constants.DEFAULT_NAMESPACE_ID);
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
Service service = serviceManager.getService(namespaceId, serviceName);
@ -231,38 +191,6 @@ public class CatalogController {
return result;
}
@RequestMapping("/getServicesByIP")
public JSONObject getServicesByIP(HttpServletRequest request) {
String ip = WebUtils.required(request, "ip");
Set<String> serviceNames = new HashSet<>();
Map<String, Set<String>> serviceNameMap = serviceManager.getAllServiceNames();
for (String namespaceId : serviceNameMap.keySet()) {
for (String serviceName : serviceNameMap.get(namespaceId)) {
Service service = serviceManager.getService(namespaceId, serviceName);
List<Instance> instances = service.allIPs();
for (Instance instance : instances) {
if (ip.contains(":")) {
if (StringUtils.equals(instance.getIp() + ":" + instance.getPort(), ip)) {
serviceNames.add(namespaceId + UtilsAndCommons.NAMESPACE_SERVICE_CONNECTOR + service.getName());
}
} else {
if (StringUtils.equals(instance.getIp(), ip)) {
serviceNames.add(namespaceId + UtilsAndCommons.NAMESPACE_SERVICE_CONNECTOR + service.getName());
}
}
}
}
}
JSONObject result = new JSONObject();
result.put("doms", serviceNames);
return result;
}
/**
* getStringClusterInfoMap
*
@ -307,4 +235,52 @@ public class CatalogController {
return ipAddressInfos;
}
private JSONObject serviceList(HttpServletRequest request) throws Exception {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
Constants.DEFAULT_NAMESPACE_ID);
JSONObject result = new JSONObject();
int page = Integer.parseInt(WebUtils.required(request, "pageNo"));
int pageSize = Integer.parseInt(WebUtils.required(request, "pageSize"));
String keyword = WebUtils.optional(request, "keyword", StringUtils.EMPTY);
String containedInstance = WebUtils.required(request, "instance");
List<Service> services = new ArrayList<>();
int total = serviceManager.getPagedService(namespaceId, page - 1, pageSize, keyword, containedInstance, services);
if (CollectionUtils.isEmpty(services)) {
result.put("serviceList", Collections.emptyList());
result.put("count", 0);
return result;
}
JSONArray serviceJsonArray = new JSONArray();
for (Service service : services) {
ServiceView serviceView = new ServiceView();
serviceView.setName(UtilsAndCommons.getServiceName(service.getName()));
serviceView.setGroupName(UtilsAndCommons.getGroupName(service.getName()));
serviceView.setClusterCount(service.getClusterMap().size());
serviceView.setIpCount(service.allIPs().size());
// FIXME should be optimized:
int validCount = 0;
for (Instance instance : service.allIPs()) {
if (instance.isHealthy()) {
validCount++;
}
}
serviceView.setHealthyInstanceCount(validCount);
serviceJsonArray.add(serviceView);
}
result.put("serviceList", serviceJsonArray);
result.put("count", total);
return result;
}
}

View File

@ -21,7 +21,8 @@ import com.alibaba.nacos.naming.cluster.ServerMode;
import com.alibaba.nacos.naming.cluster.transport.Serializer;
import com.alibaba.nacos.naming.consistency.Datum;
import com.alibaba.nacos.naming.consistency.KeyBuilder;
import com.alibaba.nacos.naming.consistency.ephemeral.partition.PartitionConsistencyServiceImpl;
import com.alibaba.nacos.naming.consistency.ephemeral.distro.DataStore;
import com.alibaba.nacos.naming.consistency.ephemeral.distro.DistroConsistencyServiceImpl;
import com.alibaba.nacos.naming.core.Instances;
import com.alibaba.nacos.naming.core.ServiceManager;
import com.alibaba.nacos.naming.exception.NacosException;
@ -47,14 +48,17 @@ import java.util.Map;
* @since 1.0.0
*/
@RestController
@RequestMapping(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/partition")
public class PartitionController {
@RequestMapping(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/distro")
public class DistroController {
@Autowired
private Serializer serializer;
@Autowired
private PartitionConsistencyServiceImpl consistencyService;
private DistroConsistencyServiceImpl consistencyService;
@Autowired
private DataStore dataStore;
@Autowired
private ServiceManager serviceManager;
@ -89,24 +93,14 @@ public class PartitionController {
return "ok";
}
@RequestMapping(value = "/timestamps", method = RequestMethod.PUT)
public String syncTimestamps(HttpServletRequest request, HttpServletResponse response) throws Exception {
@RequestMapping(value = "/checksum", method = RequestMethod.PUT)
public String syncChecksum(HttpServletRequest request, HttpServletResponse response) throws Exception {
String source = WebUtils.required(request, "source");
String entity = IOUtils.toString(request.getInputStream(), "UTF-8");
Map<String, String> dataMap =
serializer.deserialize(entity.getBytes(), new TypeReference<Map<String, String>>() {
});
for (String key : dataMap.keySet()) {
String namespaceId = KeyBuilder.getNamespace(key);
String serviceName = KeyBuilder.getServiceName(key);
if (!serviceManager.containService(namespaceId, serviceName)
&& ServerMode.AP.name().equals(switchDomain.getServerMode())) {
serviceManager.createEmptyService(namespaceId, serviceName);
}
}
consistencyService.onReceiveTimestamps(dataMap, source);
consistencyService.onReceiveChecksums(dataMap, source);
return "ok";
}
@ -120,4 +114,9 @@ public class PartitionController {
}
response.getWriter().write(new String(serializer.serialize(datumMap), "UTF-8"));
}
@RequestMapping(value = "/datums", method = RequestMethod.GET)
public void getAllDatums(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.getWriter().write(new String(serializer.serialize(dataStore.getDataMap()), "UTF-8"));
}
}

View File

@ -68,7 +68,7 @@ public class HealthController {
return result;
}
@RequestMapping(value = "", method = RequestMethod.PUT)
@RequestMapping(value = {"", "/instance"}, method = RequestMethod.PUT)
public String update(HttpServletRequest request) throws Exception {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,

View File

@ -52,7 +52,7 @@ import java.util.*;
* @author nkorange
*/
@RestController
@RequestMapping(UtilsAndCommons.NACOS_NAMING_CONTEXT)
@RequestMapping(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/instance")
public class InstanceController {
@Autowired
@ -89,7 +89,7 @@ public class InstanceController {
};
@CanDistro
@RequestMapping(value = "/instance", method = RequestMethod.POST)
@RequestMapping(value = "", method = RequestMethod.POST)
public String register(HttpServletRequest request) throws Exception {
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
@ -100,7 +100,7 @@ public class InstanceController {
}
@CanDistro
@RequestMapping(value = "/instance", method = RequestMethod.DELETE)
@RequestMapping(value = "", method = RequestMethod.DELETE)
public String deregister(HttpServletRequest request) throws Exception {
Instance instance = getIPAddress(request);
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
@ -117,7 +117,7 @@ public class InstanceController {
return "ok";
}
@RequestMapping(value = "/instance", method = RequestMethod.PUT)
@RequestMapping(value = "", method = RequestMethod.PUT)
public String update(HttpServletRequest request) throws Exception {
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
@ -126,7 +126,7 @@ public class InstanceController {
return "ok";
}
@RequestMapping(value = {"/instances", "/instance/list"}, method = RequestMethod.GET)
@RequestMapping(value = "/list", method = RequestMethod.GET)
public JSONObject list(HttpServletRequest request) throws Exception {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
@ -152,7 +152,7 @@ public class InstanceController {
return doSrvIPXT(namespaceId, serviceName, agent, clusters, clientIP, udpPort, env, isCheck, app, tenant, healthyOnly);
}
@RequestMapping(value = "/instance", method = RequestMethod.GET)
@RequestMapping(value = "", method = RequestMethod.GET)
public JSONObject detail(HttpServletRequest request) throws Exception {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
@ -194,7 +194,7 @@ public class InstanceController {
}
@CanDistro
@RequestMapping(value = "/instance/beat", method = RequestMethod.PUT)
@RequestMapping(value = "/beat", method = RequestMethod.PUT)
public JSONObject beat(HttpServletRequest request) throws Exception {
JSONObject result = new JSONObject();
@ -254,7 +254,7 @@ public class InstanceController {
}
@RequestMapping("/instance/listWithHealthStatus")
@RequestMapping("/statuses")
public JSONObject listWithHealthStatus(HttpServletRequest request) throws NacosException {
String key = WebUtils.required(request, "key");
@ -402,7 +402,7 @@ public class InstanceController {
result.put("name", serviceName);
result.put("cacheMillis", cacheMillis);
result.put("lastRefTime", System.currentTimeMillis());
result.put("checksum", service.getChecksum() + System.currentTimeMillis());
result.put("checksum", service.getChecksum());
result.put("useSpecifiedURL", false);
result.put("clusters", clusters);
result.put("env", env);

View File

@ -34,6 +34,7 @@ import com.alibaba.nacos.naming.push.PushService;
import com.alibaba.nacos.naming.web.NeedAuth;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@ -72,7 +73,7 @@ public class OperatorController {
@Autowired
private DistroMapper distroMapper;
@RequestMapping("/pushState")
@RequestMapping("/push/state")
public JSONObject pushState(HttpServletRequest request) {
JSONObject result = new JSONObject();
@ -112,7 +113,7 @@ public class OperatorController {
return result;
}
@RequestMapping("/switches")
@RequestMapping(value = "/switches", method = RequestMethod.GET)
public SwitchDomain switches(HttpServletRequest request) {
return switchDomain;
}
@ -152,12 +153,12 @@ public class OperatorController {
return result;
}
@RequestMapping("/getResponsibleServer4Dom")
public JSONObject getResponsibleServer4Dom(HttpServletRequest request) {
@RequestMapping(value = "/distro/server", method = RequestMethod.GET)
public JSONObject getResponsibleServer4Service(HttpServletRequest request) {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
Constants.DEFAULT_NAMESPACE_ID);
String dom = WebUtils.required(request, "dom");
Service service = serviceManager.getService(namespaceId, dom);
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
Service service = serviceManager.getService(namespaceId, serviceName);
if (service == null) {
throw new IllegalArgumentException("service not found");
@ -165,39 +166,12 @@ public class OperatorController {
JSONObject result = new JSONObject();
result.put("responsibleServer", distroMapper.mapSrv(dom));
result.put("responsibleServer", distroMapper.mapSrv(serviceName));
return result;
}
@RequestMapping("/getHealthyServerList")
public JSONObject getHealthyServerList(HttpServletRequest request) {
JSONObject result = new JSONObject();
result.put("healthyList", distroMapper.getHealthyList());
return result;
}
@RequestMapping("/responsible")
public JSONObject responsible(HttpServletRequest request) {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
Constants.DEFAULT_NAMESPACE_ID);
String dom = WebUtils.required(request, "dom");
Service service = serviceManager.getService(namespaceId, dom);
if (service == null) {
throw new IllegalArgumentException("service not found");
}
JSONObject result = new JSONObject();
result.put("responsible", distroMapper.responsible(dom));
return result;
}
@RequestMapping("/distroStatus")
@RequestMapping(value = "/distro/status", method = RequestMethod.GET)
public JSONObject distroStatus(HttpServletRequest request) {
JSONObject result = new JSONObject();
@ -216,7 +190,21 @@ public class OperatorController {
return result;
}
@RequestMapping("/serverStatus")
@RequestMapping(value = "/servers", method = RequestMethod.GET)
public JSONObject getHealthyServerList(HttpServletRequest request) {
boolean healthy = Boolean.parseBoolean(WebUtils.optional(request, "healthy", "false"));
JSONObject result = new JSONObject();
if (healthy) {
result.put("servers", serverListManager.getHealthyServers());
} else {
result.put("servers", serverListManager.getServers());
}
return result;
}
@RequestMapping("/server/status")
public String serverStatus(HttpServletRequest request) {
String serverStatus = WebUtils.required(request, "serverStatus");
serverListManager.onReceiveServerStatus(serverStatus);

View File

@ -96,7 +96,7 @@ public class RaftController {
}
@NeedAuth
@RequestMapping("/getPeer")
@RequestMapping(value = "/peer", method = RequestMethod.GET)
public JSONObject getPeer(HttpServletRequest request, HttpServletResponse response) {
List<RaftPeer> peers = raftCore.getPeers();
RaftPeer peer = null;
@ -116,7 +116,7 @@ public class RaftController {
}
@NeedAuth
@RequestMapping("/reloadDatum")
@RequestMapping(value = "/datum/reload", method = RequestMethod.PUT)
public String reloadDatum(HttpServletRequest request, HttpServletResponse response) throws Exception {
String key = WebUtils.required(request, "key");
raftCore.loadDatum(key);
@ -124,7 +124,7 @@ public class RaftController {
}
@NeedAuth
@RequestMapping(value = "/publish", method = RequestMethod.POST)
@RequestMapping(value = "/datum", method = RequestMethod.POST)
public String publish(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setHeader("Content-Type", "application/json; charset=" + getAcceptEncoding(request));
@ -155,7 +155,7 @@ public class RaftController {
}
@NeedAuth
@RequestMapping(value = "/delete", method = RequestMethod.POST)
@RequestMapping(value = "/datum", method = RequestMethod.DELETE)
public String delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setHeader("Content-Type", "application/json; charset=" + getAcceptEncoding(request));
@ -166,7 +166,7 @@ public class RaftController {
}
@NeedAuth
@RequestMapping("/get")
@RequestMapping(value = "/datum", method = RequestMethod.GET)
public String get(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setHeader("Content-Type", "application/json; charset=" + getAcceptEncoding(request));
@ -185,7 +185,7 @@ public class RaftController {
return JSON.toJSONString(datums);
}
@RequestMapping("/state")
@RequestMapping(value = "/state", method = RequestMethod.GET)
public JSONObject state(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setHeader("Content-Type", "application/json; charset=" + getAcceptEncoding(request));
@ -200,7 +200,7 @@ public class RaftController {
}
@NeedAuth
@RequestMapping(value = "/onPublish", method = RequestMethod.POST)
@RequestMapping(value = "/datum/commit", method = RequestMethod.POST)
public String onPublish(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setHeader("Content-Type", "application/json; charset=" + getAcceptEncoding(request));
@ -232,7 +232,7 @@ public class RaftController {
}
@NeedAuth
@RequestMapping(value = "/onDelete", method = RequestMethod.POST)
@RequestMapping(value = "/datum/commit", method = RequestMethod.DELETE)
public String onDelete(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setHeader("Content-Type", "application/json; charset=" + getAcceptEncoding(request));
@ -252,7 +252,7 @@ public class RaftController {
return "ok";
}
@RequestMapping("/getLeader")
@RequestMapping(value = "/leader", method = RequestMethod.GET)
public JSONObject getLeader(HttpServletRequest request, HttpServletResponse response) {
JSONObject result = new JSONObject();
@ -260,7 +260,7 @@ public class RaftController {
return result;
}
@RequestMapping("/getAllListeners")
@RequestMapping(value = "/listeners", method = RequestMethod.GET)
public JSONObject getAllListeners(HttpServletRequest request, HttpServletResponse response) {
JSONObject result = new JSONObject();

View File

@ -561,7 +561,7 @@ public class ServiceManager implements RecordListener<Service> {
return serviceMap.get(namespaceId);
}
public int getPagedService(String namespaceId, int startPage, int pageSize, String keyword, List<Service> serviceList) {
public int getPagedService(String namespaceId, int startPage, int pageSize, String keyword, String containedInstance, List<Service> serviceList) {
List<Service> matchList;
@ -572,7 +572,34 @@ public class ServiceManager implements RecordListener<Service> {
if (StringUtils.isNotBlank(keyword)) {
matchList = searchServices(namespaceId, ".*" + keyword + ".*");
} else {
matchList = new ArrayList<Service>(chooseServiceMap(namespaceId).values());
matchList = new ArrayList<>(chooseServiceMap(namespaceId).values());
}
if (StringUtils.isNotBlank(containedInstance)) {
boolean contained;
for (int i = 0; i < matchList.size(); i++) {
Service service = matchList.get(i);
contained = false;
List<Instance> instances = service.allIPs();
for (Instance instance : instances) {
if (containedInstance.contains(":")) {
if (StringUtils.equals(instance.getIp() + ":" + instance.getPort(), containedInstance)) {
contained = true;
break;
}
} else {
if (StringUtils.equals(instance.getIp(), containedInstance)) {
contained = true;
break;
}
}
}
if (!contained) {
matchList.remove(i);
i--;
}
}
}
if (pageSize >= matchList.size()) {

View File

@ -88,14 +88,6 @@ public class ClientBeatCheckTask implements Runnable {
// then remove obsolete instances:
for (Instance instance : instances) {
if (System.currentTimeMillis() - instance.getLastBeat() > service.getIpDeleteTimeout()) {
// protect threshold met:
if (service.meetProtectThreshold()) {
Loggers.SRV_LOG.info("protect threshold met, service: {}, ip: {}, healthy: {}, total: {}",
service.getName(), JSON.toJSONString(instance), service.healthyInstanceCount(), service.allIPs().size());
return;
}
// delete instance
Loggers.SRV_LOG.info("[AUTO-DELETE-IP] service: {}, ip: {}", service.getName(), JSON.toJSONString(instance));
deleteIP(instance);

View File

@ -56,7 +56,7 @@ public class GlobalExecutor {
Thread t = new Thread(r);
t.setDaemon(true);
t.setName("com.alibaba.nacos.naming.partition.task.dispatcher");
t.setName("com.alibaba.nacos.naming.distro.task.dispatcher");
return t;
}
@ -70,7 +70,7 @@ public class GlobalExecutor {
Thread t = new Thread(r);
t.setDaemon(true);
t.setName("com.alibaba.nacos.naming.partition.data.syncer");
t.setName("com.alibaba.nacos.naming.distro.data.syncer");
return t;
}

View File

@ -200,6 +200,29 @@ public class HttpClient {
}
}
public static void asyncHttpDeleteLarge(String url, List<String> headers, String content, AsyncCompletionHandler handler) throws Exception {
AsyncHttpClient.BoundRequestBuilder builder = asyncHttpClient.prepareDelete(url);
if (!CollectionUtils.isEmpty(headers)) {
for (String header : headers) {
builder.setHeader(header.split("=")[0], header.split("=")[1]);
}
}
builder.setBody(content.getBytes());
builder.setHeader("Content-Type", "application/json; charset=UTF-8");
builder.setHeader("Accept-Charset", "UTF-8");
builder.setHeader("Accept-Encoding", "gzip");
builder.setHeader("Content-Encoding", "gzip");
if (handler != null) {
builder.execute(handler);
} else {
builder.execute();
}
}
public static HttpResult httpPost(String url, List<String> headers, Map<String, String> paramValues) {
return httpPost(url, headers, paramValues, "UTF-8");
}

View File

@ -31,13 +31,15 @@ import java.util.*;
*/
public class NamingProxy {
private static final String DATA_ON_SYNC_URL = "/partition/datum";
private static final String DATA_ON_SYNC_URL = "/distro/datum";
private static final String DATA_GET_URL = "/partition/datum";
private static final String DATA_GET_URL = "/distro/datum";
private static final String TIMESTAMP_SYNC_URL = "/partition/timestamps";
private static final String ALL_DATA_GET_URL = "/distro/datums";
public static void syncTimestamps(Map<String, String> timestamps, String server) {
private static final String TIMESTAMP_SYNC_URL = "/distro/checksum";
public static void syncChecksums(Map<String, String> checksumMap, String server) {
try {
Map<String, String> headers = new HashMap<>(128);
@ -48,7 +50,7 @@ public class NamingProxy {
HttpClient.asyncHttpPutLarge("http://" + server + RunningConfig.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + TIMESTAMP_SYNC_URL + "?source=" + NetUtils.localServer(),
headers, JSON.toJSONBytes(timestamps),
headers, JSON.toJSONBytes(checksumMap),
new AsyncCompletionHandler() {
@Override
public Object onCompleted(Response response) throws Exception {
@ -90,6 +92,23 @@ public class NamingProxy {
+ result.code + " msg: " + result.content);
}
public static byte[] getAllData(String server) throws Exception {
Map<String, String> params = new HashMap<>(8);
HttpClient.HttpResult result = HttpClient.httpGet("http://" + server + RunningConfig.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + ALL_DATA_GET_URL, new ArrayList<>(), params);
if (HttpURLConnection.HTTP_OK == result.code) {
return result.content.getBytes();
}
throw new IOException("failed to req API: " + "http://" + server
+ RunningConfig.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_GET_URL + ". code: "
+ result.code + " msg: " + result.content);
}
public static boolean syncData(byte[] data, String curServer) throws Exception {
try {
Map<String, String> headers = new HashMap<>(128);

View File

@ -16,6 +16,7 @@
package com.alibaba.nacos.naming.misc;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.naming.cluster.ServerMode;
import com.alibaba.nacos.naming.consistency.ConsistencyService;
@ -99,38 +100,20 @@ public class SwitchManager implements RecordListener<SwitchDomain> {
throw new IllegalArgumentException("malformed factor");
}
update(dom);
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), dom);
}
return;
switchDomain = dom;
}
if (entry.equals(SwitchEntry.DISTRO_THRESHOLD)) {
Float threshold = Float.parseFloat(value);
if (threshold <= 0) {
throw new IllegalArgumentException("distroThreshold can not be zero or negative: " + threshold);
}
switchDomain.setDistroThreshold(threshold);
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
}
return;
}
if (entry.equals(SwitchEntry.CLIENT_BEAT_INTERVAL)) {
long clientBeatInterval = Long.parseLong(value);
switchDomain.setClientBeatInterval(clientBeatInterval);
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
}
return;
}
if (entry.equals(SwitchEntry.PUSH_VERSION)) {
@ -153,11 +136,6 @@ public class SwitchManager implements RecordListener<SwitchDomain> {
} else {
throw new IllegalArgumentException("unsupported client type: " + type);
}
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
}
return;
}
if (entry.equals(SwitchEntry.PUSH_CACHE_MILLIS)) {
@ -168,10 +146,6 @@ public class SwitchManager implements RecordListener<SwitchDomain> {
}
switchDomain.setDefaultPushCacheMillis(cacheMillis);
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
}
return;
}
// extremely careful while modifying this, cause it will affect all clients without pushing enabled
@ -183,50 +157,26 @@ public class SwitchManager implements RecordListener<SwitchDomain> {
}
switchDomain.setDefaultCacheMillis(cacheMillis);
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
}
return;
}
if (entry.equals(SwitchEntry.MASTERS)) {
List<String> masters = Arrays.asList(value.split(","));
switchDomain.setMasters(masters);
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
}
return;
}
if (entry.equals(SwitchEntry.DISTRO)) {
boolean enabled = Boolean.parseBoolean(value);
switchDomain.setDistroEnabled(enabled);
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
}
return;
}
if (entry.equals(SwitchEntry.CHECK)) {
boolean enabled = Boolean.parseBoolean(value);
switchDomain.setHealthCheckEnabled(enabled);
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
}
return;
}
if (entry.equals(SwitchEntry.PUSH_ENABLED)) {
boolean enabled = Boolean.parseBoolean(value);
switchDomain.setPushEnabled(enabled);
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
}
return;
}
if (entry.equals(SwitchEntry.SERVICE_STATUS_SYNC_PERIOD)) {
@ -237,10 +187,6 @@ public class SwitchManager implements RecordListener<SwitchDomain> {
}
switchDomain.setServiceStatusSynchronizationPeriodMillis(millis);
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
}
return;
}
if (entry.equals(SwitchEntry.SERVER_STATUS_SYNC_PERIOD)) {
@ -251,40 +197,24 @@ public class SwitchManager implements RecordListener<SwitchDomain> {
}
switchDomain.setServerStatusSynchronizationPeriodMillis(millis);
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
}
return;
}
if (entry.equals(SwitchEntry.HEALTH_CHECK_TIMES)) {
Integer times = Integer.parseInt(value);
switchDomain.setCheckTimes(times);
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
}
return;
}
if (entry.equals(SwitchEntry.DISABLE_ADD_IP)) {
boolean disableAddIP = Boolean.parseBoolean(value);
switchDomain.setDisableAddIP(disableAddIP);
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
}
return;
}
if (entry.equals(SwitchEntry.SEND_BEAT_ONLY)) {
boolean sendBeatOnly = Boolean.parseBoolean(value);
switchDomain.setSendBeatOnly(sendBeatOnly);
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
}
return;
}
if (entry.equals(SwitchEntry.LIMITED_URL_MAP)) {
@ -314,10 +244,6 @@ public class SwitchManager implements RecordListener<SwitchDomain> {
}
switchDomain.setLimitedUrlMap(limitedUrlMap);
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
}
return;
}
}
@ -327,37 +253,27 @@ public class SwitchManager implements RecordListener<SwitchDomain> {
if (!StringUtils.isNotEmpty(enabled)) {
switchDomain.setEnableStandalone(Boolean.parseBoolean(enabled));
}
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
}
return;
}
if (entry.equals(SwitchEntry.OVERRIDDEN_SERVER_STATUS)) {
String status = value;
switchDomain.setOverriddenServerStatus(status);
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
if (Constants.NULL_STRING.equals(status)) {
status = StringUtils.EMPTY;
}
return;
switchDomain.setOverriddenServerStatus(status);
}
if (entry.equals(SwitchEntry.SERVER_MODE)) {
String mode = value;
switchDomain.setServerMode(ServerMode.valueOf(mode).name());
if (!debug) {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
}
return;
}
throw new IllegalArgumentException("update entry not found: " + entry);
if (debug) {
update(switchDomain);
} else {
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), switchDomain);
}
} finally {
lock.unlock();
}

View File

@ -62,7 +62,7 @@ public class UtilsAndCommons {
public static final String NACOS_NAMING_RAFT_CONTEXT = "/raft";
public static final String NACOS_NAMING_PARTITION_CONTEXT = "/partition";
public static final String NACOS_NAMING_PARTITION_CONTEXT = "/distro";
public static final String NACOS_NAMING_OPERATOR_CONTEXT = "/operator";

View File

@ -15,7 +15,6 @@
*/
package com.alibaba.nacos.naming.web;
import com.alibaba.nacos.naming.boot.RunningConfig;
import com.alibaba.nacos.naming.controllers.*;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
@ -46,7 +45,7 @@ public class FilterBase {
initClassMethod(CatalogController.class);
initClassMethod(HealthController.class);
initClassMethod(RaftController.class);
initClassMethod(PartitionController.class);
initClassMethod(DistroController.class);
initClassMethod(OperatorController.class);
}