in nlsCppSdk/framework/common/nlsClientImpl.cpp [726:808]
int NlsClientImpl::vipServerGetIp(const std::string &vipServerDomain,
const int vipServerPort,
const std::string &targetDomain,
std::string &url) {
#ifdef ENABLE_VIPSERVER
if (vipServerDomain.empty() || targetDomain.empty() || vipServerPort < 0) {
LOG_ERROR("vipServerGetIp::Input Param error ...");
return -(InvalidInputParam);
}
int tmpPort = vipServerPort;
char buff[512] = {0};
if (tmpPort == 0) {
tmpPort = VipServerPort;
}
if (snprintf(buff, 512, "%s:%d", vipServerDomain.c_str(), tmpPort) < 0) {
return -(InvalidInputParam);
}
LOG_DEBUG("vipServerGetIp: %s.", buff);
MUTEX_LOCK(_mtx);
if (!_isInitalizeVsClient) {
Option option;
//设置日志最大大小(可选)
option.set_max_log_size(10LL * 1024 * 1024);
//初始化
// buff: vipserver顶级域名
if (!VipClientApi::Init(buff, option)) {
LOG_ERROR("Init failed top domain:%s errno:%d errstr:%s.", buff,
VipClientApi::Errno(), VipClientApi::Errstr());
MUTEX_UNLOCK(_mtx);
return -(VipClientInitFailed);
} else {
_isInitalizeVsClient = true;
LOG_INFO("VipClientApi::Init Successed ...");
}
}
MUTEX_UNLOCK(_mtx);
std::string ip;
IPHost host;
// 同步获取域名(targetDomain)下的一个IPHost
if (VipClientApi::QueryIp(targetDomain.c_str(), &host, 5 * 1000)) {
std::string host_str = helper::ToString(host);
LOG_DEBUG("get domain ip ok %s, iphost:%s", targetDomain.c_str(),
host_str.c_str());
} else {
LOG_ERROR("Target domain:%s QueryIp::Failed. errno:%d errstr:%s.",
targetDomain.c_str(), VipClientApi::Errno(),
VipClientApi::Errstr());
}
//同步获取IP列表,2000ms超时
IPHost iphost;
if (VipClientApi::QueryIp(targetDomain.c_str(), &iphost, 5 * 1000)) {
ip = iphost.ip();
int port = iphost.port();
LOG_DEBUG("valid ip %s %d", iphost.ip(), iphost.port());
char buffer[256] = {0};
if (snprintf(buffer, 256, "ws://%s:%d/ws/v1", ip.c_str(), port) < 0) {
LOG_ERROR("ERROR : Merge host Failed.");
return -(VipClientMergeHostFailed);
}
url = buffer;
LOG_DEBUG("targetUrl: %s", url.c_str());
return Success;
} else {
LOG_ERROR("ERROR: QueryIp Failed. errno:%d errstr:%s.",
VipClientApi::Errno(), VipClientApi::Errstr());
return -(VipClientQueryIpFailed);
}
#else
LOG_WARN("Donnot enable VipServer.");
#endif // ENABLE_VIPSERVER
return Success;
}