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 = "endpoint";
public final static String ENDPOINT_PORT = "endpointPort";
public final static String NAMESPACE = "namespace"; public final static String NAMESPACE = "namespace";
public final static String ACCESS_KEY = "accessKey"; public final static String ACCESS_KEY = "accessKey";
@ -39,7 +41,17 @@ public class PropertyKeyConst {
public final static String ENCODE = "encode"; public final static String ENCODE = "encode";
public final static String NAMING_LOAD_CACHE_AT_START = "namingLoadCacheAtStart"; 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_CLIENT_BEAT_THREAD_COUNT = "namingClientBeatThreadCount";
public final static String NAMING_POLLING_THREAD_COUNT = "namingPollingThreadCount"; 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 int WRITE_REDIRECT_CODE = 307;
public static final String SERVICE_INFO_SPLITER = "@@"; 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.config.utils.TenantUtil;
import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.StringUtils; import com.alibaba.nacos.client.utils.StringUtils;
import com.alibaba.nacos.client.utils.TemplateUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.io.IOException; import java.io.IOException;
@ -42,6 +43,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.Callable;
/** /**
* Config Impl * Config Impl
@ -73,19 +75,33 @@ public class NacosConfigService implements ConfigService {
} else { } else {
encode = encodeTmp.trim(); encode = encodeTmp.trim();
} }
String namespaceTmp = properties.getProperty(PropertyKeyConst.NAMESPACE); initNamespace(properties);
if (StringUtils.isBlank(namespaceTmp)) {
namespace = TenantUtil.getUserTenant();
properties.put(PropertyKeyConst.NAMESPACE, namespace);
} else {
namespace = namespaceTmp;
properties.put(PropertyKeyConst.NAMESPACE, namespace);
}
agent = new MetricsHttpAgent(new ServerHttpAgent(properties)); agent = new MetricsHttpAgent(new ServerHttpAgent(properties));
agent.start(); agent.start();
worker = new ClientWorker(agent, configFilterChainManager); 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 @Override
public String getConfig(String dataId, String group, long timeoutMs) throws NacosException { public String getConfig(String dataId, String group, long timeoutMs) throws NacosException {
return getConfigInner(namespace, dataId, group, timeoutMs); 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.impl.SpasAdapter;
import com.alibaba.nacos.client.config.utils.IOUtils; import com.alibaba.nacos.client.config.utils.IOUtils;
import com.alibaba.nacos.client.identify.STSConfig; 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.JSONUtils;
import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.client.utils.ParamUtil;
@ -41,6 +42,7 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.Callable;
/** /**
* Server Agent * Server Agent
@ -197,29 +199,29 @@ public class ServerHttpAgent implements HttpAgent {
public ServerHttpAgent(ServerListManager mgr, Properties properties) { public ServerHttpAgent(ServerListManager mgr, Properties properties) {
serverListMgr = mgr; serverListMgr = mgr;
String ak = properties.getProperty(PropertyKeyConst.ACCESS_KEY); init(properties);
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;
}
} }
public ServerHttpAgent(Properties properties) throws NacosException { 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); 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); String ak = properties.getProperty(PropertyKeyConst.ACCESS_KEY);
if (StringUtils.isBlank(ak)) { if (StringUtils.isBlank(ak)) {
accessKey = SpasAdapter.getAk(); accessKey = SpasAdapter.getAk();
@ -273,8 +275,9 @@ public class ServerHttpAgent implements HttpAgent {
} }
} }
String stsResponse = getSTSResponse(); String stsResponse = getSTSResponse();
STSCredential stsCredentialTmp = (STSCredential)JSONUtils.deserializeObject(stsResponse, STSCredential stsCredentialTmp = (STSCredential) JSONUtils.deserializeObject(stsResponse,
new TypeReference<STSCredential>() {}); new TypeReference<STSCredential>() {
});
sTSCredential = stsCredentialTmp; sTSCredential = stsCredentialTmp;
LOGGER.info("[getSTSCredential] code:{}, accessKeyId:{}, lastUpdated:{}, expiration:{}", sTSCredential.getCode(), LOGGER.info("[getSTSCredential] code:{}, accessKeyId:{}, lastUpdated:{}, expiration:{}", sTSCredential.getCode(),
sTSCredential.getAccessKeyId(), sTSCredential.getLastUpdated(), sTSCredential.getExpiration()); sTSCredential.getAccessKeyId(), sTSCredential.getLastUpdated(), sTSCredential.getExpiration());
@ -291,7 +294,7 @@ public class ServerHttpAgent implements HttpAgent {
int respCode; int respCode;
String response; String response;
try { try {
conn = (HttpURLConnection)new URL(securityCredentialsUrl).openConnection(); conn = (HttpURLConnection) new URL(securityCredentialsUrl).openConnection();
conn.setRequestMethod("GET"); conn.setRequestMethod("GET");
conn.setConnectTimeout(ParamUtil.getConnectTimeout() > 100 ? ParamUtil.getConnectTimeout() : 100); conn.setConnectTimeout(ParamUtil.getConnectTimeout() > 100 ? ParamUtil.getConnectTimeout() : 100);
conn.setReadTimeout(1000); 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.EventDispatcher.ServerlistChangeEvent;
import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult;
import com.alibaba.nacos.client.config.utils.IOUtils; import com.alibaba.nacos.client.config.utils.IOUtils;
import com.alibaba.nacos.client.utils.EnvUtil; import com.alibaba.nacos.client.utils.*;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.client.utils.StringUtils;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.util.ArrayList; import java.util.*;
import java.util.Collections; import java.util.concurrent.Callable;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
@ -93,6 +86,8 @@ public class ServerListManager {
public ServerListManager(String endpoint, String namespace) throws NacosException { public ServerListManager(String endpoint, String namespace) throws NacosException {
isFixed = false; isFixed = false;
isStarted = false; isStarted = false;
endpoint = initEndpoint(endpoint);
if (StringUtils.isBlank(endpoint)) { if (StringUtils.isBlank(endpoint)) {
throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "endpoint is blank"); throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "endpoint is blank");
} }
@ -158,10 +153,8 @@ public class ServerListManager {
} }
private void initParam(Properties properties) { private void initParam(Properties properties) {
String endpointTmp = properties.getProperty(PropertyKeyConst.ENDPOINT); endpoint = initEndpoint(properties.getProperty(PropertyKeyConst.ENDPOINT));
if (!StringUtils.isBlank(endpointTmp)) {
endpoint = endpointTmp;
}
String contentPathTmp = properties.getProperty(PropertyKeyConst.CONTEXT_PATH); String contentPathTmp = properties.getProperty(PropertyKeyConst.CONTEXT_PATH);
if (!StringUtils.isBlank(contentPathTmp)) { if (!StringUtils.isBlank(contentPathTmp)) {
contentPath = 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 { public synchronized void start() throws NacosException {
if (isStarted || isFixed) { if (isStarted || isFixed) {

View File

@ -25,6 +25,8 @@ public class Constants {
public static final String SECRET_KEY = "secretKey"; 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 PROPERTIES_FILENAME = "spas.properties";
public static final String CREDENTIAL_PATH = "/home/admin/.spas_key/"; 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_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_ACCESS_KEY = "spas_accessKey";
public static final String ENV_SECRET_KEY = "spas_secretKey"; 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 = ""; 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 com.alibaba.nacos.client.utils.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.io.File; import java.io.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.Properties; import java.util.Properties;
import java.util.Timer; import java.util.Timer;
@ -144,6 +140,7 @@ public class CredentialWatcher {
String accessKey = null; String accessKey = null;
String secretKey = null; String secretKey = null;
String tenantId = null;
if (propertiesIS == null) { if (propertiesIS == null) {
propertyPath = null; propertyPath = null;
accessKey = System.getenv(Constants.ENV_ACCESS_KEY); accessKey = System.getenv(Constants.ENV_ACCESS_KEY);
@ -183,6 +180,9 @@ public class CredentialWatcher {
if (properties.containsKey(Constants.SECRET_KEY)) { if (properties.containsKey(Constants.SECRET_KEY)) {
secretKey = properties.getProperty(Constants.SECRET_KEY); secretKey = properties.getProperty(Constants.SECRET_KEY);
} }
if (properties.containsKey(Constants.TENANT_ID)) {
tenantId = properties.getProperty(Constants.TENANT_ID);
}
} else { } else {
if (properties.containsKey(Constants.DOCKER_ACCESS_KEY)) { if (properties.containsKey(Constants.DOCKER_ACCESS_KEY)) {
accessKey = properties.getProperty(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)) { if (properties.containsKey(Constants.DOCKER_SECRET_KEY)) {
secretKey = properties.getProperty(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(); 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()) { if (!credential.valid()) {
SpasLogger.warn("[1] Credential file missing required property {} Credential file missing {} or {}", SpasLogger.warn("[1] Credential file missing required property {} Credential file missing {} or {}",
appName, Constants.ACCESS_KEY, Constants.SECRET_KEY); appName, Constants.ACCESS_KEY, Constants.SECRET_KEY);

View File

@ -26,13 +26,16 @@ public class Credentials implements SpasCredential {
private volatile String secretKey; 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.accessKey = accessKey;
this.secretKey = secretKey; this.secretKey = secretKey;
this.tenantId = tenantId;
} }
public Credentials() { public Credentials() {
this(null, null); this(null, null, null);
} }
public String getAccessKey() { public String getAccessKey() {
@ -51,16 +54,24 @@ public class Credentials implements SpasCredential {
this.secretKey = secretKey; this.secretKey = secretKey;
} }
public String getTenantId() {
return tenantId;
}
public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}
public boolean valid() { 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) { public boolean identical(Credentials other) {
return this == other || return this == other || (other != null
(other != null && && (accessKey == null && other.accessKey == null
(accessKey == null && other.accessKey == null || accessKey != null && accessKey.equals(other.accessKey)) || accessKey != null && accessKey.equals(other.accessKey))
&& && (secretKey == null && other.secretKey == null
(secretKey == null && other.secretKey == null || secretKey != null && secretKey.equals( || secretKey != null && secretKey.equals(other.secretKey)));
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.ConfigurationFactory;
import org.apache.logging.log4j.core.config.ConfigurationSource; import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.composite.CompositeConfiguration; 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.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import static org.apache.logging.log4j.core.config.ConfigurationFactory.CONFIGURATION_FILE_PROPERTY;
/** /**
* Support for Log4j version 2.7 or higher * 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 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>(); private Set<String> locationList = new HashSet<String>();
public Log4J2NacosLogging() { public Log4J2NacosLogging() {
@ -63,9 +71,13 @@ public class Log4J2NacosLogging extends AbstractNacosLogging {
@Override @Override
public void loadConfiguration() { public void loadConfiguration() {
String config = findConfig(getCurrentlySupportedConfigLocations()); if (locationList.isEmpty()) {
if (config != null) { return;
locationList.add(config); }
List<String> configList = findConfig(getCurrentlySupportedConfigLocations());
if (configList != null) {
locationList.addAll(configList);
} }
final List<AbstractConfiguration> configurations = new ArrayList<AbstractConfiguration>(); final List<AbstractConfiguration> configurations = new ArrayList<AbstractConfiguration>();
@ -112,22 +124,35 @@ public class Log4J2NacosLogging extends AbstractNacosLogging {
List<String> supportedConfigLocations = new ArrayList<String>(); List<String> supportedConfigLocations = new ArrayList<String>();
if (ClassUtils.isPresent(YAML_PARSER_CLASS_NAME)) { 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)) { 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.xml");
supportedConfigLocations.add("log4j2-test.xml");
return supportedConfigLocations.toArray(new String[supportedConfigLocations.size()]); 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) { for (String location : locations) {
ClassLoader defaultClassLoader = ClassUtils.getDefaultClassLoader(); ClassLoader defaultClassLoader = ClassUtils.getDefaultClassLoader();
if (defaultClassLoader != null && defaultClassLoader.getResource(location) != null) { if (defaultClassLoader != null && defaultClassLoader.getResource(location) != null) {
return "classpath:" + location; List<String> list = new ArrayList<String>();
list.add("classpath:" + location);
return list;
} }
} }
return null; return null;

View File

@ -16,6 +16,7 @@
package com.alibaba.nacos.client.naming; package com.alibaba.nacos.client.naming;
import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.SystemPropertyKeyConst;
import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService; 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.ListView;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.api.selector.AbstractSelector; 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.BeatInfo;
import com.alibaba.nacos.client.naming.beat.BeatReactor; import com.alibaba.nacos.client.naming.beat.BeatReactor;
import com.alibaba.nacos.client.naming.core.Balancer; 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.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.StringUtils; import com.alibaba.nacos.client.naming.utils.StringUtils;
import com.alibaba.nacos.client.naming.utils.UtilAndComs; 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.BooleanUtils;
import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.math.NumberUtils;
@ -40,13 +44,13 @@ import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.Callable;
/** /**
* @author nkorange * @author nkorange
*/ */
@SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule") @SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule")
public class NacosNamingService implements NamingService { public class NacosNamingService implements NamingService {
/** /**
* Each Naming instance should have different namespace. * Each Naming instance should have different namespace.
*/ */
@ -68,74 +72,179 @@ public class NacosNamingService implements NamingService {
private NamingProxy serverProxy; 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)) { public NacosNamingService(Properties properties) {
namespace = UtilAndComs.DEFAULT_NAMESPACE_ID;
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); logName = System.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME);
if (StringUtils.isEmpty(logName)) { 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"); cacheDir = System.getProperty("com.alibaba.nacos.naming.cache.dir");
if (StringUtils.isEmpty(cacheDir)) { if (StringUtils.isEmpty(cacheDir)) {
cacheDir = System.getProperty("user.home") + "/nacos/naming/" + namespace; cacheDir = System.getProperty("user.home") + "/nacos/naming/" + namespace;
} }
} }
public NacosNamingService(String serverList) { private void initEndpoint(Properties properties) {
if (properties == null) {
this.serverList = serverList; return;
init(); }
eventDispatcher = new EventDispatcher();
serverProxy = new NamingProxy(namespace, endpoint, serverList); String endpointUrl = TemplateUtils.stringEmptyAndThenExecute(properties.getProperty(PropertyKeyConst.ENDPOINT), new Callable<String>() {
beatReactor = new BeatReactor(serverProxy); @Override
hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir); 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(); if (properties != null) {
tmpNamespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
serverList = properties.getProperty(PropertyKeyConst.SERVER_ADDR);
if (StringUtils.isNotEmpty(properties.getProperty(PropertyKeyConst.NAMESPACE))) {
namespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
} }
if (StringUtils.isNotEmpty(properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME))) { tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable<String>() {
logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME); @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; tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable<String>() {
if (StringUtils.isNotEmpty(properties.getProperty(PropertyKeyConst.NAMING_LOAD_CACHE_AT_START))) { @Override
loadCacheAtStart = BooleanUtils.toBoolean( public String call() {
properties.getProperty(PropertyKeyConst.NAMING_LOAD_CACHE_AT_START)); String namespace = CredentialService.getInstance().getCredential().getTenantId();
} LogUtils.NAMING_LOGGER.info("initializer namespace from Credential Module " + namespace);
return namespace;
}
});
int clientBeatThreadCount = NumberUtils.toInt( tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable<String>() {
properties.getProperty(PropertyKeyConst.NAMING_CLIENT_BEAT_THREAD_COUNT), @Override
UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT); public String call() {
return UtilAndComs.DEFAULT_NAMESPACE_ID;
}
});
namespace = tmpNamespace;
}
int pollingThreadCount = NumberUtils.toInt(properties.getProperty(PropertyKeyConst.NAMING_POLLING_THREAD_COUNT), private void initWebRootContext() {
UtilAndComs.DEFAULT_POLLING_THREAD_COUNT); // support the web context with ali-yun if the app deploy by EDAS
final String webContext = System.getProperty(SystemPropertyKeyConst.NAMING_WEB_CONTEXT);
eventDispatcher = new EventDispatcher(); TemplateUtils.stringNotEmptyAndThenExecute(webContext, new Runnable() {
serverProxy = new NamingProxy(namespace, endpoint, serverList); @Override
beatReactor = new BeatReactor(serverProxy, clientBeatThreadCount); public void run() {
hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir, loadCacheAtStart, pollingThreadCount); 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 @Override

View File

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

View File

@ -30,16 +30,22 @@ import java.net.URLEncoder;
import java.util.*; import java.util.*;
import java.util.zip.GZIPInputStream; 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 * @author nkorange
*/ */
public class HttpClient { public class HttpClient {
public static final int TIME_OUT_MILLIS = Integer.getInteger("com.alibaba.nacos.client.naming.ctimeout", 50000); public static final int TIME_OUT_MILLIS = Integer
public static final int CON_TIME_OUT_MILLIS = Integer.getInteger("com.alibaba.nacos.client.naming.ctimeout", 3000); .getInteger("com.alibaba.nacos.client.naming.ctimeout", 50000);
private static final boolean ENABLE_HTTPS = Boolean.getBoolean("com.alibaba.nacos.client.naming.tls.enable"); public static final int CON_TIME_OUT_MILLIS = Integer
.getInteger("com.alibaba.nacos.client.naming.ctimeout", 3000);
private static final boolean ENABLE_HTTPS = Boolean
.getBoolean("com.alibaba.nacos.client.naming.tls.enable");
private static final String POST = "POST";
private static final String PUT = "PUT";
static { static {
// limit max redirection // limit max redirection
@ -67,10 +73,19 @@ public class HttpClient {
conn = (HttpURLConnection) new URL(url).openConnection(); conn = (HttpURLConnection) new URL(url).openConnection();
setHeaders(conn, headers, encoding);
conn.setConnectTimeout(CON_TIME_OUT_MILLIS); conn.setConnectTimeout(CON_TIME_OUT_MILLIS);
conn.setReadTimeout(TIME_OUT_MILLIS); conn.setReadTimeout(TIME_OUT_MILLIS);
conn.setRequestMethod(method); 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(); conn.connect();
NAMING_LOGGER.debug("Request from server: " + url); NAMING_LOGGER.debug("Request from server: " + url);
return getResult(conn); return getResult(conn);
@ -78,7 +93,7 @@ public class HttpClient {
try { try {
if (conn != null) { if (conn != null) {
NAMING_LOGGER.warn("failed to request " + conn.getURL() + " from " NAMING_LOGGER.warn("failed to request " + conn.getURL() + " from "
+ InetAddress.getByName(conn.getURL().getHost()).getHostAddress()); + InetAddress.getByName(conn.getURL().getHost()).getHostAddress());
} }
} catch (Exception e1) { } catch (Exception e1) {
NAMING_LOGGER.error("[NA] failed to request ", 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) private static String encodingParams(Map<String, String> params, String encoding)
throws UnsupportedEncodingException { throws UnsupportedEncodingException {
StringBuilder sb = new StringBuilder();
if (null == params || params.isEmpty()) { if (null == params || params.isEmpty()) {
return null; return null;
} }
params.put("encoding", encoding); params.put("encoding", encoding);
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) { for (Map.Entry<String, String> entry : params.entrySet()) {
if (StringUtils.isEmpty(entry.getValue())) { if (StringUtils.isEmpty(entry.getValue())) {
@ -175,6 +190,9 @@ public class HttpClient {
sb.append("&"); sb.append("&");
} }
if (sb.length() > 0) {
sb = sb.deleteCharAt(sb.length() - 1);
}
return sb.toString(); 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.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference; 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.exception.NacosException;
import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.api.naming.pojo.Instance; 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.AbstractSelector;
import com.alibaba.nacos.api.selector.ExpressionSelector; import com.alibaba.nacos.api.selector.ExpressionSelector;
import com.alibaba.nacos.api.selector.SelectorType; import com.alibaba.nacos.api.selector.SelectorType;
import com.alibaba.nacos.client.config.impl.SpasAdapter;
import com.alibaba.nacos.client.monitor.MetricsMonitor; import com.alibaba.nacos.client.monitor.MetricsMonitor;
import com.alibaba.nacos.client.naming.beat.BeatInfo; import com.alibaba.nacos.client.naming.beat.BeatInfo;
import com.alibaba.nacos.client.naming.utils.*; import com.alibaba.nacos.client.naming.utils.*;
import com.alibaba.nacos.client.naming.utils.*;
import com.alibaba.nacos.client.utils.TemplateUtils;
import com.alibaba.nacos.common.util.HttpMethod; import com.alibaba.nacos.common.util.HttpMethod;
import com.alibaba.nacos.common.util.UuidUtils; import com.alibaba.nacos.common.util.UuidUtils;
@ -35,10 +41,7 @@ import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.util.*; import java.util.*;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.*;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; 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 static final int DEFAULT_SERVER_PORT = 8848;
private int serverPort = DEFAULT_SERVER_PORT;
private String namespaceId; private String namespaceId;
private String endpoint; private String endpoint;
@ -63,7 +68,7 @@ public class NamingProxy {
private long vipSrvRefInterMillis = TimeUnit.SECONDS.toMillis(30); private long vipSrvRefInterMillis = TimeUnit.SECONDS.toMillis(30);
private ScheduledExecutorService executorService; private Properties properties;
public NamingProxy(String namespaceId, String endpoint, String serverList) { 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 @Override
public Thread newThread(Runnable r) { public Thread newThread(Runnable r) {
Thread t = new Thread(r); Thread t = new Thread(r);
@ -100,12 +113,7 @@ public class NamingProxy {
try { try {
String urlString = "http://" + endpoint + "/nacos/serverlist"; String urlString = "http://" + endpoint + "/nacos/serverlist";
List<String> headers = 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());
HttpClient.HttpResult result = HttpClient.httpGet(urlString, headers, null, UtilAndComs.ENCODING); HttpClient.HttpResult result = HttpClient.httpGet(urlString, headers, null, UtilAndComs.ENCODING);
if (HttpURLConnection.HTTP_OK != result.code) { if (HttpURLConnection.HTTP_OK != result.code) {
@ -305,17 +313,13 @@ public class NamingProxy {
throws NacosException { throws NacosException {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
long end = 0; long end = 0;
checkSignature(params);
List<String> headers = Arrays.asList("Client-Version", UtilAndComs.VERSION, List<String> headers = builderHeaders();
"User-Agent", UtilAndComs.VERSION,
"Accept-Encoding", "gzip,deflate,sdch",
"Connection", "Keep-Alive",
"RequestId", UuidUtils.generateUuid());
String url; String url;
if (!curServer.contains(UtilAndComs.SERVER_ADDR_IP_SPLITER)) { 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; 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() { public String getNamespaceId() {
return namespaceId; 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 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 ENCODING = "UTF-8";
public static final String ENV_LIST_KEY = "envList"; 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 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 String DEFAULT_NAMESPACE_ID = "public";
public static final int REQUEST_DOMAIN_RETRY_COUNT = 3; 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 String SERVER_ADDR_IP_SPLITER = ":";
public static final int DEFAULT_CLIENT_BEAT_THREAD_COUNT = Runtime.getRuntime().availableProcessors() > 1 ? public static final int DEFAULT_CLIENT_BEAT_THREAD_COUNT = Runtime.getRuntime()
Runtime.getRuntime().availableProcessors() / 2 : 1; .availableProcessors() > 1 ? Runtime.getRuntime().availableProcessors() / 2
: 1;
public static final int DEFAULT_POLLING_THREAD_COUNT = Runtime.getRuntime().availableProcessors() > 1 ? public static final int DEFAULT_POLLING_THREAD_COUNT = Runtime.getRuntime()
Runtime.getRuntime().availableProcessors() / 2 : 1; .availableProcessors() > 1 ? Runtime.getRuntime().availableProcessors() / 2
: 1;
} }

View File

@ -37,6 +37,11 @@ public class StringUtils {
return true; return true;
} }
public static boolean isNotBlank(String str) {
return !isBlank(str);
}
public static boolean isNotEmpty(String str) { public static boolean isNotEmpty(String str) {
return !StringUtils.isEmpty(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); GlobalExecutor.registerServerStatusReporter(new ServerStatusReporter(), 5000);
} }
private List<Server> refreshServerList() { public List<Server> refreshServerList() {
List<Server> result = new ArrayList<>(); List<Server> result = new ArrayList<>();

View File

@ -77,24 +77,6 @@ public interface ConsistencyService {
*/ */
void unlisten(String key, RecordListener listener) throws NacosException; 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 * 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.api.exception.NacosException;
import com.alibaba.nacos.naming.consistency.ephemeral.EphemeralConsistencyService; import com.alibaba.nacos.naming.consistency.ephemeral.EphemeralConsistencyService;
import com.alibaba.nacos.naming.consistency.persistent.PersistentConsistencyService; import com.alibaba.nacos.naming.consistency.persistent.PersistentConsistencyService;
import com.alibaba.nacos.naming.core.DistroMapper;
import com.alibaba.nacos.naming.pojo.Record; import com.alibaba.nacos.naming.pojo.Record;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** /**
* Publish execution delegate * Consistency delegate
* *
* @author nkorange * @author nkorange
* @since 1.0.0 * @since 1.0.0
@ -35,9 +34,6 @@ public class DelegateConsistencyServiceImpl implements ConsistencyService {
@Autowired @Autowired
private PersistentConsistencyService persistentConsistencyService; private PersistentConsistencyService persistentConsistencyService;
@Autowired
private DistroMapper distroMapper;
@Autowired @Autowired
private EphemeralConsistencyService ephemeralConsistencyService; private EphemeralConsistencyService ephemeralConsistencyService;
@ -58,6 +54,14 @@ public class DelegateConsistencyServiceImpl implements ConsistencyService {
@Override @Override
public void listen(String key, RecordListener listener) throws NacosException { 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); mapConsistencyService(key).listen(key, listener);
} }
@ -66,16 +70,6 @@ public class DelegateConsistencyServiceImpl implements ConsistencyService {
mapConsistencyService(key).unlisten(key, listener); 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 @Override
public boolean isAvailable() { public boolean isAvailable() {
return ephemeralConsistencyService.isAvailable() && persistentConsistencyService.isAvailable(); return ephemeralConsistencyService.isAvailable() && persistentConsistencyService.isAvailable();

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * 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.consistency.Datum;
import com.alibaba.nacos.naming.core.Instances; import com.alibaba.nacos.naming.core.Instances;
@ -80,4 +80,8 @@ public class DataStore {
} }
return count; 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 * See the License for the specific language governing permissions and
* limitations under the License. * 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.ServerListManager;
import com.alibaba.nacos.naming.cluster.servers.Server; import com.alibaba.nacos.naming.cluster.servers.Server;
import com.alibaba.nacos.naming.cluster.servers.ServerChangeListener; import com.alibaba.nacos.naming.cluster.servers.ServerChangeListener;
@ -30,18 +29,12 @@ import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; 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.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import static org.apache.commons.lang3.CharEncoding.UTF_8;
/** /**
* Data replicator * Data replicator
* *
@ -71,8 +64,6 @@ public class DataSyncer implements ServerChangeListener {
private List<Server> servers; private List<Server> servers;
private boolean initialized = false;
@PostConstruct @PostConstruct
public void init() { public void init() {
serverListManager.listen(this); serverListManager.listen(this);
@ -175,33 +166,6 @@ public class DataSyncer implements ServerChangeListener {
@Override @Override
public void run() { 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 { try {
// send local timestamps to other servers: // send local timestamps to other servers:
Map<String, String> keyChecksums = new HashMap<>(64); Map<String, String> keyChecksums = new HashMap<>(64);
@ -225,7 +189,7 @@ public class DataSyncer implements ServerChangeListener {
if (NetUtils.localServer().equals(member.getKey())) { if (NetUtils.localServer().equals(member.getKey())) {
continue; continue;
} }
NamingProxy.syncTimestamps(keyChecksums, member.getKey()); NamingProxy.syncChecksums(keyChecksums, member.getKey());
} }
} catch (Exception e) { } catch (Exception e) {
Loggers.EPHEMERAL.error("timed sync task failed.", 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; return key + UtilsAndCommons.CACHE_KEY_SPLITER + targetServer;
} }
public boolean isInitialized() {
return initialized;
}
@Override @Override
public void onChangeServerList(List<Server> latestMembers) { public void onChangeServerList(List<Server> latestMembers) {

View File

@ -13,22 +13,28 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * 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.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.cluster.transport.Serializer;
import com.alibaba.nacos.naming.consistency.RecordListener;
import com.alibaba.nacos.naming.consistency.Datum; import com.alibaba.nacos.naming.consistency.Datum;
import com.alibaba.nacos.naming.consistency.KeyBuilder; 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.consistency.ephemeral.EphemeralConsistencyService;
import com.alibaba.nacos.naming.core.DistroMapper; import com.alibaba.nacos.naming.core.DistroMapper;
import com.alibaba.nacos.naming.core.Instances; import com.alibaba.nacos.naming.core.Instances;
import com.alibaba.nacos.naming.misc.Loggers; import com.alibaba.nacos.naming.core.Service;
import com.alibaba.nacos.naming.misc.NamingProxy; import com.alibaba.nacos.naming.misc.*;
import com.alibaba.nacos.naming.pojo.Record; import com.alibaba.nacos.naming.pojo.Record;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -37,7 +43,7 @@ import java.util.concurrent.ConcurrentHashMap;
/** /**
* A consistency protocol algorithm called <b>Partition</b> * A consistency protocol algorithm called <b>Partition</b>
* <p> * <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 * 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 * and synchronized by its responsible server. So every Nacos server only handles writings
* for a subset of the total service data. * for a subset of the total service data.
@ -48,8 +54,8 @@ import java.util.concurrent.ConcurrentHashMap;
* @author nkorange * @author nkorange
* @since 1.0.0 * @since 1.0.0
*/ */
@Service("partitionConsistencyService") @org.springframework.stereotype.Service("distroConsistencyService")
public class PartitionConsistencyServiceImpl implements EphemeralConsistencyService { public class DistroConsistencyServiceImpl implements EphemeralConsistencyService {
@Autowired @Autowired
private DistroMapper distroMapper; private DistroMapper distroMapper;
@ -66,8 +72,52 @@ public class PartitionConsistencyServiceImpl implements EphemeralConsistencyServ
@Autowired @Autowired
private Serializer serializer; private Serializer serializer;
@Autowired
private ServerListManager serverListManager;
@Autowired
private SwitchDomain switchDomain;
private boolean initialized = false;
private volatile Map<String, List<RecordListener>> listeners = new ConcurrentHashMap<>(); 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 @Override
public void put(String key, Record value) throws NacosException { public void put(String key, Record value) throws NacosException {
onPut(key, value); 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> toUpdateKeys = new ArrayList<>();
List<String> toRemoveKeys = new ArrayList<>(); List<String> toRemoveKeys = new ArrayList<>();
for (Map.Entry<String, String> entry : timestamps.entrySet()) { for (Map.Entry<String, String> entry : checksumMap.entrySet()) {
if (isResponsible(entry.getKey())) { if (distroMapper.responsible(KeyBuilder.getServiceName(entry.getKey()))) {
// this key should not be sent from remote server: // this key should not be sent from remote server:
Loggers.EPHEMERAL.error("receive responsible key timestamp of " + entry.getKey() + " from " + server); Loggers.EPHEMERAL.error("receive responsible key timestamp of " + entry.getKey() + " from " + server);
// abort the procedure: // abort the procedure:
@ -146,7 +196,7 @@ public class PartitionConsistencyServiceImpl implements EphemeralConsistencyServ
continue; continue;
} }
if (!timestamps.containsKey(key)) { if (!checksumMap.containsKey(key)) {
toRemoveKeys.add(key); toRemoveKeys.add(key);
} }
} }
@ -163,31 +213,71 @@ public class PartitionConsistencyServiceImpl implements EphemeralConsistencyServ
try { try {
byte[] result = NamingProxy.getData(toUpdateKeys, server); byte[] result = NamingProxy.getData(toUpdateKeys, server);
if (result.length > 0) { processData(result);
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);
}
}
}
}
} catch (Exception e) { } catch (Exception e) {
Loggers.EPHEMERAL.error("get data from " + server + " failed!", 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 @Override
public void listen(String key, RecordListener listener) throws NacosException { public void listen(String key, RecordListener listener) throws NacosException {
if (!listeners.containsKey(key)) { 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 @Override
public boolean isAvailable() { 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 * See the License for the specific language governing permissions and
* limitations under the License. * 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.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -27,19 +27,19 @@ import org.springframework.stereotype.Component;
@Component @Component
public class PartitionConfig { public class PartitionConfig {
@Value("${nacos.naming.partition.taskDispatchPeriod}") @Value("${nacos.naming.distro.taskDispatchPeriod}")
private int taskDispatchPeriod = 2000; private int taskDispatchPeriod = 2000;
@Value("${nacos.naming.partition.batchSyncKeyCount}") @Value("${nacos.naming.distro.batchSyncKeyCount}")
private int batchSyncKeyCount = 1000; private int batchSyncKeyCount = 1000;
@Value("${nacos.naming.partition.initDataRatio}") @Value("${nacos.naming.distro.initDataRatio}")
private float initDataRatio = 0.9F; private float initDataRatio = 0.9F;
@Value("${nacos.naming.partition.syncRetryDelay}") @Value("${nacos.naming.distro.syncRetryDelay}")
private long syncRetryDelay = 5000L; private long syncRetryDelay = 5000L;
@Value("${nacos.naming.partition.taskDispatchThreadCount}") @Value("${nacos.naming.distro.taskDispatchThreadCount}")
private int taskDispatchThreadCount = Runtime.getRuntime().availableProcessors(); private int taskDispatchThreadCount = Runtime.getRuntime().availableProcessors();
public int getTaskDispatchPeriod() { public int getTaskDispatchPeriod() {

View File

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

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * 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.cluster.servers.Server;
import com.alibaba.nacos.naming.misc.GlobalExecutor; import com.alibaba.nacos.naming.misc.GlobalExecutor;

View File

@ -16,10 +16,12 @@
package com.alibaba.nacos.naming.consistency.persistent.raft; package com.alibaba.nacos.naming.consistency.persistent.raft;
import com.alibaba.nacos.api.exception.NacosException; 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.Datum;
import com.alibaba.nacos.naming.consistency.RecordListener;
import com.alibaba.nacos.naming.consistency.persistent.PersistentConsistencyService; import com.alibaba.nacos.naming.consistency.persistent.PersistentConsistencyService;
import com.alibaba.nacos.naming.misc.Loggers; import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.naming.pojo.Record; import com.alibaba.nacos.naming.pojo.Record;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -36,6 +38,9 @@ public class RaftConsistencyServiceImpl implements PersistentConsistencyService
@Autowired @Autowired
private RaftCore raftCore; private RaftCore raftCore;
@Autowired
private SwitchDomain switchDomain;
@Override @Override
public void put(String key, Record value) throws NacosException { public void put(String key, Record value) throws NacosException {
try { try {
@ -71,19 +76,9 @@ public class RaftConsistencyServiceImpl implements PersistentConsistencyService
raftCore.unlisten(key, listener); raftCore.unlisten(key, listener);
} }
@Override
public boolean isResponsible(String key) {
return false;
}
@Override
public String getResponsibleServer(String key) {
return null;
}
@Override @Override
public boolean isAvailable() { public boolean isAvailable() {
return raftCore.isInitialized(); return raftCore.isInitialized() || ServerStatus.UP.name().equals(switchDomain.getOverriddenServerStatus());
} }
public void onPut(Datum datum, RaftPeer source) throws NacosException { 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.apache.commons.lang3.math.NumberUtils;
import org.javatuples.Pair; import org.javatuples.Pair;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; 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_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() { private ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
@Override @Override
@ -228,8 +229,7 @@ public class RaftCore {
if (!isLeader()) { if (!isLeader()) {
Map<String, String> params = new HashMap<>(1); Map<String, String> params = new HashMap<>(1);
params.put("key", URLEncoder.encode(key, "UTF-8")); params.put("key", URLEncoder.encode(key, "UTF-8"));
raftProxy.proxy(getLeader().ip, API_DEL, params, HttpMethod.DELETE);
raftProxy.proxyGET(getLeader().ip, API_DEL, params);
return; return;
} }
@ -244,7 +244,7 @@ public class RaftCore {
for (final String server : peers.allServersWithoutMySelf()) { for (final String server : peers.allServersWithoutMySelf()) {
String url = buildURL(server, API_ON_DEL); String url = buildURL(server, API_ON_DEL);
HttpClient.asyncHttpPostLarge(url, null, JSON.toJSONString(json) HttpClient.asyncHttpDeleteLarge(url, null, JSON.toJSONString(json)
, new AsyncCompletionHandler<Integer>() { , new AsyncCompletionHandler<Integer>() {
@Override @Override
public Integer onCompleted(Response response) throws Exception { 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) { if (!Objects.equals(peer, candidate) && peer.state == RaftPeer.State.LEADER) {
try { try {
String url = RaftCore.buildURL(peer.ip, RaftCore.API_GET_PEER); 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 @Override
public Integer onCompleted(Response response) throws Exception { public Integer onCompleted(Response response) throws Exception {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) { if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {

View File

@ -15,9 +15,11 @@
*/ */
package com.alibaba.nacos.naming.consistency.persistent.raft; 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.boot.RunningConfig;
import com.alibaba.nacos.naming.misc.HttpClient; import com.alibaba.nacos.naming.misc.HttpClient;
import com.alibaba.nacos.naming.misc.UtilsAndCommons; import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.net.HttpURLConnection; 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 { public void proxyPostLarge(String server, String api, String content, Map<String, String> headers) throws Exception {
// do proxy // do proxy
if (!server.contains(UtilsAndCommons.IP_PORT_SPLITER)) { 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.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; 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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -49,54 +50,7 @@ public class CatalogController {
@Autowired @Autowired
protected ServiceManager serviceManager; protected ServiceManager serviceManager;
@RequestMapping(value = "/serviceList") @RequestMapping(value = "/service")
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")
public ServiceDetailView serviceDetail(HttpServletRequest request) throws Exception { public ServiceDetailView serviceDetail(HttpServletRequest request) throws Exception {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
@ -130,15 +84,15 @@ public class CatalogController {
return detailView; return detailView;
} }
@RequestMapping(value = "/instanceList") @RequestMapping(value = "/instances")
public JSONObject instanceList(HttpServletRequest request) throws Exception { public JSONObject instanceList(HttpServletRequest request) throws Exception {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
Constants.DEFAULT_NAMESPACE_ID); Constants.DEFAULT_NAMESPACE_ID);
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
String clusterName = WebUtils.required(request, CommonParams.CLUSTER_NAME); String clusterName = WebUtils.required(request, CommonParams.CLUSTER_NAME);
int page = Integer.parseInt(WebUtils.required(request, "startPg")); int page = Integer.parseInt(WebUtils.required(request, "pageNo"));
int pageSize = Integer.parseInt(WebUtils.required(request, "pgSize")); int pageSize = Integer.parseInt(WebUtils.required(request, "pageSize"));
Service service = serviceManager.getService(namespaceId, serviceName); Service service = serviceManager.getService(namespaceId, serviceName);
if (service == null) { if (service == null) {
@ -174,37 +128,43 @@ public class CatalogController {
} }
@RequestMapping(value = "/services", method = RequestMethod.GET) @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, boolean withInstances = Boolean.parseBoolean(WebUtils.optional(request, "withInstances", "true"));
Constants.DEFAULT_NAMESPACE_ID);
List<ServiceDetailInfo> serviceDetailInfoList = new ArrayList<>();
serviceManager if (withInstances) {
.getServiceMap(namespaceId) String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
.forEach( Constants.DEFAULT_NAMESPACE_ID);
(serviceName, service) -> { List<ServiceDetailInfo> serviceDetailInfoList = new ArrayList<>();
ServiceDetailInfo serviceDetailInfo = new ServiceDetailInfo(); serviceManager
serviceDetailInfo.setServiceName(UtilsAndCommons.getServiceName(serviceName)); .getServiceMap(namespaceId)
serviceDetailInfo.setGroupName(UtilsAndCommons.getGroupName(serviceName)); .forEach(
serviceDetailInfo.setMetadata(service.getMetadata()); (serviceName, service) -> {
Map<String, ClusterInfo> clusterInfoMap = getStringClusterInfoMap(service); ServiceDetailInfo serviceDetailInfo = new ServiceDetailInfo();
serviceDetailInfo.setClusterMap(clusterInfoMap); 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) { public JSONObject rt4Service(HttpServletRequest request) {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
Constants.DEFAULT_NAMESPACE_ID); Constants.DEFAULT_NAMESPACE_ID);
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
Service service = serviceManager.getService(namespaceId, serviceName); Service service = serviceManager.getService(namespaceId, serviceName);
@ -231,38 +191,6 @@ public class CatalogController {
return result; 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 * getStringClusterInfoMap
* *
@ -307,4 +235,52 @@ public class CatalogController {
return ipAddressInfos; 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.cluster.transport.Serializer;
import com.alibaba.nacos.naming.consistency.Datum; import com.alibaba.nacos.naming.consistency.Datum;
import com.alibaba.nacos.naming.consistency.KeyBuilder; 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.Instances;
import com.alibaba.nacos.naming.core.ServiceManager; import com.alibaba.nacos.naming.core.ServiceManager;
import com.alibaba.nacos.naming.exception.NacosException; import com.alibaba.nacos.naming.exception.NacosException;
@ -47,14 +48,17 @@ import java.util.Map;
* @since 1.0.0 * @since 1.0.0
*/ */
@RestController @RestController
@RequestMapping(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/partition") @RequestMapping(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/distro")
public class PartitionController { public class DistroController {
@Autowired @Autowired
private Serializer serializer; private Serializer serializer;
@Autowired @Autowired
private PartitionConsistencyServiceImpl consistencyService; private DistroConsistencyServiceImpl consistencyService;
@Autowired
private DataStore dataStore;
@Autowired @Autowired
private ServiceManager serviceManager; private ServiceManager serviceManager;
@ -89,24 +93,14 @@ public class PartitionController {
return "ok"; return "ok";
} }
@RequestMapping(value = "/timestamps", method = RequestMethod.PUT) @RequestMapping(value = "/checksum", method = RequestMethod.PUT)
public String syncTimestamps(HttpServletRequest request, HttpServletResponse response) throws Exception { public String syncChecksum(HttpServletRequest request, HttpServletResponse response) throws Exception {
String source = WebUtils.required(request, "source"); String source = WebUtils.required(request, "source");
String entity = IOUtils.toString(request.getInputStream(), "UTF-8"); String entity = IOUtils.toString(request.getInputStream(), "UTF-8");
Map<String, String> dataMap = Map<String, String> dataMap =
serializer.deserialize(entity.getBytes(), new TypeReference<Map<String, String>>() { serializer.deserialize(entity.getBytes(), new TypeReference<Map<String, String>>() {
}); });
consistencyService.onReceiveChecksums(dataMap, source);
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);
return "ok"; return "ok";
} }
@ -120,4 +114,9 @@ public class PartitionController {
} }
response.getWriter().write(new String(serializer.serialize(datumMap), "UTF-8")); 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; return result;
} }
@RequestMapping(value = "", method = RequestMethod.PUT) @RequestMapping(value = {"", "/instance"}, method = RequestMethod.PUT)
public String update(HttpServletRequest request) throws Exception { public String update(HttpServletRequest request) throws Exception {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,

View File

@ -52,7 +52,7 @@ import java.util.*;
* @author nkorange * @author nkorange
*/ */
@RestController @RestController
@RequestMapping(UtilsAndCommons.NACOS_NAMING_CONTEXT) @RequestMapping(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/instance")
public class InstanceController { public class InstanceController {
@Autowired @Autowired
@ -89,7 +89,7 @@ public class InstanceController {
}; };
@CanDistro @CanDistro
@RequestMapping(value = "/instance", method = RequestMethod.POST) @RequestMapping(value = "", method = RequestMethod.POST)
public String register(HttpServletRequest request) throws Exception { public String register(HttpServletRequest request) throws Exception {
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
@ -100,7 +100,7 @@ public class InstanceController {
} }
@CanDistro @CanDistro
@RequestMapping(value = "/instance", method = RequestMethod.DELETE) @RequestMapping(value = "", method = RequestMethod.DELETE)
public String deregister(HttpServletRequest request) throws Exception { public String deregister(HttpServletRequest request) throws Exception {
Instance instance = getIPAddress(request); Instance instance = getIPAddress(request);
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
@ -117,7 +117,7 @@ public class InstanceController {
return "ok"; return "ok";
} }
@RequestMapping(value = "/instance", method = RequestMethod.PUT) @RequestMapping(value = "", method = RequestMethod.PUT)
public String update(HttpServletRequest request) throws Exception { public String update(HttpServletRequest request) throws Exception {
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID); String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
@ -126,7 +126,7 @@ public class InstanceController {
return "ok"; return "ok";
} }
@RequestMapping(value = {"/instances", "/instance/list"}, method = RequestMethod.GET) @RequestMapping(value = "/list", method = RequestMethod.GET)
public JSONObject list(HttpServletRequest request) throws Exception { public JSONObject list(HttpServletRequest request) throws Exception {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, 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); 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 { public JSONObject detail(HttpServletRequest request) throws Exception {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
@ -194,7 +194,7 @@ public class InstanceController {
} }
@CanDistro @CanDistro
@RequestMapping(value = "/instance/beat", method = RequestMethod.PUT) @RequestMapping(value = "/beat", method = RequestMethod.PUT)
public JSONObject beat(HttpServletRequest request) throws Exception { public JSONObject beat(HttpServletRequest request) throws Exception {
JSONObject result = new JSONObject(); JSONObject result = new JSONObject();
@ -254,7 +254,7 @@ public class InstanceController {
} }
@RequestMapping("/instance/listWithHealthStatus") @RequestMapping("/statuses")
public JSONObject listWithHealthStatus(HttpServletRequest request) throws NacosException { public JSONObject listWithHealthStatus(HttpServletRequest request) throws NacosException {
String key = WebUtils.required(request, "key"); String key = WebUtils.required(request, "key");
@ -402,7 +402,7 @@ public class InstanceController {
result.put("name", serviceName); result.put("name", serviceName);
result.put("cacheMillis", cacheMillis); result.put("cacheMillis", cacheMillis);
result.put("lastRefTime", System.currentTimeMillis()); result.put("lastRefTime", System.currentTimeMillis());
result.put("checksum", service.getChecksum() + System.currentTimeMillis()); result.put("checksum", service.getChecksum());
result.put("useSpecifiedURL", false); result.put("useSpecifiedURL", false);
result.put("clusters", clusters); result.put("clusters", clusters);
result.put("env", env); 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 com.alibaba.nacos.naming.web.NeedAuth;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; 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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -72,7 +73,7 @@ public class OperatorController {
@Autowired @Autowired
private DistroMapper distroMapper; private DistroMapper distroMapper;
@RequestMapping("/pushState") @RequestMapping("/push/state")
public JSONObject pushState(HttpServletRequest request) { public JSONObject pushState(HttpServletRequest request) {
JSONObject result = new JSONObject(); JSONObject result = new JSONObject();
@ -112,7 +113,7 @@ public class OperatorController {
return result; return result;
} }
@RequestMapping("/switches") @RequestMapping(value = "/switches", method = RequestMethod.GET)
public SwitchDomain switches(HttpServletRequest request) { public SwitchDomain switches(HttpServletRequest request) {
return switchDomain; return switchDomain;
} }
@ -152,12 +153,12 @@ public class OperatorController {
return result; return result;
} }
@RequestMapping("/getResponsibleServer4Dom") @RequestMapping(value = "/distro/server", method = RequestMethod.GET)
public JSONObject getResponsibleServer4Dom(HttpServletRequest request) { public JSONObject getResponsibleServer4Service(HttpServletRequest request) {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
Constants.DEFAULT_NAMESPACE_ID); Constants.DEFAULT_NAMESPACE_ID);
String dom = WebUtils.required(request, "dom"); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
Service service = serviceManager.getService(namespaceId, dom); Service service = serviceManager.getService(namespaceId, serviceName);
if (service == null) { if (service == null) {
throw new IllegalArgumentException("service not found"); throw new IllegalArgumentException("service not found");
@ -165,39 +166,12 @@ public class OperatorController {
JSONObject result = new JSONObject(); JSONObject result = new JSONObject();
result.put("responsibleServer", distroMapper.mapSrv(dom)); result.put("responsibleServer", distroMapper.mapSrv(serviceName));
return result; return result;
} }
@RequestMapping("/getHealthyServerList") @RequestMapping(value = "/distro/status", method = RequestMethod.GET)
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")
public JSONObject distroStatus(HttpServletRequest request) { public JSONObject distroStatus(HttpServletRequest request) {
JSONObject result = new JSONObject(); JSONObject result = new JSONObject();
@ -216,7 +190,21 @@ public class OperatorController {
return result; 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) { public String serverStatus(HttpServletRequest request) {
String serverStatus = WebUtils.required(request, "serverStatus"); String serverStatus = WebUtils.required(request, "serverStatus");
serverListManager.onReceiveServerStatus(serverStatus); serverListManager.onReceiveServerStatus(serverStatus);

View File

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

View File

@ -561,7 +561,7 @@ public class ServiceManager implements RecordListener<Service> {
return serviceMap.get(namespaceId); 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; List<Service> matchList;
@ -572,7 +572,34 @@ public class ServiceManager implements RecordListener<Service> {
if (StringUtils.isNotBlank(keyword)) { if (StringUtils.isNotBlank(keyword)) {
matchList = searchServices(namespaceId, ".*" + keyword + ".*"); matchList = searchServices(namespaceId, ".*" + keyword + ".*");
} else { } 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()) { if (pageSize >= matchList.size()) {

View File

@ -88,14 +88,6 @@ public class ClientBeatCheckTask implements Runnable {
// then remove obsolete instances: // then remove obsolete instances:
for (Instance instance : instances) { for (Instance instance : instances) {
if (System.currentTimeMillis() - instance.getLastBeat() > service.getIpDeleteTimeout()) { 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 // delete instance
Loggers.SRV_LOG.info("[AUTO-DELETE-IP] service: {}, ip: {}", service.getName(), JSON.toJSONString(instance)); Loggers.SRV_LOG.info("[AUTO-DELETE-IP] service: {}, ip: {}", service.getName(), JSON.toJSONString(instance));
deleteIP(instance); deleteIP(instance);

View File

@ -56,7 +56,7 @@ public class GlobalExecutor {
Thread t = new Thread(r); Thread t = new Thread(r);
t.setDaemon(true); t.setDaemon(true);
t.setName("com.alibaba.nacos.naming.partition.task.dispatcher"); t.setName("com.alibaba.nacos.naming.distro.task.dispatcher");
return t; return t;
} }
@ -70,7 +70,7 @@ public class GlobalExecutor {
Thread t = new Thread(r); Thread t = new Thread(r);
t.setDaemon(true); t.setDaemon(true);
t.setName("com.alibaba.nacos.naming.partition.data.syncer"); t.setName("com.alibaba.nacos.naming.distro.data.syncer");
return t; 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) { public static HttpResult httpPost(String url, List<String> headers, Map<String, String> paramValues) {
return httpPost(url, headers, paramValues, "UTF-8"); return httpPost(url, headers, paramValues, "UTF-8");
} }

View File

@ -31,13 +31,15 @@ import java.util.*;
*/ */
public class NamingProxy { 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 { try {
Map<String, String> headers = new HashMap<>(128); Map<String, String> headers = new HashMap<>(128);
@ -48,7 +50,7 @@ public class NamingProxy {
HttpClient.asyncHttpPutLarge("http://" + server + RunningConfig.getContextPath() HttpClient.asyncHttpPutLarge("http://" + server + RunningConfig.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + TIMESTAMP_SYNC_URL + "?source=" + NetUtils.localServer(), + UtilsAndCommons.NACOS_NAMING_CONTEXT + TIMESTAMP_SYNC_URL + "?source=" + NetUtils.localServer(),
headers, JSON.toJSONBytes(timestamps), headers, JSON.toJSONBytes(checksumMap),
new AsyncCompletionHandler() { new AsyncCompletionHandler() {
@Override @Override
public Object onCompleted(Response response) throws Exception { public Object onCompleted(Response response) throws Exception {
@ -90,6 +92,23 @@ public class NamingProxy {
+ result.code + " msg: " + result.content); + 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 { public static boolean syncData(byte[] data, String curServer) throws Exception {
try { try {
Map<String, String> headers = new HashMap<>(128); Map<String, String> headers = new HashMap<>(128);

View File

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

View File

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