diff --git a/include/NacosExceptions.h b/include/NacosExceptions.h index 56f9561..be451fd 100644 --- a/include/NacosExceptions.h +++ b/include/NacosExceptions.h @@ -81,6 +81,7 @@ class NacosException : public std::exception { static const int UNABLE_TO_GET_HOST_IP = 1008; static const int UNABLE_TO_CREATE_SOCKET = 1009; static const int INVALID_CONFIG_PARAM = 1010; + static const int UNABLE_TO_GET_HOST_NAME = 1011; }; diff --git a/src/config/AppConfigManager.cpp b/src/config/AppConfigManager.cpp index d3da064..935c5c0 100644 --- a/src/config/AppConfigManager.cpp +++ b/src/config/AppConfigManager.cpp @@ -102,6 +102,7 @@ void AppConfigManager::initDefaults() { NacosString homedir = DirUtils::getHome(); + set(PropertyKeyConst::INSTANCE_ID_PREFIX, NetUtils::getHostName()); set(PropertyKeyConst::INSTANCE_ID_SEQ_FILE, homedir + ConfigConstant::FILE_SEPARATOR + "nacos" + ConfigConstant::FILE_SEPARATOR + "instance_seq.dat"); set(PropertyKeyConst::NACOS_SNAPSHOT_PATH, homedir + ConfigConstant::FILE_SEPARATOR + "nacos" + ConfigConstant::FILE_SEPARATOR + "snapshot"); log_info("[AppConfigManager]-initDefaults:DEFAULT_SNAPSHOT_PATH:%s\n", appConfig[PropertyKeyConst::NACOS_SNAPSHOT_PATH].c_str()); diff --git a/src/utils/NetUtils.cpp b/src/utils/NetUtils.cpp index 44935a7..2b986e2 100644 --- a/src/utils/NetUtils.cpp +++ b/src/utils/NetUtils.cpp @@ -5,6 +5,9 @@ #include #include #include +#include + +#define HOST_AND_LEN 250 namespace nacos{ @@ -44,4 +47,17 @@ NacosString NetUtils::getHostIp() NACOS_THROW(NacosException){ //Usually the program will not run to here throw NacosException(NacosException::UNABLE_TO_GET_HOST_IP, "Failed to get IF address"); } + +NacosString NetUtils::getHostName() NACOS_THROW(NacosException) +{ + char hostname[HOST_AND_LEN]; + + int res = gethostname(hostname, HOST_AND_LEN); + if (res == 0) { + return NacosString(hostname); + } + + throw NacosException(NacosException::UNABLE_TO_GET_HOST_NAME, "Failed to get hostname, errno = " + NacosStringOps::valueOf(errno)); +} + }//namespace nacos diff --git a/src/utils/NetUtils.h b/src/utils/NetUtils.h index 71de3b6..bb7b23e 100644 --- a/src/utils/NetUtils.h +++ b/src/utils/NetUtils.h @@ -10,6 +10,9 @@ class NetUtils { public: //Get IP address (best guess) static NacosString getHostIp() NACOS_THROW(NacosException); + + //Get hostname + static NacosString getHostName() NACOS_THROW(NacosException); }; }//namespace nacos diff --git a/test/testcase/testNamingService.cpp b/test/testcase/testNamingService.cpp index e9806c3..952156e 100644 --- a/test/testcase/testNamingService.cpp +++ b/test/testcase/testNamingService.cpp @@ -71,8 +71,10 @@ bool testNamingProxySmokeTest() { for (int i = 0; i < 10; i++) { NacosString serviceName = "TestServiceName" + NacosStringOps::valueOf(i); NacosString serverlist = namingProxy->queryList(serviceName, ConfigConstant::DEFAULT_GROUP, "TestCluster", 0, false); - - if (serverlist.find("\"serviceName\":\"" + serviceName + "\"") == string::npos) { + cout << serverlist << endl; + if (serverlist.find("\"serviceName\":\"" + serviceName + "\"") == string::npos && + //nacos 2.x compatibility + serverlist.find("\"serviceName\":\"DEFAULT_GROUP@@" + serviceName + "\"") == string::npos) { cout << "Failed to get data for:" << serviceName << endl; return false; }