Skip to content

Commit

Permalink
Client-side generated instanceId when registering a service
Browse files Browse the repository at this point in the history
  • Loading branch information
TTTTTAAAAAKKKKEEEENNNN committed Jun 6, 2021
1 parent b94a7bb commit 4ff85e4
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions include/constant/PropertyKeyConst.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class PropertyKeyConst {

static const int NACOS_DEFAULT_PORT = 8848;

static const NacosString INSTANCE_ID_SEQ_FILE;

static const NacosString INSTANCE_ID_PREFIX;

/*public static class SystemEnv {
static const NacosString ALIBABA_ALIWARE_ENDPOINT_PORT = "ALIBABA_ALIWARE_ENDPOINT_PORT";
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_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
2 changes: 2 additions & 0 deletions src/constant/PropertyKeyConst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ const NacosString PropertyKeyConst::CLIENT_NAME = "nacos.client.name";
const NacosString PropertyKeyConst::AUTH_USERNAME = "nacos.auth.username";
const NacosString PropertyKeyConst::AUTH_PASSWORD = "nacos.auth.password";
const NacosString PropertyKeyConst::LOCAL_IP = "nacos.client.ip";
const NacosString PropertyKeyConst::INSTANCE_ID_SEQ_FILE = "nacos.instId.seq.file";
const NacosString PropertyKeyConst::INSTANCE_ID_PREFIX = "nacos.instId.prefix";
}//namespace nacos
5 changes: 5 additions & 0 deletions src/factory/NacosServiceFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "src/naming/subscribe/HostReactor.h"
#include "src/security/SecurityManager.h"
#include "src/utils/ConfigParserUtils.h"
#include "src/utils/SequenceProvider.h"
#include "utils/DirUtils.h"

//Unlike Java, in cpp, there's no container, no spring to do the ORM job, so I have to handle it myself
Expand Down Expand Up @@ -114,6 +115,10 @@ NamingService *NacosServiceFactory::CreateNamingService() NACOS_THROW(NacosExcep
HostReactor *hostReactor = new HostReactor(objectConfigData);
objectConfigData->_hostReactor = hostReactor;

const NacosString &seqConfile = appConfigManager->get(PropertyKeyConst::INSTANCE_ID_SEQ_FILE);
SequenceProvider<int64_t> *sequenceProvider = new SequenceProvider<int64_t>(seqConfile, 1, 10);
objectConfigData->_sequenceProvider = sequenceProvider;

objectConfigData->checkAssembledObject();
NamingService *instance = new NacosNamingService(objectConfigData);

Expand Down
8 changes: 8 additions & 0 deletions src/factory/ObjectConfigData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "src/listen/ClientWorker.h"
#include "src/security/SecurityManager.h"
#include "utils/UuidUtils.h"
#include "src/utils/SequenceProvider.h"

namespace nacos{

Expand Down Expand Up @@ -44,6 +45,7 @@ void ObjectConfigData::checkNamingService() NACOS_THROW(NacosException) {
NACOS_ASSERT(_serverListManager != NULL);
NACOS_ASSERT(_udpNamingServiceListener != NULL);
NACOS_ASSERT(_udpNamingServiceListener != NULL);
NACOS_ASSERT(_sequenceProvider != NULL);
}

void ObjectConfigData::checkConfigService() NACOS_THROW(NacosException) {
Expand Down Expand Up @@ -206,6 +208,12 @@ void ObjectConfigData::destroyNamingService() {
delete _appConfigManager;
_appConfigManager = NULL;
}

if (_sequenceProvider != NULL)
{
delete _sequenceProvider;
_sequenceProvider = NULL;
}
}

void ObjectConfigData::destroyMaintainService() {
Expand Down
2 changes: 2 additions & 0 deletions src/factory/ObjectConfigData.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class LocalSnapshotManager;
class SecurityManager;
class UdpNamingServiceListener;
class HostReactor;
template <typename T>class SequenceProvider;

enum FactoryType {
CONFIG = 0,
Expand Down Expand Up @@ -58,6 +59,7 @@ class ObjectConfigData {
SecurityManager *_securityManager;
UdpNamingServiceListener *_udpNamingServiceListener;
HostReactor *_hostReactor;
SequenceProvider<int64_t> *_sequenceProvider;
};
}//namespace nacos

Expand Down
4 changes: 3 additions & 1 deletion src/naming/NacosNamingService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "src/naming/subscribe/SubscriptionPoller.h"
#include "src/naming/subscribe/UdpNamingServiceListener.h"
#include "src/naming/beat/BeatReactor.h"
#include "src/utils/SequenceProvider.h"
#include "utils/NamingUtils.h"
#include "constant/UtilAndComs.h"
#include "utils/ParamUtils.h"
Expand Down Expand Up @@ -88,7 +89,8 @@ void NacosNamingService::registerInstance
const NacosString &groupName,
Instance &instance
) NACOS_THROW(NacosException) {

const NacosString &instanceIdPrefix = _objectConfigData->_appConfigManager->get(PropertyKeyConst::INSTANCE_ID_PREFIX);
instance.instanceId = instanceIdPrefix + NacosStringOps::valueOf(_objectConfigData->_sequenceProvider->next());
if (instance.ephemeral) {
BeatInfo beatInfo;
beatInfo.serviceName = NamingUtils::getGroupedName(serviceName, groupName);
Expand Down

0 comments on commit 4ff85e4

Please sign in to comment.