Skip to content

Commit

Permalink
register service with auto-generated instanceid to avoid instance bei…
Browse files Browse the repository at this point in the history
…ng null
  • Loading branch information
TTTTTAAAAAKKKKEEEENNNN committed Jun 7, 2021
1 parent 4ff85e4 commit f91852d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/NacosExceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

};

Expand Down
1 change: 1 addition & 0 deletions src/config/AppConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
16 changes: 16 additions & 0 deletions src/utils/NetUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <ifaddrs.h>
#include <src/log/Logger.h>
#include <string.h>
#include <unistd.h>

#define HOST_AND_LEN 250

namespace nacos{

Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions src/utils/NetUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions test/testcase/testNamingService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit f91852d

Please sign in to comment.