From addb77b5d5c02e9ba8fcbc51c2b7a25a56064e30 Mon Sep 17 00:00:00 2001 From: ruanshudong Date: Tue, 24 Mar 2020 21:41:56 +0800 Subject: [PATCH] suport tarsnode deploy in domain --- servant/libservant/Application.cpp | 6 ++++-- servant/libservant/EndpointManager.cpp | 20 ++++++++++---------- servant/libservant/StatReport.cpp | 11 ++++++----- servant/libservant/TarsConfig.cpp | 5 +++-- servant/libservant/TarsCurrent.cpp | 14 ++++++++++++-- servant/libservant/TarsLogger.cpp | 8 ++++---- servant/servant/Application.h | 1 + servant/servant/EndpointManager.h | 1 - servant/servant/TarsConfig.h | 2 +- servant/servant/TarsCurrent.h | 6 ++++++ servant/servant/TarsLogger.h | 2 +- 11 files changed, 48 insertions(+), 28 deletions(-) diff --git a/servant/libservant/Application.cpp b/servant/libservant/Application.cpp index 109c2de..6e86a7d 100644 --- a/servant/libservant/Application.cpp +++ b/servant/libservant/Application.cpp @@ -97,6 +97,7 @@ std::string ServerConfig::Key; bool ServerConfig::VerifyClient = false; #endif +map ServerConfig::Context; /////////////////////////////////////////////////////////////////////////////////////////// TC_Config Application::_conf; @@ -475,7 +476,7 @@ bool Application::cmdLoadProperty(const string& command, const string& params, s ServerConfig::Config = _conf.get("/tars/application/server"); - TarsRemoteConfig::getInstance()->setConfigInfo(_communicator, ServerConfig::Config, ServerConfig::Application, ServerConfig::ServerName, ServerConfig::BasePath,setDivision()); + TarsRemoteConfig::getInstance()->setConfigInfo(_communicator, ServerConfig::Config, ServerConfig::Application, ServerConfig::ServerName, ServerConfig::BasePath,setDivision(), 5); ServerConfig::Notify = _conf.get("/tars/application/server"); @@ -1049,6 +1050,7 @@ void Application::initializeServer() ServerConfig::BackPacketLimit = TC_Common::strto(_conf.get("/tars/application/server", "100*1024*1024")); ServerConfig::BackPacketMin = TC_Common::strto(_conf.get("/tars/application/server", "1024")); + ServerConfig::Context["node_name"] = ServerConfig::LocalIp; #if TARS_SSL ServerConfig::CA = _conf.get("/tars/application/server"); ServerConfig::Cert = _conf.get("/tars/application/server"); @@ -1149,7 +1151,7 @@ void Application::initializeServer() /////////////////////////////////////////////////////////////////////////////////////////////////// //初始化到信息中心代理 cout << OUT_LINE << "\n" << TC_Common::outfill("[set remote notify] ") << "OK" << endl; - TarsRemoteNotify::getInstance()->setNotifyInfo(_communicator, ServerConfig::Notify, ServerConfig::Application, ServerConfig::ServerName, setDivision()); + TarsRemoteNotify::getInstance()->setNotifyInfo(_communicator, ServerConfig::Notify, ServerConfig::Application, ServerConfig::ServerName, setDivision(), ServerConfig::LocalIp); /////////////////////////////////////////////////////////////////////////////////////////////////// //初始化到Node的代理 diff --git a/servant/libservant/EndpointManager.cpp b/servant/libservant/EndpointManager.cpp index fbbf2ae..7a74694 100644 --- a/servant/libservant/EndpointManager.cpp +++ b/servant/libservant/EndpointManager.cpp @@ -389,16 +389,16 @@ void QueryEpBase::refreshReg(GetEndpointType type, const string & sName) { case E_ALL: { - iRet = _queryFPrx->findObjectById4Any(_objName,activeEp,inactiveEp); + iRet = _queryFPrx->findObjectById4Any(_objName,activeEp,inactiveEp, ServerConfig::Context); break; } case E_STATION: { - iRet = _queryFPrx->findObjectByIdInSameStation(_objName,sName,activeEp,inactiveEp); + iRet = _queryFPrx->findObjectByIdInSameStation(_objName,sName,activeEp,inactiveEp, ServerConfig::Context); } case E_SET: { - iRet = _queryFPrx->findObjectByIdInSameSet(_objName,sName,activeEp,inactiveEp); + iRet = _queryFPrx->findObjectByIdInSameSet(_objName,sName,activeEp,inactiveEp, ServerConfig::Context); break; } case E_DEFAULT: @@ -408,11 +408,11 @@ void QueryEpBase::refreshReg(GetEndpointType type, const string & sName) { //指定set调用时,指定set的优先级最高 string setId = _invokeSetId.empty()?ClientConfig::SetDivision:_invokeSetId; - iRet = _queryFPrx->findObjectByIdInSameSet(_objName,setId,activeEp,inactiveEp); + iRet = _queryFPrx->findObjectByIdInSameSet(_objName,setId,activeEp,inactiveEp, ServerConfig::Context); } else { - iRet = _queryFPrx->findObjectByIdInSameGroup(_objName,activeEp,inactiveEp); + iRet = _queryFPrx->findObjectByIdInSameGroup(_objName,activeEp,inactiveEp, ServerConfig::Context); } break; } @@ -425,17 +425,17 @@ void QueryEpBase::refreshReg(GetEndpointType type, const string & sName) { case E_ALL: { - _queryFPrx->async_findObjectById4Any(this,_objName); + _queryFPrx->async_findObjectById4Any(this,_objName, ServerConfig::Context); break; } case E_STATION: { - _queryFPrx->async_findObjectByIdInSameStation(this,_objName,sName); + _queryFPrx->async_findObjectByIdInSameStation(this,_objName,sName, ServerConfig::Context); break; } case E_SET: { - _queryFPrx->async_findObjectByIdInSameSet(this,_objName,sName); + _queryFPrx->async_findObjectByIdInSameSet(this,_objName,sName, ServerConfig::Context); break; } case E_DEFAULT: @@ -445,11 +445,11 @@ void QueryEpBase::refreshReg(GetEndpointType type, const string & sName) { //指定set调用时,指定set的优先级最高 string setId = _invokeSetId.empty()?ClientConfig::SetDivision:_invokeSetId; - _queryFPrx->async_findObjectByIdInSameSet(this,_objName,setId); + _queryFPrx->async_findObjectByIdInSameSet(this,_objName,setId, ServerConfig::Context); } else { - _queryFPrx->async_findObjectByIdInSameGroup(this,_objName); + _queryFPrx->async_findObjectByIdInSameGroup(this,_objName, ServerConfig::Context); } break; } diff --git a/servant/libservant/StatReport.cpp b/servant/libservant/StatReport.cpp index a30a023..5fda852 100755 --- a/servant/libservant/StatReport.cpp +++ b/servant/libservant/StatReport.cpp @@ -19,6 +19,7 @@ #include "util/tc_timeprovider.h" #include "servant/TarsLogger.h" #include "servant/Communicator.h" +#include "servant/Application.h" #include namespace tars @@ -36,7 +37,7 @@ StatReport::StatReport(size_t iEpollNum) , _epollNum(iEpollNum) , _retValueNumLimit(10) { - srand(time(NULL)); + srand(time(NULL)); for(size_t i = 0 ; i < _epollNum; i++) { @@ -477,7 +478,7 @@ int StatReport::reportMicMsg(MapStatMicMsg& msg,bool bFromClient) if(_statPrx) { TLOGTARS("[TARS][StatReport::reportMicMsg send size:" << mTemp.size()<<"]"<< endl); - _statPrx->tars_set_timeout(_reportTimeout)->async_reportMicMsg(NULL,mTemp,bFromClient); + _statPrx->tars_set_timeout(_reportTimeout)->async_reportMicMsg(NULL,mTemp,bFromClient, ServerConfig::Context); } iLen = iTemLen; mTemp.clear(); @@ -499,7 +500,7 @@ int StatReport::reportMicMsg(MapStatMicMsg& msg,bool bFromClient) if(_statPrx) { TLOGTARS("[TARS][StatReport::reportMicMsg send size:" << mTemp.size()<<"]"<< endl); - _statPrx->tars_set_timeout(_reportTimeout)->async_reportMicMsg(NULL,mTemp,bFromClient); + _statPrx->tars_set_timeout(_reportTimeout)->async_reportMicMsg(NULL,mTemp,bFromClient, ServerConfig::Context); } } return 0; @@ -690,7 +691,7 @@ int StatReport::reportSampleMsg() if(_statPrx) { TLOGTARS("[TARS][StatReport::reportSampleMsg send size:" << vTemp.size()<< "]"<< endl); - _statPrx->tars_set_timeout(_reportTimeout)->async_reportSampleMsg(NULL,vTemp); + _statPrx->tars_set_timeout(_reportTimeout)->async_reportSampleMsg(NULL,vTemp, ServerConfig::Context); } iLen = iTemLen; vTemp.clear(); @@ -702,7 +703,7 @@ int StatReport::reportSampleMsg() if(_statPrx) { TLOGTARS("[TARS][StatReport::reportSampleMsg send size:" << vTemp.size()<< "]"<< endl); - _statPrx->tars_set_timeout(_reportTimeout)->async_reportSampleMsg(NULL,vTemp); + _statPrx->tars_set_timeout(_reportTimeout)->async_reportSampleMsg(NULL,vTemp, ServerConfig::Context); } } diff --git a/servant/libservant/TarsConfig.cpp b/servant/libservant/TarsConfig.cpp index 40d2ce4..077d17e 100755 --- a/servant/libservant/TarsConfig.cpp +++ b/servant/libservant/TarsConfig.cpp @@ -18,6 +18,7 @@ #include "util/tc_file.h" #include "servant/Communicator.h" #include "servant/TarsNotify.h" +#include "servant/Application.h" #include namespace tars @@ -108,7 +109,7 @@ string TarsRemoteConfig::getRemoteFile(const string &sFileName, bool bAppConfigO { if(_setdivision.empty()) { - ret = _configPrx->loadConfig(_app, (bAppConfigOnly ? "" : _serverName), sFileName, stream); + ret = _configPrx->loadConfig(_app, (bAppConfigOnly ? "" : _serverName), sFileName, stream, ServerConfig::Context); } else { @@ -118,7 +119,7 @@ string TarsRemoteConfig::getRemoteFile(const string &sFileName, bool bAppConfigO confInfo.filename = sFileName; confInfo.bAppOnly = bAppConfigOnly; confInfo.setdivision = _setdivision; - ret = _configPrx->loadConfigByInfo(confInfo,stream); + ret = _configPrx->loadConfigByInfo(confInfo,stream, ServerConfig::Context); } break; diff --git a/servant/libservant/TarsCurrent.cpp b/servant/libservant/TarsCurrent.cpp index 7ab277e..df5e440 100644 --- a/servant/libservant/TarsCurrent.cpp +++ b/servant/libservant/TarsCurrent.cpp @@ -60,10 +60,20 @@ TarsCurrent::~TarsCurrent() } } +const string &TarsCurrent::getHostName() const +{ + auto it = _request.context.find("node_name"); + if(it != _request.context.end()) + { + return it->second; + } + return _data->ip(); + +} + const string &TarsCurrent::getIp() const { - return _data->ip(); - // return _ip; + return _data->ip(); } int TarsCurrent::getPort() const diff --git a/servant/libservant/TarsLogger.cpp b/servant/libservant/TarsLogger.cpp index 41a8454..0669f82 100644 --- a/servant/libservant/TarsLogger.cpp +++ b/servant/libservant/TarsLogger.cpp @@ -84,7 +84,7 @@ void RollWriteT::operator()(ostream &of, const deque > &ds) { try { - _logPrx->logger(DYEING_DIR, DYEING_FILE, "roll", "%Y%m%d", vRemoteDyeing); + _logPrx->logger(DYEING_DIR, DYEING_FILE, "roll", "%Y%m%d", vRemoteDyeing, ServerConfig::Context); } catch(exception &ex) { @@ -254,7 +254,7 @@ void RemoteTimeWriteT::sync2remote(const vector &v) stInfo.sSepar = _timeWrite->_separ; stInfo.sLogType = _timeWrite->_logType; - _timeWrite->_logPrx->loggerbyInfo(stInfo,v); + _timeWrite->_logPrx->loggerbyInfo(stInfo,v, ServerConfig::Context); if (_timeWrite->_reportSuccPtr) { @@ -276,7 +276,7 @@ void RemoteTimeWriteT::sync2remoteDyeing(const vector &v) { try { - _timeWrite->_logPrx->logger(DYEING_DIR, DYEING_FILE, "", _timeWrite->_format, v); + _timeWrite->_logPrx->logger(DYEING_DIR, DYEING_FILE, "", _timeWrite->_format, v, ServerConfig::Context); } catch(exception &ex) { @@ -418,7 +418,7 @@ void TimeWriteT::operator()(ostream &of, const deque > &buf { try { - _logPrx->logger(DYEING_DIR, DYEING_FILE, "day", "%Y%m%d", vDyeingLog); + _logPrx->logger(DYEING_DIR, DYEING_FILE, "day", "%Y%m%d", vDyeingLog, ServerConfig::Context); } catch(exception &ex) { diff --git a/servant/servant/Application.h b/servant/servant/Application.h index a85ac7d..66aca63 100644 --- a/servant/servant/Application.h +++ b/servant/servant/Application.h @@ -140,6 +140,7 @@ struct ServerConfig static std::string Key; static bool VerifyClient; #endif + static map Context; //框架内部用, 传递节点名称(以域名形式部署时) }; class PropertyReport; diff --git a/servant/servant/EndpointManager.h b/servant/servant/EndpointManager.h index f4ff397..84e9e74 100644 --- a/servant/servant/EndpointManager.h +++ b/servant/servant/EndpointManager.h @@ -246,7 +246,6 @@ protected: */ set _inactiveEndpoints; - private: /////////以下是请求主控的策略信息///////////////// diff --git a/servant/servant/TarsConfig.h b/servant/servant/TarsConfig.h index d52f8c8..09b4a5a 100644 --- a/servant/servant/TarsConfig.h +++ b/servant/servant/TarsConfig.h @@ -57,7 +57,7 @@ public: * * @return int */ - int setConfigInfo(const CommunicatorPtr &comm, const string &obj, const string & app, const string &serverName, const string& basePath,const string& setdivision="",int maxBakNum=5); + int setConfigInfo(const CommunicatorPtr &comm, const string &obj, const string & app, const string &serverName, const string& basePath,const string& setdivision="",int maxBakNum = 5); /** * 读取ConfigServer上配置文件到本地,并备份原文件 diff --git a/servant/servant/TarsCurrent.h b/servant/servant/TarsCurrent.h index b59e20e..50335cb 100644 --- a/servant/servant/TarsCurrent.h +++ b/servant/servant/TarsCurrent.h @@ -56,6 +56,12 @@ public: const string &getIp() const; /** + * get host name + * @return + */ + const string &getHostName() const; + + /** * 获取端口 * @return int */ diff --git a/servant/servant/TarsLogger.h b/servant/servant/TarsLogger.h index 2645860..c948f03 100755 --- a/servant/servant/TarsLogger.h +++ b/servant/servant/TarsLogger.h @@ -529,7 +529,7 @@ public: * @param server, 服务名称 * @param logpath, 日志路径 */ - void setLogInfo(const CommunicatorPtr &comm, const string &obj, const string &sApp, const string &sServer, const string &sLogpath,const string& setdivision="", const bool &bLogStatReport = false); + void setLogInfo(const CommunicatorPtr &comm, const string &obj, const string &sApp, const string &sServer, const string &sLogpath,const string& setdivision="", const bool &bLogStatReport=false); /** * 初始化设置时间格式("%Y%m%d")