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

This commit is contained in:
chuntaojun 2020-10-13 20:54:38 +08:00
commit ff9f31811e
31 changed files with 493 additions and 139 deletions

View File

@ -19,6 +19,8 @@ package com.alibaba.nacos.client.logging;
import com.alibaba.nacos.common.utils.ConvertUtils; import com.alibaba.nacos.common.utils.ConvertUtils;
import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.StringUtils;
import java.io.File;
/** /**
* Abstract nacos logging. * Abstract nacos logging.
* *
@ -31,13 +33,13 @@ public abstract class AbstractNacosLogging {
private static final String NACOS_LOGGING_DEFAULT_CONFIG_ENABLED_PROPERTY = "nacos.logging.default.config.enabled"; private static final String NACOS_LOGGING_DEFAULT_CONFIG_ENABLED_PROPERTY = "nacos.logging.default.config.enabled";
private static final String NACOS_LOGGING_PATH_PROPERTY = "nacos.logging.path"; private static final String NACOS_LOGGING_PATH_PROPERTY = "JM.LOG.PATH";
static { static {
String loggingPath = System.getProperty(NACOS_LOGGING_PATH_PROPERTY); String loggingPath = System.getProperty(NACOS_LOGGING_PATH_PROPERTY);
if (StringUtils.isBlank(loggingPath)) { if (StringUtils.isBlank(loggingPath)) {
String userHome = System.getProperty("user.home"); String userHome = System.getProperty("user.home");
System.setProperty(NACOS_LOGGING_PATH_PROPERTY, userHome + "/logs/nacos"); System.setProperty(NACOS_LOGGING_PATH_PROPERTY, userHome + File.separator + "logs");
} }
} }

View File

@ -39,6 +39,7 @@ import com.alibaba.nacos.client.utils.ValidatorUtils;
import com.alibaba.nacos.common.utils.ConvertUtils; import com.alibaba.nacos.common.utils.ConvertUtils;
import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.StringUtils;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -151,9 +152,13 @@ public class NacosNamingService implements NamingService {
} }
private void initCacheDir() { private void initCacheDir() {
cacheDir = System.getProperty("com.alibaba.nacos.naming.cache.dir"); String jmSnapshotPath = System.getProperty("JM.SNAPSHOT.PATH");
if (StringUtils.isEmpty(cacheDir)) { if (!StringUtils.isBlank(jmSnapshotPath)) {
cacheDir = System.getProperty("user.home") + "/nacos/naming/" + namespace; cacheDir = jmSnapshotPath + File.separator + "nacos" + File.separator + "naming"
+ File.separator + namespace;
} else {
cacheDir = System.getProperty("user.home") + File.separator + "nacos" + File.separator + "naming"
+ File.separator + namespace;
} }
} }

View File

@ -17,8 +17,8 @@
<Configuration status="WARN"> <Configuration status="WARN">
<Appenders> <Appenders>
<RollingFile name="CONFIG_LOG_FILE" fileName="${sys:nacos.logging.path}/config.log" <RollingFile name="CONFIG_LOG_FILE" fileName="${sys:JM.LOG.PATH}/nacos/config.log"
filePattern="${sys:nacos.logging.path}/config.log.%d{yyyy-MM-dd}.%i"> filePattern="${sys:JM.LOG.PATH}/nacos/config.log.%d{yyyy-MM-dd}.%i">
<PatternLayout> <PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n</Pattern> <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n</Pattern>
</PatternLayout> </PatternLayout>
@ -31,8 +31,8 @@
<DefaultRolloverStrategy max="${sys:JM.LOG.RETAIN.COUNT:-7}"/> <DefaultRolloverStrategy max="${sys:JM.LOG.RETAIN.COUNT:-7}"/>
</RollingFile> </RollingFile>
<RollingFile name="NAMING_LOG_FILE" fileName="${sys:nacos.logging.path}/naming.log" <RollingFile name="NAMING_LOG_FILE" fileName="${sys:JM.LOG.PATH}/nacos/naming.log"
filePattern="${sys:nacos.logging.path}/naming.log.%d{yyyy-MM-dd}.%i"> filePattern="${sys:JM.LOG.PATH}/nacos/naming.log.%d{yyyy-MM-dd}.%i">
<PatternLayout> <PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n</Pattern> <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n</Pattern>
</PatternLayout> </PatternLayout>

View File

@ -19,10 +19,10 @@
<contextName>nacos</contextName> <contextName>nacos</contextName>
<appender name="CONFIG_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <appender name="CONFIG_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${nacos.logging.path}/config.log</file> <file>${JM.LOG.PATH}/nacos/config.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${nacos.logging.path}/config.log.%i</fileNamePattern> <fileNamePattern>${JM.LOG.PATH}/nacos/config.log.%i</fileNamePattern>
<maxIndex>${JM.LOG.RETAIN.COUNT:-7}</maxIndex> <maxIndex>${JM.LOG.RETAIN.COUNT:-7}</maxIndex>
</rollingPolicy> </rollingPolicy>
@ -36,10 +36,10 @@
</appender> </appender>
<appender name="NAMING_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <appender name="NAMING_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${nacos.logging.path}/naming.log</file> <file>${JM.LOG.PATH}/nacos/naming.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${nacos.logging.path}/naming.log.%i</fileNamePattern> <fileNamePattern>${JM.LOG.PATH}/nacos/naming.log.%i</fileNamePattern>
<maxIndex>${JM.LOG.RETAIN.COUNT:-7}</maxIndex> <maxIndex>${JM.LOG.RETAIN.COUNT:-7}</maxIndex>
</rollingPolicy> </rollingPolicy>

View File

@ -43,6 +43,7 @@ import com.alibaba.nacos.config.server.service.trace.ConfigTraceService;
import com.alibaba.nacos.config.server.utils.MD5Util; import com.alibaba.nacos.config.server.utils.MD5Util;
import com.alibaba.nacos.config.server.utils.ParamUtils; import com.alibaba.nacos.config.server.utils.ParamUtils;
import com.alibaba.nacos.config.server.utils.RequestUtil; import com.alibaba.nacos.config.server.utils.RequestUtil;
import com.alibaba.nacos.config.server.utils.NamespaceUtil;
import com.alibaba.nacos.config.server.utils.TimeUtils; import com.alibaba.nacos.config.server.utils.TimeUtils;
import com.alibaba.nacos.config.server.utils.ZipUtils; import com.alibaba.nacos.config.server.utils.ZipUtils;
import com.alibaba.nacos.sys.utils.InetUtils; import com.alibaba.nacos.sys.utils.InetUtils;
@ -90,8 +91,6 @@ public class ConfigController {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigController.class); private static final Logger LOGGER = LoggerFactory.getLogger(ConfigController.class);
private static final String NAMESPACE_PUBLIC_KEY = "public";
private static final String EXPORT_CONFIG_FILE_NAME = "nacos_config_export_"; private static final String EXPORT_CONFIG_FILE_NAME = "nacos_config_export_";
private static final String EXPORT_CONFIG_FILE_NAME_EXT = ".zip"; private static final String EXPORT_CONFIG_FILE_NAME_EXT = ".zip";
@ -175,7 +174,7 @@ public class ConfigController {
.notifyConfigChange(new ConfigDataChangeEvent(true, dataId, group, tenant, time.getTime())); .notifyConfigChange(new ConfigDataChangeEvent(true, dataId, group, tenant, time.getTime()));
} }
ConfigTraceService ConfigTraceService
.logPersistenceEvent(dataId, group, tenant, requestIpApp, time.getTime(), InetUtils.getSelfIp(), .logPersistenceEvent(dataId, group, tenant, requestIpApp, time.getTime(), InetUtils.getSelfIP(),
ConfigTraceService.PERSISTENCE_EVENT_PUB, content); ConfigTraceService.PERSISTENCE_EVENT_PUB, content);
return true; return true;
} }
@ -196,7 +195,7 @@ public class ConfigController {
throws IOException, ServletException, NacosException { throws IOException, ServletException, NacosException {
// check tenant // check tenant
ParamUtils.checkTenant(tenant); ParamUtils.checkTenant(tenant);
tenant = processTenant(tenant); tenant = NamespaceUtil.processNamespaceParameter(tenant);
// check params // check params
ParamUtils.checkParam(dataId, group, "datumId", "content"); ParamUtils.checkParam(dataId, group, "datumId", "content");
ParamUtils.checkParam(tag); ParamUtils.checkParam(tag);
@ -469,7 +468,7 @@ public class ConfigController {
@RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant, @RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant,
@RequestParam(value = "ids", required = false) List<Long> ids) { @RequestParam(value = "ids", required = false) List<Long> ids) {
ids.removeAll(Collections.singleton(null)); ids.removeAll(Collections.singleton(null));
tenant = processTenant(tenant); tenant = NamespaceUtil.processNamespaceParameter(tenant);
List<ConfigAllInfo> dataList = persistService.findAllConfigInfo4Export(dataId, group, tenant, appName, ids); List<ConfigAllInfo> dataList = persistService.findAllConfigInfo4Export(dataId, group, tenant, appName, ids);
List<ZipUtils.ZipItem> zipItemList = new ArrayList<>(); List<ZipUtils.ZipItem> zipItemList = new ArrayList<>();
StringBuilder metaData = null; StringBuilder metaData = null;
@ -527,12 +526,12 @@ public class ConfigController {
return ResultBuilder.buildResult(ResultCodeEnum.DATA_EMPTY, failedData); return ResultBuilder.buildResult(ResultCodeEnum.DATA_EMPTY, failedData);
} }
if (StringUtils.isNotBlank(namespace)) { namespace = NamespaceUtil.processNamespaceParameter(namespace);
if (persistService.tenantInfoCountByTenantId(namespace) <= 0) { if (StringUtils.isNotBlank(namespace) && persistService.tenantInfoCountByTenantId(namespace) <= 0) {
failedData.put("succCount", 0); failedData.put("succCount", 0);
return ResultBuilder.buildResult(ResultCodeEnum.NAMESPACE_NOT_EXIST, failedData); return ResultBuilder.buildResult(ResultCodeEnum.NAMESPACE_NOT_EXIST, failedData);
}
} }
List<ConfigAllInfo> configInfoList = null; List<ConfigAllInfo> configInfoList = null;
try { try {
ZipUtils.UnZipResult unziped = ZipUtils.unzip(file.getBytes()); ZipUtils.UnZipResult unziped = ZipUtils.unzip(file.getBytes());
@ -598,7 +597,7 @@ public class ConfigController {
configInfo.getTenant(), time.getTime())); configInfo.getTenant(), time.getTime()));
ConfigTraceService ConfigTraceService
.logPersistenceEvent(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(), .logPersistenceEvent(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(),
requestIpApp, time.getTime(), InetUtils.getSelfIp(), requestIpApp, time.getTime(), InetUtils.getSelfIP(),
ConfigTraceService.PERSISTENCE_EVENT_PUB, configInfo.getContent()); ConfigTraceService.PERSISTENCE_EVENT_PUB, configInfo.getContent());
} }
return ResultBuilder.buildSuccessResult("导入成功", saveResult); return ResultBuilder.buildSuccessResult("导入成功", saveResult);
@ -628,10 +627,9 @@ public class ConfigController {
return ResultBuilder.buildResult(ResultCodeEnum.NO_SELECTED_CONFIG, failedData); return ResultBuilder.buildResult(ResultCodeEnum.NO_SELECTED_CONFIG, failedData);
} }
configBeansList.removeAll(Collections.singleton(null)); configBeansList.removeAll(Collections.singleton(null));
if (NAMESPACE_PUBLIC_KEY.equalsIgnoreCase(namespace)) { namespace = NamespaceUtil.processNamespaceParameter(namespace);
namespace = ""; if (StringUtils.isNotBlank(namespace) && persistService.tenantInfoCountByTenantId(namespace) <= 0) {
} else if (persistService.tenantInfoCountByTenantId(namespace) <= 0) {
failedData.put("succCount", 0); failedData.put("succCount", 0);
return ResultBuilder.buildResult(ResultCodeEnum.NAMESPACE_NOT_EXIST, failedData); return ResultBuilder.buildResult(ResultCodeEnum.NAMESPACE_NOT_EXIST, failedData);
} }
@ -684,17 +682,10 @@ public class ConfigController {
configInfo.getTenant(), time.getTime())); configInfo.getTenant(), time.getTime()));
ConfigTraceService ConfigTraceService
.logPersistenceEvent(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(), .logPersistenceEvent(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(),
requestIpApp, time.getTime(), InetUtils.getSelfIp(), requestIpApp, time.getTime(), InetUtils.getSelfIP(),
ConfigTraceService.PERSISTENCE_EVENT_PUB, configInfo.getContent()); ConfigTraceService.PERSISTENCE_EVENT_PUB, configInfo.getContent());
} }
return ResultBuilder.buildSuccessResult("Clone Completed Successfully", saveResult); return ResultBuilder.buildSuccessResult("Clone Completed Successfully", saveResult);
} }
private String processTenant(String tenant) {
if (StringUtils.isEmpty(tenant) || NAMESPACE_PUBLIC_KEY.equalsIgnoreCase(tenant)) {
return "";
}
return tenant;
}
} }

View File

@ -69,7 +69,7 @@ public class HealthController {
sb.append("master db (").append(dbStatus.split(":")[1]).append(") down. "); sb.append("master db (").append(dbStatus.split(":")[1]).append(") down. ");
} }
if (!memberManager.isInIpList()) { if (!memberManager.isInIpList()) {
sb.append("server ip ").append(InetUtils.getSelfIp()) sb.append("server ip ").append(InetUtils.getSelfIP())
.append(" is not in the serverList of address server. "); .append(" is not in the serverList of address server. ");
} }
} }

View File

@ -102,7 +102,7 @@ public class ApplicationInfo {
*/ */
public boolean canCurrentServerOwnTheLock() { public boolean canCurrentServerOwnTheLock() {
boolean currentOwnerIsMe = boolean currentOwnerIsMe =
subInfoCollectLockOwner == null || InetUtils.getSelfIp().equals(subInfoCollectLockOwner); subInfoCollectLockOwner == null || InetUtils.getSelfIP().equals(subInfoCollectLockOwner);
if (currentOwnerIsMe) { if (currentOwnerIsMe) {
return true; return true;
@ -115,7 +115,7 @@ public class ApplicationInfo {
} }
public String currentServer() { public String currentServer() {
return InetUtils.getSelfIp(); return InetUtils.getSelfIP();
} }
} }

View File

@ -419,7 +419,7 @@ public abstract class DumpService {
} }
} else { } else {
// remove config info // remove config info
persistService.removeConfigInfo(dataId, group, tenant, InetUtils.getSelfIp(), null); persistService.removeConfigInfo(dataId, group, tenant, InetUtils.getSelfIP(), null);
LOGGER.warn( LOGGER.warn(
"[merge-delete] delete config info because no datum. dataId=" + dataId + ", groupId=" "[merge-delete] delete config info because no datum. dataId=" + dataId + ", groupId="
+ group); + group);

View File

@ -106,7 +106,7 @@ public class MergeDatumService {
return; return;
} }
for (ConfigInfoChanged item : persistService.findAllAggrGroup()) { for (ConfigInfoChanged item : persistService.findAllAggrGroup()) {
addMergeTask(item.getDataId(), item.getGroup(), item.getTenant(), InetUtils.getSelfIp()); addMergeTask(item.getDataId(), item.getGroup(), item.getTenant(), InetUtils.getSelfIP());
} }
} }
@ -163,7 +163,7 @@ public class MergeDatumService {
ContentUtils.truncateContent(cf.getContent())); ContentUtils.truncateContent(cf.getContent()));
} else { } else {
// remove // remove
persistService.removeConfigInfo(dataId, group, tenant, InetUtils.getSelfIp(), null); persistService.removeConfigInfo(dataId, group, tenant, InetUtils.getSelfIP(), null);
LOGGER.warn("[merge-delete] delete config info because no datum. dataId=" + dataId + ", groupId=" LOGGER.warn("[merge-delete] delete config info because no datum. dataId=" + dataId + ", groupId="
+ group); + group);
} }

View File

@ -84,7 +84,7 @@ public class MergeTaskProcessor implements NacosTaskProcessor {
ContentUtils.truncateContent(cf.getContent())); ContentUtils.truncateContent(cf.getContent()));
ConfigTraceService ConfigTraceService
.logPersistenceEvent(dataId, group, tenant, null, time.getTime(), InetUtils.getSelfIp(), .logPersistenceEvent(dataId, group, tenant, null, time.getTime(), InetUtils.getSelfIP(),
ConfigTraceService.PERSISTENCE_EVENT_MERGE, cf.getContent()); ConfigTraceService.PERSISTENCE_EVENT_MERGE, cf.getContent());
} else { } else {
// remove // remove
@ -98,7 +98,7 @@ public class MergeTaskProcessor implements NacosTaskProcessor {
"[merge-delete] delete config info because no datum. dataId=" + dataId + ", groupId=" + group); "[merge-delete] delete config info because no datum. dataId=" + dataId + ", groupId=" + group);
ConfigTraceService ConfigTraceService
.logPersistenceEvent(dataId, group, tenant, null, time.getTime(), InetUtils.getSelfIp(), .logPersistenceEvent(dataId, group, tenant, null, time.getTime(), InetUtils.getSelfIP(),
ConfigTraceService.PERSISTENCE_EVENT_REMOVE, null); ConfigTraceService.PERSISTENCE_EVENT_REMOVE, null);
} }
NotifyCenter.publishEvent(new ConfigDataChangeEvent(false, dataId, group, tenant, tag, time.getTime())); NotifyCenter.publishEvent(new ConfigDataChangeEvent(false, dataId, group, tenant, tag, time.getTime()));

View File

@ -127,14 +127,14 @@ public class AsyncNotifyService {
if (unHealthNeedDelay) { if (unHealthNeedDelay) {
// target ip is unhealthy, then put it in the notification list // target ip is unhealthy, then put it in the notification list
ConfigTraceService.logNotifyEvent(task.getDataId(), task.getGroup(), task.getTenant(), null, ConfigTraceService.logNotifyEvent(task.getDataId(), task.getGroup(), task.getTenant(), null,
task.getLastModified(), InetUtils.getSelfIp(), ConfigTraceService.NOTIFY_EVENT_UNHEALTH, task.getLastModified(), InetUtils.getSelfIP(), ConfigTraceService.NOTIFY_EVENT_UNHEALTH,
0, task.target); 0, task.target);
// get delay time and set fail count to the task // get delay time and set fail count to the task
asyncTaskExecute(task); asyncTaskExecute(task);
} else { } else {
Header header = Header.newInstance(); Header header = Header.newInstance();
header.addParam(NotifyService.NOTIFY_HEADER_LAST_MODIFIED, String.valueOf(task.getLastModified())); header.addParam(NotifyService.NOTIFY_HEADER_LAST_MODIFIED, String.valueOf(task.getLastModified()));
header.addParam(NotifyService.NOTIFY_HEADER_OP_HANDLE_IP, InetUtils.getSelfIp()); header.addParam(NotifyService.NOTIFY_HEADER_OP_HANDLE_IP, InetUtils.getSelfIP());
if (task.isBeta) { if (task.isBeta) {
header.addParam("isBeta", "true"); header.addParam("isBeta", "true");
} }
@ -168,13 +168,13 @@ public class AsyncNotifyService {
if (result.ok()) { if (result.ok()) {
ConfigTraceService.logNotifyEvent(task.getDataId(), task.getGroup(), task.getTenant(), null, ConfigTraceService.logNotifyEvent(task.getDataId(), task.getGroup(), task.getTenant(), null,
task.getLastModified(), InetUtils.getSelfIp(), ConfigTraceService.NOTIFY_EVENT_OK, delayed, task.getLastModified(), InetUtils.getSelfIP(), ConfigTraceService.NOTIFY_EVENT_OK, delayed,
task.target); task.target);
} else { } else {
LOGGER.error("[notify-error] target:{} dataId:{} group:{} ts:{} code:{}", task.target, task.getDataId(), LOGGER.error("[notify-error] target:{} dataId:{} group:{} ts:{} code:{}", task.target, task.getDataId(),
task.getGroup(), task.getLastModified(), result.getCode()); task.getGroup(), task.getLastModified(), result.getCode());
ConfigTraceService.logNotifyEvent(task.getDataId(), task.getGroup(), task.getTenant(), null, ConfigTraceService.logNotifyEvent(task.getDataId(), task.getGroup(), task.getTenant(), null,
task.getLastModified(), InetUtils.getSelfIp(), ConfigTraceService.NOTIFY_EVENT_ERROR, delayed, task.getLastModified(), InetUtils.getSelfIP(), ConfigTraceService.NOTIFY_EVENT_ERROR, delayed,
task.target); task.target);
//get delay time and set fail count to the task //get delay time and set fail count to the task
@ -196,7 +196,7 @@ public class AsyncNotifyService {
task.getGroup(), task.getLastModified(), ex.toString()); task.getGroup(), task.getLastModified(), ex.toString());
ConfigTraceService ConfigTraceService
.logNotifyEvent(task.getDataId(), task.getGroup(), task.getTenant(), null, task.getLastModified(), .logNotifyEvent(task.getDataId(), task.getGroup(), task.getTenant(), null, task.getLastModified(),
InetUtils.getSelfIp(), ConfigTraceService.NOTIFY_EVENT_EXCEPTION, delayed, task.target); InetUtils.getSelfIP(), ConfigTraceService.NOTIFY_EVENT_EXCEPTION, delayed, task.target);
//get delay time and set fail count to the task //get delay time and set fail count to the task
asyncTaskExecute(task); asyncTaskExecute(task);

View File

@ -73,13 +73,13 @@ public class NotifyTaskProcessor implements NacosTaskProcessor {
*/ */
List<String> headers = Arrays List<String> headers = Arrays
.asList(NotifyService.NOTIFY_HEADER_LAST_MODIFIED, String.valueOf(lastModified), .asList(NotifyService.NOTIFY_HEADER_LAST_MODIFIED, String.valueOf(lastModified),
NotifyService.NOTIFY_HEADER_OP_HANDLE_IP, InetUtils.getSelfIp()); NotifyService.NOTIFY_HEADER_OP_HANDLE_IP, InetUtils.getSelfIP());
String urlString = MessageFormat String urlString = MessageFormat
.format(URL_PATTERN, serverIp, ApplicationUtils.getContextPath(), dataId, group); .format(URL_PATTERN, serverIp, ApplicationUtils.getContextPath(), dataId, group);
RestResult<String> result = NotifyService.invokeURL(urlString, headers, Constants.ENCODE); RestResult<String> result = NotifyService.invokeURL(urlString, headers, Constants.ENCODE);
if (result.ok()) { if (result.ok()) {
ConfigTraceService.logNotifyEvent(dataId, group, tenant, null, lastModified, InetUtils.getSelfIp(), ConfigTraceService.logNotifyEvent(dataId, group, tenant, null, lastModified, InetUtils.getSelfIP(),
ConfigTraceService.NOTIFY_EVENT_OK, delayed, serverIp); ConfigTraceService.NOTIFY_EVENT_OK, delayed, serverIp);
MetricsMonitor.getNotifyRtTimer().record(delayed, TimeUnit.MILLISECONDS); MetricsMonitor.getNotifyRtTimer().record(delayed, TimeUnit.MILLISECONDS);
@ -89,7 +89,7 @@ public class NotifyTaskProcessor implements NacosTaskProcessor {
MetricsMonitor.getConfigNotifyException().increment(); MetricsMonitor.getConfigNotifyException().increment();
LOGGER.error("[notify-error] {}, {}, to {}, result {}", LOGGER.error("[notify-error] {}, {}, to {}, result {}",
new Object[] {dataId, group, serverIp, result.getCode()}); new Object[] {dataId, group, serverIp, result.getCode()});
ConfigTraceService.logNotifyEvent(dataId, group, tenant, null, lastModified, InetUtils.getSelfIp(), ConfigTraceService.logNotifyEvent(dataId, group, tenant, null, lastModified, InetUtils.getSelfIP(),
ConfigTraceService.NOTIFY_EVENT_ERROR, delayed, serverIp); ConfigTraceService.NOTIFY_EVENT_ERROR, delayed, serverIp);
return false; return false;
} }
@ -97,7 +97,7 @@ public class NotifyTaskProcessor implements NacosTaskProcessor {
MetricsMonitor.getConfigNotifyException().increment(); MetricsMonitor.getConfigNotifyException().increment();
LOGGER.error("[notify-exception] " + dataId + ", " + group + ", to " + serverIp + ", " + e.toString()); LOGGER.error("[notify-exception] " + dataId + ", " + group + ", to " + serverIp + ", " + e.toString());
LOGGER.debug("[notify-exception] " + dataId + ", " + group + ", to " + serverIp + ", " + e.toString(), e); LOGGER.debug("[notify-exception] " + dataId + ", " + group + ", to " + serverIp + ", " + e.toString(), e);
ConfigTraceService.logNotifyEvent(dataId, group, tenant, null, lastModified, InetUtils.getSelfIp(), ConfigTraceService.logNotifyEvent(dataId, group, tenant, null, lastModified, InetUtils.getSelfIP(),
ConfigTraceService.NOTIFY_EVENT_EXCEPTION, delayed, serverIp); ConfigTraceService.NOTIFY_EVENT_EXCEPTION, delayed, serverIp);
return false; return false;
} }

View File

@ -2292,7 +2292,7 @@ public class ExternalStoragePersistServiceImpl implements PersistService {
.queryForObject(sqlFetchRows, new Object[] {nid}, HISTORY_DETAIL_ROW_MAPPER); .queryForObject(sqlFetchRows, new Object[] {nid}, HISTORY_DETAIL_ROW_MAPPER);
return historyInfo; return historyInfo;
} catch (DataAccessException e) { } catch (DataAccessException e) {
LogUtil.FATAL_LOG.error("[list-config-history] error, nid:{}", new Object[] {nid}, e); LogUtil.FATAL_LOG.error("[detail-config-history] error, nid:{}", new Object[] {nid}, e);
throw e; throw e;
} }
} }

View File

@ -87,7 +87,7 @@ public class ConfigTraceService {
// (md5) // (md5)
String md5 = content == null ? null : MD5Utils.md5Hex(content, Constants.ENCODE); String md5 = content == null ? null : MD5Utils.md5Hex(content, Constants.ENCODE);
LogUtil.TRACE_LOG.info("{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}", InetUtils.getSelfIp(), dataId, group, tenant, LogUtil.TRACE_LOG.info("{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}", InetUtils.getSelfIP(), dataId, group, tenant,
requestIpAppName, ts, handleIp, "persist", type, -1, md5); requestIpAppName, ts, handleIp, "persist", type, -1, md5);
} }
@ -116,7 +116,7 @@ public class ConfigTraceService {
} }
//localIp | dataid | group | tenant | requestIpAppName | ts | handleIp | event | type | [delayed] | ext //localIp | dataid | group | tenant | requestIpAppName | ts | handleIp | event | type | [delayed] | ext
// (targetIp) // (targetIp)
LogUtil.TRACE_LOG.info("{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}", InetUtils.getSelfIp(), dataId, group, tenant, LogUtil.TRACE_LOG.info("{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}", InetUtils.getSelfIP(), dataId, group, tenant,
requestIpAppName, ts, handleIp, "notify", type, delayed, targetIp); requestIpAppName, ts, handleIp, "notify", type, delayed, targetIp);
} }
@ -143,7 +143,7 @@ public class ConfigTraceService {
tenant = null; tenant = null;
} }
//localIp | dataid | group | tenant | requestIpAppName | ts | handleIp | event | type | [delayed] | length //localIp | dataid | group | tenant | requestIpAppName | ts | handleIp | event | type | [delayed] | length
LogUtil.TRACE_LOG.info("{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}", InetUtils.getSelfIp(), dataId, group, tenant, LogUtil.TRACE_LOG.info("{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}", InetUtils.getSelfIP(), dataId, group, tenant,
requestIpAppName, ts, handleIp, "dump", type, delayed, length); requestIpAppName, ts, handleIp, "dump", type, delayed, length);
} }
@ -169,7 +169,7 @@ public class ConfigTraceService {
} }
//localIp | dataid | group | tenant | requestIpAppName | ts | handleIp | event | type | [delayed = -1] //localIp | dataid | group | tenant | requestIpAppName | ts | handleIp | event | type | [delayed = -1]
LogUtil.TRACE_LOG LogUtil.TRACE_LOG
.info("{}|{}|{}|{}|{}|{}|{}|{}|{}|{}", InetUtils.getSelfIp(), dataId, group, tenant, requestIpAppName, .info("{}|{}|{}|{}|{}|{}|{}|{}|{}|{}", InetUtils.getSelfIP(), dataId, group, tenant, requestIpAppName,
ts, handleIp, "dump-all", type, -1); ts, handleIp, "dump-all", type, -1);
} }
@ -196,7 +196,7 @@ public class ConfigTraceService {
} }
//localIp | dataid | group | tenant| requestIpAppName| ts | event | type | [delayed] | ext(clientIp) //localIp | dataid | group | tenant| requestIpAppName| ts | event | type | [delayed] | ext(clientIp)
LogUtil.TRACE_LOG LogUtil.TRACE_LOG
.info("{}|{}|{}|{}|{}|{}|{}|{}|{}|{}", InetUtils.getSelfIp(), dataId, group, tenant, requestIpAppName, .info("{}|{}|{}|{}|{}|{}|{}|{}|{}|{}", InetUtils.getSelfIP(), dataId, group, tenant, requestIpAppName,
ts, "pull", type, delayed, clientIp); ts, "pull", type, delayed, clientIp);
} }
} }

View File

@ -0,0 +1,47 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.config.server.utils;
import org.apache.commons.lang3.StringUtils;
/**
* namespace(tenant) util.
* Because config and naming treat namespace(tenant) differently,
* this tool class can only be used by the config module.
* @author klw(213539@qq.com)
* @date 2020/10/12 17:56
*/
public class NamespaceUtil {
private static final String NAMESPACE_PUBLIC_KEY = "public";
private static final String NAMESPACE_NULL_KEY = "null";
/**
* Treat the namespace(tenant) parameters with values of "public" and "null" as an empty string.
* @param tenant namespace(tenant) id
* @return java.lang.String A namespace(tenant) string processed
*/
public static String processNamespaceParameter(String tenant) {
if (StringUtils.isBlank(tenant) || NAMESPACE_PUBLIC_KEY.equalsIgnoreCase(tenant) || NAMESPACE_NULL_KEY
.equalsIgnoreCase(tenant)) {
return "";
}
return tenant.trim();
}
}

View File

@ -0,0 +1,47 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.config.server.utils;
import org.junit.Assert;
import org.junit.Test;
/**
* test NamespaceUtil.
*
* @author klw(213539 @ qq.com)
* @date 2020/10/13 9:46
*/
public class NamespaceUtilTest {
@Test
public void testProcessTenantParameter() {
String strPublic = "public";
String strNull = "null";
String strEmpty = "";
String strAbc = "abc";
String strdef123 = "def123";
String strAbcHasSpace = " abc ";
Assert.assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(strPublic));
Assert.assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(strNull));
Assert.assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(strEmpty));
Assert.assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(null));
Assert.assertEquals(strAbc, NamespaceUtil.processNamespaceParameter(strAbc));
Assert.assertEquals(strdef123, NamespaceUtil.processNamespaceParameter(strdef123));
Assert.assertEquals(strAbc, NamespaceUtil.processNamespaceParameter(strAbcHasSpace));
}
}

View File

@ -38,9 +38,5 @@ public class MemberMetaDataConstants {
public static final String VERSION = "version"; public static final String VERSION = "version";
public static final String[] META_KEY_LIST = new String[] {SITE_KEY, AD_WEIGHT, RAFT_PORT, WEIGHT, public static final String[] BASIC_META_KEYS = new String[] {SITE_KEY, AD_WEIGHT, RAFT_PORT, WEIGHT, VERSION};
LAST_REFRESH_TIME, VERSION};
public static final String[] META_KEY_LIST_WITHOUT_LAST_REFRESH_TIME = new String[] {SITE_KEY, AD_WEIGHT, RAFT_PORT,
WEIGHT, VERSION};
} }

View File

@ -28,6 +28,7 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -236,40 +237,40 @@ public class MemberUtils {
} }
/** /**
* Judge whether two member is full equals. * Judge whether basic info has changed.
* *
* @param actual actual member * @param actual actual member
* @param expected expected member * @param expected expected member
* @return true if all content is same, otherwise false * @return true if all content is same, otherwise false
*/ */
public static boolean fullEquals(Member actual, Member expected) { public static boolean isBasicInfoChanged(Member actual, Member expected) {
if (null == expected) { if (null == expected) {
return null == actual; return null == actual;
} }
if (!expected.getIp().equals(actual.getIp())) { if (!expected.getIp().equals(actual.getIp())) {
return false; return true;
} }
if (expected.getPort() != actual.getPort()) { if (expected.getPort() != actual.getPort()) {
return false; return true;
} }
if (!expected.getAddress().equals(actual.getAddress())) { if (!expected.getAddress().equals(actual.getAddress())) {
return false; return true;
} }
if (!expected.getState().equals(actual.getState())) { if (!expected.getState().equals(actual.getState())) {
return false; return true;
} }
return equalsExtendInfo(expected, actual); return isBasicInfoChangedInExtendInfo(expected, actual);
} }
private static boolean equalsExtendInfo(Member expected, Member actual) { private static boolean isBasicInfoChangedInExtendInfo(Member expected, Member actual) {
for (String each : MemberMetaDataConstants.META_KEY_LIST_WITHOUT_LAST_REFRESH_TIME) { for (String each : MemberMetaDataConstants.BASIC_META_KEYS) {
if (expected.getExtendInfo().containsKey(each) != actual.getExtendInfo().containsKey(each)) { if (expected.getExtendInfo().containsKey(each) != actual.getExtendInfo().containsKey(each)) {
return false; return true;
} }
if (null != expected.getExtendVal(each) && !expected.getExtendVal(each).equals(actual.getExtendVal(each))) { if (!Objects.equals(expected.getExtendVal(each), actual.getExtendVal(each))) {
return false; return true;
} }
} }
return true; return false;
} }
} }

View File

@ -78,7 +78,8 @@ import java.util.concurrent.ConcurrentSkipListMap;
@Component(value = "serverMemberManager") @Component(value = "serverMemberManager")
public class ServerMemberManager implements ApplicationListener<WebServerInitializedEvent> { public class ServerMemberManager implements ApplicationListener<WebServerInitializedEvent> {
private final NacosAsyncRestTemplate asyncRestTemplate = HttpClientBeanHolder.getNacosAsyncRestTemplate(Loggers.CORE); private final NacosAsyncRestTemplate asyncRestTemplate = HttpClientBeanHolder
.getNacosAsyncRestTemplate(Loggers.CORE);
/** /**
* Cluster node list. * Cluster node list.
@ -131,7 +132,7 @@ public class ServerMemberManager implements ApplicationListener<WebServerInitial
protected void init() throws NacosException { protected void init() throws NacosException {
Loggers.CORE.info("Nacos-related cluster resource initialization"); Loggers.CORE.info("Nacos-related cluster resource initialization");
this.port = ApplicationUtils.getProperty("server.port", Integer.class, 8848); this.port = ApplicationUtils.getProperty("server.port", Integer.class, 8848);
this.localAddress = InetUtils.getSelfIp() + ":" + port; this.localAddress = InetUtils.getSelfIP() + ":" + port;
this.self = MemberUtils.singleParse(this.localAddress); this.self = MemberUtils.singleParse(this.localAddress);
this.self.setExtendVal(MemberMetaDataConstants.VERSION, VersionUtils.version); this.self.setExtendVal(MemberMetaDataConstants.VERSION, VersionUtils.version);
serverList.put(self.getAddress(), self); serverList.put(self.getAddress(), self);
@ -169,14 +170,14 @@ public class ServerMemberManager implements ApplicationListener<WebServerInitial
NotifyCenter.registerSubscriber(new Subscriber<InetUtils.IPChangeEvent>() { NotifyCenter.registerSubscriber(new Subscriber<InetUtils.IPChangeEvent>() {
@Override @Override
public void onEvent(InetUtils.IPChangeEvent event) { public void onEvent(InetUtils.IPChangeEvent event) {
String newAddress = event.getNewIp() + ":" + port; String newAddress = event.getNewIP() + ":" + port;
ServerMemberManager.this.localAddress = newAddress; ServerMemberManager.this.localAddress = newAddress;
ApplicationUtils.setLocalAddress(localAddress); ApplicationUtils.setLocalAddress(localAddress);
Member self = ServerMemberManager.this.self; Member self = ServerMemberManager.this.self;
self.setIp(event.getNewIp()); self.setIp(event.getNewIP());
String oldAddress = event.getOldIp() + ":" + port; String oldAddress = event.getOldIP() + ":" + port;
ServerMemberManager.this.serverList.remove(oldAddress); ServerMemberManager.this.serverList.remove(oldAddress);
ServerMemberManager.this.serverList.put(newAddress, self); ServerMemberManager.this.serverList.put(newAddress, self);
@ -209,10 +210,11 @@ public class ServerMemberManager implements ApplicationListener<WebServerInitial
if (NodeState.DOWN.equals(newMember.getState())) { if (NodeState.DOWN.equals(newMember.getState())) {
memberAddressInfos.remove(newMember.getAddress()); memberAddressInfos.remove(newMember.getAddress());
} }
if (!MemberUtils.fullEquals(newMember, member)) { boolean isPublishChangeEvent = MemberUtils.isBasicInfoChanged(newMember, member);
newMember.setExtendVal(MemberMetaDataConstants.LAST_REFRESH_TIME, System.currentTimeMillis()); newMember.setExtendVal(MemberMetaDataConstants.LAST_REFRESH_TIME, System.currentTimeMillis());
MemberUtils.copy(newMember, member); MemberUtils.copy(newMember, member);
// member data changes and all listeners need to be notified if (isPublishChangeEvent) {
// member basic data changes and all listeners need to be notified
NotifyCenter.publishEvent(MembersChangeEvent.builder().members(allMembers()).build()); NotifyCenter.publishEvent(MembersChangeEvent.builder().members(allMembers()).build());
} }
return member; return member;
@ -482,10 +484,10 @@ public class ServerMemberManager implements ApplicationListener<WebServerInitial
ExceptionUtil.getAllExceptionMsg(throwable)); ExceptionUtil.getAllExceptionMsg(throwable));
MemberUtils.onFail(target, throwable); MemberUtils.onFail(target, throwable);
} }
@Override @Override
public void onCancel() { public void onCancel() {
} }
}); });
} catch (Throwable ex) { } catch (Throwable ex) {

View File

@ -33,7 +33,7 @@ public class StandaloneMemberLookup extends AbstractMemberLookup {
@Override @Override
public void start() { public void start() {
if (start.compareAndSet(false, true)) { if (start.compareAndSet(false, true)) {
String url = InetUtils.getSelfIp() + ":" + ApplicationUtils.getPort(); String url = InetUtils.getSelfIP() + ":" + ApplicationUtils.getPort();
afterLookup(MemberUtils.readServerConf(Collections.singletonList(url))); afterLookup(MemberUtils.readServerConf(Collections.singletonList(url)));
} }
} }

View File

@ -86,7 +86,7 @@ public class StartingSpringApplicationRunListener implements SpringApplicationRu
System.setProperty(MODE_PROPERTY_KEY_FUNCTION_MODE, ApplicationUtils.FUNCTION_MODE_NAMING); System.setProperty(MODE_PROPERTY_KEY_FUNCTION_MODE, ApplicationUtils.FUNCTION_MODE_NAMING);
} }
System.setProperty(LOCAL_IP_PROPERTY_KEY, InetUtils.getSelfIp()); System.setProperty(LOCAL_IP_PROPERTY_KEY, InetUtils.getSelfIP());
} }
@Override @Override

View File

@ -0,0 +1,106 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.core.cluster;
import com.alibaba.nacos.sys.utils.ApplicationUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.core.env.ConfigurableEnvironment;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@RunWith(MockitoJUnitRunner.class)
public class MemberUtilsTest {
private static final String IP = "1.1.1.1";
private static final int PORT = 8848;
@Mock
private ConfigurableEnvironment environment;
private Member originalMember;
@Before
public void setUp() {
ApplicationUtils.injectEnvironment(environment);
originalMember = buildMember();
}
private Member buildMember() {
return Member.builder().ip(IP).port(PORT).state(NodeState.UP).build();
}
@Test
public void testIsBasicInfoChangedNoChangeWithoutExtendInfo() {
Member newMember = buildMember();
assertFalse(MemberUtils.isBasicInfoChanged(newMember, originalMember));
}
@Test
public void testIsBasicInfoChangedNoChangeWithExtendInfo() {
Member newMember = buildMember();
newMember.setExtendVal("test", "test");
assertFalse(MemberUtils.isBasicInfoChanged(newMember, originalMember));
}
@Test
public void testIsBasicInfoChangedForIp() {
Member newMember = buildMember();
newMember.setIp("1.1.1.2");
assertTrue(MemberUtils.isBasicInfoChanged(newMember, originalMember));
}
@Test
public void testIsBasicInfoChangedForPort() {
Member newMember = buildMember();
newMember.setPort(PORT + 1);
assertTrue(MemberUtils.isBasicInfoChanged(newMember, originalMember));
}
@Test
public void testIsBasicInfoChangedForAddress() {
Member newMember = buildMember();
newMember.setAddress("test");
assertTrue(MemberUtils.isBasicInfoChanged(newMember, originalMember));
}
@Test
public void testIsBasicInfoChangedForStatus() {
Member newMember = buildMember();
newMember.setState(NodeState.DOWN);
assertTrue(MemberUtils.isBasicInfoChanged(newMember, originalMember));
}
@Test
public void testIsBasicInfoChangedForMoreBasicExtendInfo() {
Member newMember = buildMember();
newMember.setExtendVal(MemberMetaDataConstants.VERSION, "TEST");
assertTrue(MemberUtils.isBasicInfoChanged(newMember, originalMember));
}
@Test
public void testIsBasicInfoChangedForChangedBasicExtendInfo() {
Member newMember = buildMember();
newMember.setExtendVal(MemberMetaDataConstants.WEIGHT, "100");
assertTrue(MemberUtils.isBasicInfoChanged(newMember, originalMember));
}
}

View File

@ -0,0 +1,111 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.core.cluster;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.common.notify.EventPublisher;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.sys.utils.ApplicationUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.core.env.ConfigurableEnvironment;
import javax.servlet.ServletContext;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class ServerMemberManagerTest {
@Mock
private ConfigurableEnvironment environment;
@Mock
private ServletContext servletContext;
@Mock
private EventPublisher eventPublisher;
private ServerMemberManager serverMemberManager;
private static final AtomicBoolean EVENT_PUBLISH = new AtomicBoolean(false);
@Before
public void setUp() throws Exception {
when(environment.getProperty("server.port", Integer.class, 8848)).thenReturn(8848);
when(environment.getProperty("nacos.member-change-event.queue.size", Integer.class, 128)).thenReturn(128);
ApplicationUtils.injectEnvironment(environment);
when(servletContext.getContextPath()).thenReturn("");
serverMemberManager = new ServerMemberManager(servletContext);
serverMemberManager.updateMember(Member.builder().ip("1.1.1.1").port(8848).state(NodeState.UP).build());
serverMemberManager.getMemberAddressInfos().add("1.1.1.1:8848");
NotifyCenter.getPublisherMap().put(MembersChangeEvent.class.getCanonicalName(), eventPublisher);
}
@After
public void tearDown() throws NacosException {
EVENT_PUBLISH.set(false);
NotifyCenter.deregisterPublisher(MembersChangeEvent.class);
serverMemberManager.shutdown();
}
@Test
public void testUpdateNonExistMember() {
Member newMember = Member.builder().ip("1.1.1.2").port(8848).state(NodeState.UP).build();
assertFalse(serverMemberManager.update(newMember));
}
@Test
public void testUpdateDownMember() {
Member newMember = Member.builder().ip("1.1.1.1").port(8848).state(NodeState.DOWN).build();
assertTrue(serverMemberManager.update(newMember));
assertFalse(serverMemberManager.getMemberAddressInfos().contains("1.1.1.1:8848"));
verify(eventPublisher).publish(any(MembersChangeEvent.class));
}
@Test
public void testUpdateVersionMember() {
Member newMember = Member.builder().ip("1.1.1.1").port(8848).state(NodeState.UP).build();
newMember.setExtendVal(MemberMetaDataConstants.VERSION, "testVersion");
assertTrue(serverMemberManager.update(newMember));
assertTrue(serverMemberManager.getMemberAddressInfos().contains("1.1.1.1:8848"));
assertEquals("testVersion",
serverMemberManager.getServerList().get("1.1.1.1:8848").getExtendVal(MemberMetaDataConstants.VERSION));
verify(eventPublisher).publish(any(MembersChangeEvent.class));
}
@Test
public void testUpdateNonBasicExtendInfoMember() {
Member newMember = Member.builder().ip("1.1.1.1").port(8848).state(NodeState.UP).build();
newMember.setExtendVal("naming", "test");
assertTrue(serverMemberManager.update(newMember));
assertTrue(serverMemberManager.getMemberAddressInfos().contains("1.1.1.1:8848"));
assertEquals("test", serverMemberManager.getServerList().get("1.1.1.1:8848").getExtendVal("naming"));
verify(eventPublisher, never()).publish(any(MembersChangeEvent.class));
}
}

View File

@ -32,7 +32,7 @@ public class NetUtils {
* @return local server address * @return local server address
*/ */
public static String localServer() { public static String localServer() {
return InetUtils.getSelfIp() + UtilsAndCommons.IP_PORT_SPLITER + ApplicationUtils.getPort(); return InetUtils.getSelfIP() + UtilsAndCommons.IP_PORT_SPLITER + ApplicationUtils.getPort();
} }
/** /**

View File

@ -52,6 +52,8 @@ public class ClusterControllerTest extends BaseTest {
@Before @Before
public void before() { public void before() {
super.before(); super.before();
mockInjectSwitchDomain();
mockInjectDistroMapper();
mockmvc = MockMvcBuilders.standaloneSetup(clusterController).build(); mockmvc = MockMvcBuilders.standaloneSetup(clusterController).build();
} }
@ -87,17 +89,44 @@ public class ClusterControllerTest extends BaseTest {
} }
@Test @Test
public void testUpdateInvalidType() throws Exception { public void testUpdateHealthCheckerType() throws Exception {
expectedException.expectCause(isA(NacosException.class));
expectedException.expectMessage("unknown health check type:{\"type\":\"123\"}");
Service service = new Service(TEST_SERVICE_NAME); Service service = new Service(TEST_SERVICE_NAME);
service.setNamespaceId(Constants.DEFAULT_NAMESPACE_ID); service.setNamespaceId(Constants.DEFAULT_NAMESPACE_ID);
when(serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, TEST_SERVICE_NAME)).thenReturn(service); when(serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, TEST_SERVICE_NAME)).thenReturn(service);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders MockHttpServletRequestBuilder builder = MockMvcRequestBuilders
.put(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/cluster").param("clusterName", TEST_CLUSTER_NAME) .put(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/cluster").param("clusterName", TEST_CLUSTER_NAME)
.param("serviceName", TEST_SERVICE_NAME).param("healthChecker", "{\"type\":\"123\"}") .param("serviceName", TEST_SERVICE_NAME).param("healthChecker", "{\"type\":\"123\"}")
.param("checkPort", "1").param("useInstancePort4Check", "true"); .param("checkPort", "1").param("useInstancePort4Check", "true");
mockmvc.perform(builder); mockmvc.perform(builder);
Assert.assertEquals("NONE", service.getClusterMap().get(TEST_CLUSTER_NAME).getHealthChecker().getType());
MockHttpServletRequestBuilder builder2 = MockMvcRequestBuilders
.put(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/cluster").param("clusterName", TEST_CLUSTER_NAME)
.param("serviceName", TEST_SERVICE_NAME).param("healthChecker", "{\"type\":\"TCP\"}")
.param("checkPort", "1").param("useInstancePort4Check", "true");
mockmvc.perform(builder2);
Assert.assertEquals("TCP", service.getClusterMap().get(TEST_CLUSTER_NAME).getHealthChecker().getType());
MockHttpServletRequestBuilder builder3 = MockMvcRequestBuilders
.put(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/cluster").param("clusterName", TEST_CLUSTER_NAME)
.param("serviceName", TEST_SERVICE_NAME).param("healthChecker", "{\"type\":\"HTTP\"}")
.param("checkPort", "1").param("useInstancePort4Check", "true");
mockmvc.perform(builder3);
Assert.assertEquals("HTTP", service.getClusterMap().get(TEST_CLUSTER_NAME).getHealthChecker().getType());
MockHttpServletRequestBuilder builder4 = MockMvcRequestBuilders
.put(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/cluster").param("clusterName", TEST_CLUSTER_NAME)
.param("serviceName", TEST_SERVICE_NAME).param("healthChecker", "{\"type\":\"MYSQL\"}")
.param("checkPort", "1").param("useInstancePort4Check", "true");
mockmvc.perform(builder4);
Assert.assertEquals("MYSQL", service.getClusterMap().get(TEST_CLUSTER_NAME).getHealthChecker().getType());
} }
} }

View File

@ -346,7 +346,7 @@ public class ApplicationUtils implements ApplicationContextInitializer<Configura
public static String getLocalAddress() { public static String getLocalAddress() {
if (StringUtils.isBlank(localAddress)) { if (StringUtils.isBlank(localAddress)) {
localAddress = InetUtils.getSelfIp() + ":" + getPort(); localAddress = InetUtils.getSelfIP() + ":" + getPort();
} }
return localAddress; return localAddress;
} }

View File

@ -26,6 +26,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.net.Inet4Address; import java.net.Inet4Address;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface; import java.net.NetworkInterface;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
@ -56,11 +57,11 @@ public class InetUtils {
private static final Pattern IP_PATTERN = Pattern.compile(IP_REGEX); private static final Pattern IP_PATTERN = Pattern.compile(IP_REGEX);
private static String selfIp; private static String selfIP;
private static boolean useOnlySiteLocalInterface = false; private static boolean useOnlySiteLocalInterface = false;
private static boolean preferHostnameOverIp = false; private static boolean preferHostnameOverIP = false;
private static final List<String> PREFERRED_NETWORKS = new ArrayList<String>(); private static final List<String> PREFERRED_NETWORKS = new ArrayList<String>();
@ -82,55 +83,56 @@ public class InetUtils {
Runnable ipAutoRefresh = new Runnable() { Runnable ipAutoRefresh = new Runnable() {
@Override @Override
public void run() { public void run() {
String nacosIp = System.getProperty(NACOS_SERVER_IP); String nacosIP = System.getProperty(NACOS_SERVER_IP);
if (StringUtils.isBlank(nacosIp)) { if (StringUtils.isBlank(nacosIP)) {
nacosIp = ApplicationUtils.getProperty(IP_ADDRESS); nacosIP = ApplicationUtils.getProperty(IP_ADDRESS);
} }
if (!StringUtils.isBlank(nacosIp) && !isIP(nacosIp)) { boolean illegalIP = !StringUtils.isBlank(nacosIP) && !(isIP(nacosIP) || isDomain(nacosIP));
throw new RuntimeException("nacos address " + nacosIp + " is not ip"); if (illegalIP) {
throw new RuntimeException("nacos address " + nacosIP + " is not ip");
} }
String tmpSelfIp = nacosIp; String tmpSelfIP = nacosIP;
if (StringUtils.isBlank(tmpSelfIp)) { if (StringUtils.isBlank(tmpSelfIP)) {
preferHostnameOverIp = Boolean.getBoolean(SYSTEM_PREFER_HOSTNAME_OVER_IP); preferHostnameOverIP = Boolean.getBoolean(SYSTEM_PREFER_HOSTNAME_OVER_IP);
if (!preferHostnameOverIp) { if (!preferHostnameOverIP) {
preferHostnameOverIp = Boolean preferHostnameOverIP = Boolean
.parseBoolean(ApplicationUtils.getProperty(PREFER_HOSTNAME_OVER_IP)); .parseBoolean(ApplicationUtils.getProperty(PREFER_HOSTNAME_OVER_IP));
} }
if (preferHostnameOverIp) { if (preferHostnameOverIP) {
InetAddress inetAddress; InetAddress inetAddress;
try { try {
inetAddress = InetAddress.getLocalHost(); inetAddress = InetAddress.getLocalHost();
if (inetAddress.getHostName().equals(inetAddress.getCanonicalHostName())) { if (inetAddress.getHostName().equals(inetAddress.getCanonicalHostName())) {
tmpSelfIp = inetAddress.getHostName(); tmpSelfIP = inetAddress.getHostName();
} else { } else {
tmpSelfIp = inetAddress.getCanonicalHostName(); tmpSelfIP = inetAddress.getCanonicalHostName();
} }
} catch (UnknownHostException ignore) { } catch (UnknownHostException ignore) {
LOG.warn("Unable to retrieve localhost"); LOG.warn("Unable to retrieve localhost");
} }
} else { } else {
tmpSelfIp = Objects.requireNonNull(findFirstNonLoopbackAddress()).getHostAddress(); tmpSelfIP = Objects.requireNonNull(findFirstNonLoopbackAddress()).getHostAddress();
} }
} }
if (!Objects.equals(selfIp, tmpSelfIp) && Objects.nonNull(selfIp)) { if (!Objects.equals(selfIP, tmpSelfIP) && Objects.nonNull(selfIP)) {
IPChangeEvent event = new IPChangeEvent(); IPChangeEvent event = new IPChangeEvent();
event.setOldIp(selfIp); event.setOldIP(selfIP);
event.setNewIp(tmpSelfIp); event.setNewIP(tmpSelfIP);
NotifyCenter.publishEvent(event); NotifyCenter.publishEvent(event);
} }
selfIp = tmpSelfIp; selfIP = tmpSelfIP;
} }
}; };
ipAutoRefresh.run(); ipAutoRefresh.run();
} }
public static String getSelfIp() { public static String getSelfIP() {
return selfIp; return selfIP;
} }
/** /**
@ -219,35 +221,50 @@ public class InetUtils {
return matcher.matches(); return matcher.matches();
} }
/**
* juege str is right domain.
*
* @param str nacosIP
* @return nacosIP is domain
*/
public static boolean isDomain(String str) {
InetSocketAddress address = new InetSocketAddress(str, 0);
boolean unResolved = address.isUnresolved();
if (unResolved) {
LOG.warn("the domain: '" + str + "' can not be resolved");
}
return !unResolved;
}
/** /**
* {@link com.alibaba.nacos.core.cluster.ServerMemberManager} is listener. * {@link com.alibaba.nacos.core.cluster.ServerMemberManager} is listener.
*/ */
@SuppressWarnings({"PMD.ClassNamingShouldBeCamelRule", "checkstyle:AbbreviationAsWordInName"}) @SuppressWarnings({"PMD.ClassNamingShouldBeCamelRule", "checkstyle:AbbreviationAsWordInName"})
public static class IPChangeEvent extends SlowEvent { public static class IPChangeEvent extends SlowEvent {
private String oldIp; private String oldIP;
private String newIp; private String newIP;
public String getOldIp() { public String getOldIP() {
return oldIp; return oldIP;
} }
public void setOldIp(String oldIp) { public void setOldIP(String oldIP) {
this.oldIp = oldIp; this.oldIP = oldIP;
} }
public String getNewIp() { public String getNewIP() {
return newIp; return newIP;
} }
public void setNewIp(String newIp) { public void setNewIP(String newIP) {
this.newIp = newIp; this.newIP = newIP;
} }
@Override @Override
public String toString() { public String toString() {
return "IPChangeEvent{" + "oldIp='" + oldIp + '\'' + ", newIp='" + newIp + '\'' + '}'; return "IPChangeEvent{" + "oldIP='" + oldIP + '\'' + ", newIP='" + newIP + '\'' + '}';
} }
} }

View File

@ -357,7 +357,7 @@ public class ConfigDerbyRaft_DITCase extends BaseClusterTest {
// transfer leader to ip:8807 // transfer leader to ip:8807
Map<String, String> transfer = new HashMap<>(); Map<String, String> transfer = new HashMap<>();
transfer.put(JRaftConstants.TRANSFER_LEADER, InetUtils.getSelfIp() + ":9847"); transfer.put(JRaftConstants.TRANSFER_LEADER, InetUtils.getSelfIP() + ":9847");
RestResult<String> result = protocol7.execute(transfer); RestResult<String> result = protocol7.execute(transfer);
System.out.println(result); System.out.println(result);
Assert.assertTrue(result.ok()); Assert.assertTrue(result.ok());
@ -372,7 +372,7 @@ public class ConfigDerbyRaft_DITCase extends BaseClusterTest {
// transfer leader to ip:8808 // transfer leader to ip:8808
transfer = new HashMap<>(); transfer = new HashMap<>();
transfer.put(JRaftConstants.TRANSFER_LEADER, InetUtils.getSelfIp() + ":9848"); transfer.put(JRaftConstants.TRANSFER_LEADER, InetUtils.getSelfIP() + ":9848");
result = protocol8.execute(transfer); result = protocol8.execute(transfer);
System.out.println(result); System.out.println(result);
Assert.assertTrue(result.ok()); Assert.assertTrue(result.ok());
@ -387,7 +387,7 @@ public class ConfigDerbyRaft_DITCase extends BaseClusterTest {
// transfer leader to ip:8809 // transfer leader to ip:8809
transfer = new HashMap<>(); transfer = new HashMap<>();
transfer.put(JRaftConstants.TRANSFER_LEADER, InetUtils.getSelfIp() + ":9849"); transfer.put(JRaftConstants.TRANSFER_LEADER, InetUtils.getSelfIP() + ":9849");
result = protocol9.execute(transfer); result = protocol9.execute(transfer);
System.out.println(result); System.out.println(result);
Assert.assertTrue(result.ok()); Assert.assertTrue(result.ok());

View File

@ -90,7 +90,7 @@ public class BaseClusterTest extends HttpClient4Test {
static { static {
System.getProperties().setProperty("nacos.core.auth.enabled", "false"); System.getProperties().setProperty("nacos.core.auth.enabled", "false");
System.getProperties().setProperty("embeddedStorage", "true"); System.getProperties().setProperty("embeddedStorage", "true");
String ip = InetUtils.getSelfIp(); String ip = InetUtils.getSelfIP();
clusterInfo = "nacos.member.list=" + ip + ":8847," + ip + ":8848," + ip + ":8849"; clusterInfo = "nacos.member.list=" + ip + ":8847," + ip + ":8848," + ip + ":8849";
NotifyCenter.registerSubscriber(new Subscriber<RaftDbErrorEvent>() { NotifyCenter.registerSubscriber(new Subscriber<RaftDbErrorEvent>() {

View File

@ -39,7 +39,7 @@ public class InetUtils_ITCase {
static { static {
System.setProperty("nacos.core.inet.auto-refresh", "3"); System.setProperty("nacos.core.inet.auto-refresh", "3");
// For load InetUtils.class // For load InetUtils.class
InetUtils.getSelfIp(); InetUtils.getSelfIP();
} }
@Test @Test
@ -53,10 +53,10 @@ public class InetUtils_ITCase {
Subscriber<InetUtils.IPChangeEvent> subscribe = new Subscriber<InetUtils.IPChangeEvent>() { Subscriber<InetUtils.IPChangeEvent> subscribe = new Subscriber<InetUtils.IPChangeEvent>() {
@Override @Override
public void onEvent(InetUtils.IPChangeEvent event) { public void onEvent(InetUtils.IPChangeEvent event) {
if (Objects.nonNull(event.getOldIp())) { if (Objects.nonNull(event.getOldIP())) {
try { try {
System.out.println(event); System.out.println(event);
reference.set(event.getNewIp()); reference.set(event.getNewIP());
} }
finally { finally {
latch.countDown(); latch.countDown();
@ -74,7 +74,7 @@ public class InetUtils_ITCase {
latch.await(10_000L, TimeUnit.MILLISECONDS); latch.await(10_000L, TimeUnit.MILLISECONDS);
Assert.assertEquals(testIp, reference.get()); Assert.assertEquals(testIp, reference.get());
Assert.assertEquals(testIp, InetUtils.getSelfIp()); Assert.assertEquals(testIp, InetUtils.getSelfIP());
} }
} }

View File

@ -69,7 +69,7 @@ public class MemberLookup_ITCase extends BaseTest {
DiskUtils.forceMkdir(Paths.get(path, "conf").toString()); DiskUtils.forceMkdir(Paths.get(path, "conf").toString());
File file = Paths.get(path, "conf", name).toFile(); File file = Paths.get(path, "conf", name).toFile();
DiskUtils.touch(file); DiskUtils.touch(file);
String ip = InetUtils.getSelfIp(); String ip = InetUtils.getSelfIP();
DiskUtils.writeFile(file, (ip + ":8848," + ip + ":8847," + ip + ":8849").getBytes( DiskUtils.writeFile(file, (ip + ":8848," + ip + ":8847," + ip + ":8849").getBytes(
StandardCharsets.UTF_8), false); StandardCharsets.UTF_8), false);