文章目录
使用etcd v3接口
本文主要提供一种C++访问etcd v3的方式,主要思路如下:
包中带上etcdctl命令,然后在代码中使用popen执行命令行,如:etcdctl get foo 来获取foo的值
// 使用方式举例:ExecuteShellCmd(“./etcdctl get foo”“, true, reply);
bool ExecuteShellCmd(const std::string cmd, bool needResult, std::string &result)
{
FILE *fp = NULL;
fp = popen(cmd.c_str(),"re");
if(fp == NULL)
{
printf("[ExecuteShellCmd]Execute failed,cmd:%s,needResult:%d",cmd.c_str(),needResult);
return false;
}
if (needResult)
{
int bufLen = 5*1024*1024;
char *buf = new char[bufLen];
fread(buf, bufLen, 1, fp);
result = buf;
delete[] buf;
}
printf("[ExecuteShellCmd]cmd:%s,needResult:%d,result:%s",cmd.c_str(),needResult,result.c_str());
pclose(fp);
}
watch的实现方式类似,具体代码如下:
FILE *fp = NULL;
char command[128];
char watch