CentOS 7에 Zookeeper 3.4.9 C client library 빌드 하기
설치 및 셋팅 2016. 9. 22. 10:03
주키퍼 C 라이브러리 빌드
1 2 3 4 5 6 7 8 9 10 11 | $ wget http: //apache .org /dist/zookeeper/zookeeper-3 .4.9 /zookeeper-3 .4.9. tar .gz $ tar -xvzf zookeeper-3.4.9. tar .gz $ cd zookeeper-3.4.9 /src/c # cppunit 없으면 설치 $ yum -y install cppunit-devel # autoconf 버전은 2.59이상이어야 한다. # 버전 확인법 autoconf -V $ autoreconf - if $ . /configure $ make $ make install |
샘플 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <zookeeper/zookeeper.h> | |
#include <string.h> | |
#include <list> | |
#include <unistd.h> | |
using namespace std; | |
void watcher(zhandle_t *, int event, int state, const char *path, void*v) { | |
if (state == ZOO_EXPIRED_SESSION_STATE) | |
cout << "state:" << "ZOO_EXPIRED_SESSION_STATE" << endl; | |
else if (state == ZOO_AUTH_FAILED_STATE) | |
cout << "state:" << "ZOO_AUTH_FAILED_STATE" << endl; | |
else if (state == ZOO_CONNECTING_STATE) | |
cout << "state:" << "ZOO_CONNECTING_STATE" << endl; | |
else if (state == ZOO_ASSOCIATING_STATE) | |
cout << "state:" << "ZOO_ASSOCIATING_STATE" << endl; | |
else if (state == ZOO_CONNECTED_STATE) | |
cout << "state:" << "ZOO_CONNECTED_STATE" << endl; | |
else | |
cout << "unknown state:" << state << endl; | |
if (event == ZOO_CREATED_EVENT) | |
cout << "event:" << "ZOO_CREATED_EVENT" << endl; | |
else if (event == ZOO_DELETED_EVENT) | |
cout << "event:" << "ZOO_DELETED_EVENT" << endl; | |
else if (event == ZOO_CHANGED_EVENT) | |
cout << "event:" << "ZOO_CHANGED_EVENT" << endl; | |
else if (event == ZOO_CHILD_EVENT) | |
cout << "event:" << "ZOO_CHILD_EVENT" << endl; | |
else if (event == ZOO_SESSION_EVENT) | |
cout << "event:" << "ZOO_SESSION_EVENT" << endl; | |
else if (event == ZOO_NOTWATCHING_EVENT) | |
cout << "event:" << "ZOO_NOTWATCHING_EVENT" << endl; | |
else | |
cout << "unknown event:" << event << endl; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
const string HOST("172.20.30.141:2181"); | |
const int RECV_TIMEOUT = 10000; | |
zhandle_t *zh = zookeeper_init(HOST.c_str(), watcher, RECV_TIMEOUT, nullptr, nullptr, 0); | |
const char* myData = "testall"; | |
const char* myPath = "/mytest"; | |
struct Stat stat; | |
char newPath[512]; | |
int rc = 0; | |
rc = zoo_exists(zh, myPath, 0, &stat); | |
if (rc == ZNONODE) | |
rc = zoo_create(zh, myPath, myData, sizeof(myData), &ZOO_OPEN_ACL_UNSAFE, ZOO_EPHEMERAL, newPath, sizeof(newPath)); | |
char buffer[512]; | |
int len = 512; | |
rc = zoo_wget(zh, myPath, nullptr, nullptr, buffer, &len, &stat); | |
cout << "myData:" << buffer << endl; | |
sleep(10); | |
zookeeper_close(zh); | |
return 0; | |
} |
샘플 실행
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $ g++ -std=c++11 hellozookeeper.cpp -o hellozookeeper -lzookeeper_mt . /hellozookeeper 2016-09-23 10:28:45,856:27919(0x7f348e9d0740):ZOO_INFO@log_env@726: Client environment:zookeeper.version=zookeeper C client 3.4.9 2016-09-23 10:28:45,856:27919(0x7f348e9d0740):ZOO_INFO@log_env@730: Client environment:host.name=CRD-AP-GMS00001 2016-09-23 10:28:45,856:27919(0x7f348e9d0740):ZOO_INFO@log_env@737: Client environment:os.name=Linux 2016-09-23 10:28:45,856:27919(0x7f348e9d0740):ZOO_INFO@log_env@738: Client environment:os.arch=3.10.0-229.el7.x86_64 2016-09-23 10:28:45,856:27919(0x7f348e9d0740):ZOO_INFO@log_env@739: Client environment:os.version= #1 SMP Fri Mar 6 11:36:42 UTC 2015 2016-09-23 10:28:45,856:27919(0x7f348e9d0740):ZOO_INFO@log_env@747: Client environment:user.name=root 2016-09-23 10:28:45,857:27919(0x7f348e9d0740):ZOO_INFO@log_env@755: Client environment:user.home= /root 2016-09-23 10:28:45,857:27919(0x7f348e9d0740):ZOO_INFO@log_env@767: Client environment:user. dir = /tmp/project/hellozookeeper 2016-09-23 10:28:45,857:27919(0x7f348e9d0740):ZOO_INFO@zookeeper_init@800: Initiating client connection, host=172.20.30.141:2181 sessionTimeout=10000 watcher=0x401228 sessionId=0 sessionPasswd=<null> context=(nil) flags=0 2016-09-23 10:28:45,857:27919(0x7f348d51c700):ZOO_INFO@check_events@1728: initiated connection to server [172.20.30.141:2181] 2016-09-23 10:28:45,865:27919(0x7f348d51c700):ZOO_INFO@check_events@1775: session establishment complete on server [172.20.30.141:2181], sessionId=0x157509d62b6000b, negotiated timeout=10000 state:ZOO_CONNECTED_STATE event:ZOO_SESSION_EVENT myData:testall 2016-09-23 10:28:55,871:27919(0x7f348e9d0740):ZOO_INFO@zookeeper_close@2527: Closing zookeeper sessionId=0x157509d62b6000b to [172.20.30.141:2181] |