从网上找了一个示例程序,但是用的不是很好,其中还有点问题,最后根据我自己的使用贴一个示例,希望能给大家学习时提供点参考:
zkClient.h
#pragma once
#include "../zookeeper/zookeeper.h"
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h>
#include <fcntl.h>
#include <map>
#include <vector>
#include <list>
using namespace std;
#define ONE_SECONDS 1000*1
class zkClient
{
public:
zkClient(void);
~zkClient(void);
public:
void ConnectZK(const std::string& strzkServerIP,int nPort);//连接Zk
void ConnectZK(std::string strzkServer);//连接Zk
void Update();
void SetNodePath(std::string strNodePath);
public:
zhandle_t *m_zhandle;
bool connected;
private:
std::string m_strNodePath;
std::string m_strServerIP;
int nPort;
};
zkClient.cpp
//#include "StdAfx.h"
#include "zkClient.h"
#include <glog/logging.h>
#include "../Json/JsonBase.h"
#include "ctool.h"
#include <string.h>
#include "protocol.h"
using namespace std;
#define random(a,b) (rand()%(b-a+1)+a)
void InitWatcher(zhandle_t *zh, int type, int state, const char