• 大小: 74MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-07-19
  • 语言: C/C++
  • 标签: etcd  C++  client  

资源简介

自己封装的etcd c++ client, 有线上项目使用先例。接口参考头文件,附带测试代码,有需要的话可以随意修改,写的不合适的代码还请见谅。

资源截图

代码片段和文件信息


#include “EtcdClientV3.h“
#include 
#include 

int ECSTDCALL CreateECClient(IEtcdClient** client PECParam param)
{
int ret = EC_SUCCESS;
do
{
CEtcdClient* pClient = new(std::nothrow) CEtcdClient();
if (pClient == NULL)
{
ret = EC_SYSTEM_ERROR;
break;
}

if (!pClient->Init(param))
{
delete pClient;
ret = EC_SYSTEM_ERROR;
break;
}

*client = pClient;

} while (false);

return ret;
}

void ECSTDCALL ReleaseClient(IEtcdClient* client)
{
CEtcdClient* pClient = static_cast(client);
pClient->UnInit();
}

CEtcdClient::CEtcdClient()
{
m_bInit = false;
m_bStop = false;
m_bAlreadyWatch = false;
m_kvClient      = NULL;
m_watchClient   = NULL;
m_leaseClient   = NULL;
m_leaseID       = 0;
m_pThread       = NULL;
m_ttl = 60;
m_nCurrentHostIndex = 0;
}

CEtcdClient::~CEtcdClient()
{
UnInit();
}

bool CEtcdClient::SplitHostInfo(std::string hostAddr)
{
size_t pos = hostAddr.find_first_of(‘;‘);
while (hostAddr.length() > 0)
{
std::string str = hostAddr.substr(0 pos);
m_hostArray.push_back(str);
if (pos == std::string::npos)
break;
hostAddr = hostAddr.substr(pos + 1);
pos = hostAddr.find_first_of(‘;‘);
}

return m_hostArray.size() > 0;
}

std::string CEtcdClient::GetHost()
{
if (m_hostArray.size() > 0)
{
int nIndex = m_nCurrentHostIndex;
m_nCurrentHostIndex = (m_nCurrentHostIndex + 1) % m_hostArray.size();
return m_hostArray[nIndex];
}

return ““;
}

bool CEtcdClient::Init(PECParam param)
{
if (m_bInit)
return true;

do
{
m_logManager.CreateLog();

param->server_addr[sizeof(param->server_addr) - 1] = ‘\0‘;
if (!SplitHostInfo(param->server_addr))
{
LOG(“info“ LOG_LEVEL_ERROR “split addr failed addr: %s“ param->server_addr);
break;
}

m_timer.SetTimer(3 * 1000);
/*m_ttl = param->ttl;
if (m_ttl > 180 || m_ttl == 0)
m_ttl = 180;*/

int loop_count = 0;
while (loop_count++ < m_hostArray.size())
{
string host = GetHost();
m_channel = grpc::CreateChannel(host grpc::InsecureChannelCredentials());

std::chrono::system_clock::time_point deadline =
std::chrono::system_clock::now() + std::chrono::seconds(3);

if (m_channel->WaitForConnected(deadline))
{
LOG(“info“ LOG_LEVEL_INFO “connect to host: %s successful“ host.c_str());
break;
}
else
{
m_channel.reset();
LOG(“info“ LOG_LEVEL_ERROR “connect to host: %s failed“ host.c_str());
}
}

m_kvClient = new(std::nothrow) KVClient(m_channel);
if (m_kvClient == NULL)
break;

m_watchClient = new(std::nothrow) WatchClient(m_channel this);
if (m_watchClient == NULL)
break;

m_leaseClient = new(std::nothrow) LeaseClient(m_channel);
if (m_leaseClient == NULL)
break;

//m_leaseID = m_leaseClient->GetLeaseID(m_ttl);
//if (m_leaseID <= 0)
// break;

m_pThread = new(std::

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-01-29 10:35  EtcdClientV3\
     文件        7206  2017-08-28 20:30  EtcdClientV3\EtcdClientV3.cpp
     文件        1716  2017-08-15 10:03  EtcdClientV3\EtcdClientV3.h
     文件       10096  2017-07-06 12:13  EtcdClientV3\EtcdClientV3.vcxproj
     文件        3389  2017-07-03 14:41  EtcdClientV3\EtcdClientV3.vcxproj.filters
     文件        4940  2017-07-03 15:03  EtcdClientV3\LogManager.cpp
     文件        1859  2017-07-03 14:27  EtcdClientV3\LogManager.h
     文件        1711  2017-07-06 10:48  EtcdClientV3\Makefile
     文件       12899  2017-08-15 11:02  EtcdClientV3\RpcClient.cpp
     文件        2464  2017-08-14 19:06  EtcdClientV3\RpcClient.h
     文件        2026  2017-07-03 13:28  EtcdClientV3\util.h
     文件        2097  2017-07-03 09:46  EtcdClientV3.sln
     目录           0  2018-01-29 10:35  Release\
     文件     1741312  2017-08-28 20:32  Release\EtcdClientV3.dll
     文件         855  2017-08-28 20:31  Release\EtcdClientV3.exp
     文件    15519785  2017-08-28 20:32  Release\EtcdClientV3.iobj
     文件     5691696  2017-08-28 20:32  Release\EtcdClientV3.ipdb
     文件        2088  2017-08-28 20:31  Release\EtcdClientV3.lib
     文件    15749120  2017-08-28 20:32  Release\EtcdClientV3.pdb
     文件     1262080  2017-06-12 11:05  Release\libeay32.dll
     文件      294400  2017-06-12 11:05  Release\ssleay32.dll
     文件      391680  2017-06-12 16:40  Release\zlib.dll
     目录           0  2018-01-29 10:35  TestEtcdClientV3\
     文件        7821  2017-07-03 09:52  TestEtcdClientV3\TestEtcdClientV3.vcxproj
     文件         957  2017-07-03 09:46  TestEtcdClientV3\TestEtcdClientV3.vcxproj.filters
     文件        5070  2017-12-19 11:11  TestEtcdClientV3\TestEtctClientV3.cpp
     文件          14  2015-09-20 17:20  TestEtcdClientV3\test.h
     目录           0  2018-01-29 10:35  debug\
     文件     8614400  2017-08-28 20:31  debug\EtcdClientV3_d.dll
     文件         857  2017-08-28 20:31  debug\EtcdClientV3_d.exp
     文件    20595428  2017-08-28 20:31  debug\EtcdClientV3_d.ilk
............此处省略392个文件信息

评论

共有 条评论