Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto get local ip for TseerServer & TseerAgent and some other enhancement #19

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions TseerAgent/TseerAgent.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
installpath=/usr/local
#路由服务中心地址,必填
locator=Tseer.TseerServer.QueryObj@tcp -h 127.0.0.1 -p 9903
#方便云端路由中心管理的地址,必须是非127.0.0.1
localip=127.0.0.1
#方便云端路由中心管理的地址,必须是非127.0.0.1,自动获取,如自动获取失败,手动填写可生效
#localip=127.0.0.1
#提供给api查询的地址,默认是127.0.0.1
#routerip=127.0.0.1
</server>
114 changes: 51 additions & 63 deletions TseerAgent/src/TseerAgentServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,18 +343,22 @@ int parseConfig(int argc, char *argv[])
}

string configFile = "";
if (Op.hasParam("config"))
{
if (Op.hasParam("config")) {
configFile = Op.getValue("config");
}
else
{
} else {
usage();
}

//获取本机配置的 ip (IPV4)
char iface[8];
char ip[INET_ADDRSTRLEN];
if (!get_default_if(iface, 8) || !get_ip(iface, ip, INET_ADDRSTRLEN)) {
cerr << FILE_FUN << " get default ip via /proc/net/route failed, using default value instead" << endl;
memset(ip, 0, INET_ADDRSTRLEN);
}

//读取配置文件
if(!TC_File::isFileExistEx(configFile))
{
if(!TC_File::isFileExistEx(configFile)) {
cerr <<configFile << " file don't exist!" << endl;
exit(-1);
}
Expand All @@ -363,64 +367,47 @@ int parseConfig(int argc, char *argv[])
paramConf.parseFile(configFile);

string sInstallpatch = paramConf.get("/server<installpath>", "");
if(sInstallpatch.empty())
{
if(sInstallpatch.empty()) {
sInstallpatch = g_app.g_installPath;
}
else
{
//记录安装路径
g_app.g_installPath = sInstallpatch;
}

//解析主控地址列表
std::string locator = paramConf.get("/server<locator>", "");

//主要是要注册到路由中心方便云端管理,所以这里必须是内网ip
std::string innerIp = paramConf.get("/server<localip>", "");

//默认是绑定本地127.0.0.1,提供给api访问
std::string routerIp = paramConf.get("/server<routerip>", "127.0.0.1");
if (locator.empty())
{
if (locator.empty()) {
cerr<<"you should provide locator"<<endl;
exit(-1);
}

//解析主控地址列表
if(locator.find("@") == string::npos)
{
if(locator.find("@") == string::npos) {
string tmpList;
vector<string> ipPortlist = TC_Common::sepstr<string>(locator,"|;");
for (size_t i = 0; i < ipPortlist.size(); i++)
{
for (size_t i = 0; i < ipPortlist.size(); i++) {
vector<string> ipPort = TC_Common::sepstr<string>(ipPortlist[i],":");
if(ipPort.size() < 2)
{
if(ipPort.size() < 2) {
cerr<<FILE_FUN<< ipPortlist[i]<<" is invalid"<<endl;
continue;
}

string endPoint= "tcp -h " + ipPort[0] + " -p " + ipPort[1];
if (tmpList != "")
{
if (tmpList != "") {
tmpList += ":" + endPoint;
}
else
{
} else {
tmpList = endPoint;
}
}

if(tmpList.empty())
{
if(tmpList.empty()) {
cerr<<"invalid locator param:"<<locator<<endl;
exit(-1);
}
locator = TSEERSERVER_MODULE + ".QueryObj@"+tmpList;
}

if(routerIp.empty())
{
//主要是要注册到路由中心方便云端管理,所以这里必须是内网ip
std::string innerIp = paramConf.get("/server<localip>", ip);

//默认是绑定本地127.0.0.1,提供给api访问
std::string routerIp = paramConf.get("/server<routerip>", "127.0.0.1");
if(routerIp.empty()) {
//监听127.0.0.1
routerIp = "127.0.0.1";
}
Expand All @@ -436,53 +423,54 @@ int parseConfig(int argc, char *argv[])
}
}

g_app.g_configFile = confPath + "/."+TSEERAGENT_SERVERNAME + ".conf";
g_app.g_innerIp = innerIp;

g_app.g_installPath = sInstallpatch;
g_app.g_configFile = confPath + "/."+TSEERAGENT_SERVERNAME + ".conf";
g_app.g_innerIp = innerIp;

TC_Config newConf;
map<string, string> m;

//server config
m["app"]= TSEERAGENT_APPNAME;
m["server"]= TSEERAGENT_SERVERNAME;
string sModuleName = TSEERAGENT_APPNAME + "." + TSEERAGENT_SERVERNAME;
m["localip"]= innerIp;

//server config
m["app"] = TSEERAGENT_APPNAME;
m["server"] = TSEERAGENT_SERVERNAME;
m["localip"] = innerIp;
// #服务的可执行文件
m["basepath"]= TC_File::simplifyDirectory(sInstallpatch + "/"+TSEERAGENT_APPNAME+ "/" + TSEERAGENT_SERVERNAME+"/bin/");
m["basepath"] = TC_File::simplifyDirectory(sInstallpatch + "/"+TSEERAGENT_APPNAME+ "/" + TSEERAGENT_SERVERNAME+"/bin/");
//服务的数据目录
m["datapath"]= TC_File::simplifyDirectory(sInstallpatch + "/" + TSEERAGENT_APPNAME + "/" + TSEERAGENT_SERVERNAME+"/data");
m["logpath"]= TC_File::simplifyDirectory(sInstallpatch + "/" + TSEERAGENT_APPNAME + "/" + TSEERAGENT_SERVERNAME +"/app_log");
m["logLevel"]= "DEBUG";
m["datapath"] = TC_File::simplifyDirectory(sInstallpatch + "/" + TSEERAGENT_APPNAME + "/" + TSEERAGENT_SERVERNAME+"/data");
m["logpath"] = TC_File::simplifyDirectory(sInstallpatch + "/" + TSEERAGENT_APPNAME + "/" + TSEERAGENT_SERVERNAME +"/app_log");
m["logLevel"] = paramConf.get("/server<logLevel>", "DEBUG");
//滚动日志大小和个数
m["logsize"]= TC_Common::tostr(1024*1024*15);
m["logsize"] = TC_Common::tostr(1024*1024*15);
m["closecout"] = "1";
newConf.insertDomainParam( "/tars/application/server", m, true);

//servant config
m.clear();
m["endpoint"]= "udp -h " + routerIp +" -p 8865 -t 60000";
m["maxconns"]= "10000";
m["threads"]= "8";
m["queuecap"]= "10000";
m["endpoint"] = "udp -h " + routerIp +" -p 8865 -t 60000";
m["maxconns"] = "10000";
m["threads"] = "8";
m["queuecap"] = "10000";
m["protocol"] = "tars";
m["queuetimeout"] = "60000";
m["servant"] = sModuleName + ".RouterObj";
m["allow"] = "";
m["servant"] = sModuleName + ".RouterObj";
m["allow"] = "";
m["handlegroup"] = "RouterObjAdapter";
newConf.insertDomainParam( "/tars/application/server/RouterObjAdapter", m, true);

if(!g_app.g_innerIp.empty())
{
m.clear();
m["endpoint"]= "tcp -h " + g_app.g_innerIp +" -p 9765 -t 60000";
m["maxconns"]= "10000";
m["threads"]= "5";
m["queuecap"]= "10000";
m["endpoint"] = "tcp -h " + g_app.g_innerIp +" -p 9765 -t 60000";
m["maxconns"] = "10000";
m["threads"] = "5";
m["queuecap"] = "10000";
m["protocol"] = "tars";
m["queuetimeout"] = "60000";
m["servant"] = sModuleName + ".UpdateObj";
m["allow"] = "";
m["servant"] = sModuleName + ".UpdateObj";
m["allow"] = "";
m["handlegroup"] = "UpdateObjAdapter";
newConf.insertDomainParam( "/tars/application/server/UpdateObjAdapter", m, true);
}
Expand Down
79 changes: 62 additions & 17 deletions TseerAgent/src/util.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
/**
* Tencent is pleased to support the open source community by making Tseer available.
*
* Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

/**
* Tencent is pleased to support the open source community by making Tseer available.
*
* Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
#ifndef _UTIL_H
#define _UTIL_H

#include <netinet/in.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include "util/tc_common.h"
#include "RollLogger.h"


inline bool popen_sendMsg(const string& sCommand)
{
FILE *fp;
Expand Down Expand Up @@ -47,5 +51,46 @@ inline string display(const T& t)
t.displaySimple(stream);
return(stream.str());
}
#endif

// get default network interface
inline bool get_default_if(char *iface, int size) {
if (size < 8) return false;
memset(iface, 0, size);

FILE *f = fopen("/proc/net/route", "r");
if (!f) return false;

char dest[64] = {0, };
while (!feof(f)) {
if (fscanf(f, "%s %s %*[^\r\n]%*c", iface, dest) != 2) continue;
if (strcmp(dest, "00000000") == 0) {
break;
}
}

return strlen(iface) ? true : false;
}

// get eth0's ipv4 address
inline bool get_ip(const char *iface, char* ip, int size) {
if (size < INET_ADDRSTRLEN) return false;
memset(ip, 0, size);

struct ifaddrs * ifAddrStruct = NULL;
struct ifaddrs * ifa = NULL;
void * tmpAddrPtr = NULL;

getifaddrs(&ifAddrStruct);
for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
if ((ifa->ifa_addr->sa_family == AF_INET) && (strcmp(ifa->ifa_name,iface) == 0)) {
tmpAddrPtr = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
inet_ntop(AF_INET, tmpAddrPtr, ip, INET_ADDRSTRLEN);
}
}

if (ifAddrStruct != NULL) freeifaddrs(ifAddrStruct);

return strlen(ip) ? true : false;
}

#endif
4 changes: 2 additions & 2 deletions TseerServer/TseerServer.conf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<server>
#服务安装路径,默认是/usr/local,必填
installpath=/home/louishuang/onlineseer/seer/mytest
#服务绑定本机地址,必填
localip=127.0.0.1
#服务绑定本机地址,自动获取,如失败,手动填写可生效
#localip=127.0.0.1
#服务日志等级,默认是DEBUG
logLevel=DEBUG
#服务默认的存储方式可选:etcd/mysql,默认是etcd
Expand Down
10 changes: 9 additions & 1 deletion TseerServer/src/TSeerServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ int parseConfig(int argc, char *argv[])
useAge();
}

//获取本机配置的 ip (IPV4)
char iface[8];
char ip[INET_ADDRSTRLEN];
if (!get_default_if(iface, 8) || !get_ip(iface, ip, INET_ADDRSTRLEN)) {
cerr << FILE_FUN << " get default ip via /proc/net/route failed, using 127.0.0.1 instead" << endl;
strcpy(ip, "127.0.0.1");
}

//读取配置文件
if(!TC_File::isFileExistEx(configFile))
{
Expand Down Expand Up @@ -297,7 +305,7 @@ int parseConfig(int argc, char *argv[])
#endif
}

string sInnerIp = conf.get("/server<localip>", "127.0.0.1");
string sInnerIp = conf.get("/server<localip>", ip);
string regPort = conf.get("/server<regport>", "9902");
string queryPort = conf.get("/server<queryport>", "9903");
string apiPort = conf.get("/server<apiport>", "9904");
Expand Down
Loading