当前位置:   article > 正文

C++实现zookeeper主从切换_c++ 主从

c++ 主从

1.下载zk

2.配置zk

       查看有无java环境。

       java -version

       安装java环境。

       yum list java* 查看yum源的java包。

       yum -y install java-1.8.0-openjdk 安装java包。

       在/usr/local/zookeeper下解压压缩包,解压后的文件夹重命名为zookeeper-3.5.5,进入zookeeper-3.5.5/conf目录,重命名zoo_sample.cfg为zoo.cfg,打开zoo.cfg,修改红色部分为dataDir=/usr/local/zookeeper/ zookeeper-3.5.5/data,并增加灰色底色部分。

       # the directory where the snapshot is stored.

       # do not use /tmp for storage, /tmp here is just

       # example sakes.

       dataDir=/tmp/zookeeper

       dataLogDir=/usr/local/zookeeper/zookeeper-3.5.5/logs

 

       将 zookeeper 的根目录设置到系统环境变量 PATH 中:

       sudo vi /etc/profile

       在打开的 profile 文件末尾追加如下配置:

       export ZOOKEEPER_HOME=/usr/local/zookeeper/zookeeper-3.5.5

       export PATH=$ZOOKEEPER_HOME/bin:$PATH

       export PATH

       保存并退出 vi;

       刷新 profile 文件使之立即生效:

       source /etc/profile

3.启动zk

        进入apache-zookeeper-3.5.5-bin/bin目录,执行:

        sh ./zkServer.sh start

4.C++包含的头文件和库

  1. 下载
  2. 解压后进入目录:apache-zookeeper-3.5.5/zookeeper-client/zookeeper-client-c
  3. 执行命令:

        autoreconf -if

        ./configure --disable-shared --without-cppunit

        make

       make install

        编译C lib库

  1. 编译后头文件为:/usr/local/include/zookeeper/zookeeper.h
  2. 编译后静态库文件为:/usr/local/lib/libzookeeper_mt.a
  3. 程序makefile需要引用zookeeper.h, libzookeeper_mt.a才能编译通过。

       -bash: autoreconf:未找到命令:

       yum  -y install install autoconf automake libtool

 

server.h

  1. #include <zookeeper/zookeeper.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <string.h>
  5. #include <unistd.h>
  6. using namespace std;
  7. class Server
  8. {
  9. public:
  10. Server(string path, string data, int timeout);
  11. ~Server();
  12. int connect_zk(const char *host);
  13. int create_zk_nodes();
  14. void start_work();
  15. private:
  16. void work();
  17. void run_for_master();
  18. void regist_master();
  19. static void zk_watcher(zhandle_t *zh, int type, int state, const char *path,void *watcherCtx);
  20. static void node_watcher(zhandle_t *zh, int type, int state, const char *path,void *watcherCtx);
  21. private:
  22. string m_strNodePath;
  23. string m_strNodeData;
  24. int m_iRecvTimeout;
  25. zhandle_t *m_zkHandle;
  26. struct String_vector m_path_info;
  27. volatile bool m_connect;
  28. volatile bool m_run;
  29. };

server.cpp

  1. #include "server.h"
  2. Server::Server(string path, string data, int timeout)
  3. {
  4. m_strNodePath = path;
  5. m_strNodeData = data;
  6. m_iRecvTimeout = timeout;
  7. m_run = false;
  8. m_connect = false;
  9. m_zkHandle = NULL;
  10. }
  11. Server::~Server()
  12. {
  13. if(NULL != m_zkHandle)
  14. {
  15. zookeeper_close(m_zkHandle);
  16. }
  17. }
  18. int Server::connect_zk(const char *host)
  19. {
  20. m_zkHandle = zookeeper_init(host, zk_watcher, m_iRecvTimeout, 0, this, 0);
  21. while(!m_connect)
  22. {
  23. sleep(1);
  24. }
  25. if (NULL == m_zkHandle)
  26. {
  27. cout << "zookeeper_init(): function return NULL!" << endl;
  28. return -1;
  29. }
  30. else
  31. {
  32. return 0;
  33. }
  34. }
  35. int Server::create_zk_nodes()
  36. {
  37. char buf[256] = {0};
  38. int errcode = zoo_create(m_zkHandle, m_strNodePath.c_str(),
  39. m_strNodeData.c_str(), static_cast<int>(m_strNodeData.size()),
  40. &ZOO_OPEN_ACL_UNSAFE, ZOO_EPHEMERAL|ZOO_SEQUENCE, buf, 256);
  41. if (errcode != ZOK)
  42. {
  43. cout << "create_zk_nodes():Failed to create zk node!" << endl;
  44. return -1;
  45. }
  46. else
  47. {
  48. m_strNodePath = string(buf);
  49. return 0;
  50. }
  51. }
  52. void Server::zk_watcher(zhandle_t *zh, int type, int state, const char *path,void *watcherCtx)
  53. {
  54. Server *pthis = (Server*)watcherCtx;
  55. if (type == ZOO_SESSION_EVENT)
  56. {
  57. if (state == ZOO_CONNECTED_STATE)
  58. {
  59. pthis->m_connect = true;
  60. cout << "Connected to zookeeper service successfully!" << endl;
  61. }
  62. }
  63. }
  64. void Server::start_work()
  65. {
  66. run_for_master();
  67. if (!m_run)
  68. {
  69. cout << "become slave" << endl;
  70. }
  71. int ret = zoo_wget_children(m_zkHandle, "/", node_watcher, this, &m_path_info);
  72. if (ret != 0)
  73. {
  74. cout << "get nodelist failed!" << endl;
  75. }
  76. while (!m_run)
  77. {
  78. sleep(1);
  79. }
  80. deallocate_String_vector(&m_path_info);
  81. cout << "become master" << endl;
  82. work();
  83. }
  84. void Server:: node_watcher(zhandle_t *zh, int type, int state, const char *path,void *watcherCtx)
  85. {
  86. sleep(3);
  87. Server *pthis = (Server*)watcherCtx;
  88. if (type == ZOO_CHILD_EVENT)
  89. {
  90. pthis->run_for_master();
  91. }
  92. }
  93. void Server::run_for_master()
  94. {
  95. int ret = zoo_wget_children(m_zkHandle, "/", node_watcher, this, &m_path_info);
  96. if (0 != ret)
  97. {
  98. cout << "node_watcher(): update path error!" << endl;
  99. }
  100. string pathstr;
  101. pathstr.assign(m_strNodePath, 1, 15);
  102. char *minstr = new char[16];
  103. char *p = minstr;
  104. strcpy(minstr,pathstr.c_str());
  105. for (int i=0, len=m_path_info.count; i<len; i++)
  106. {
  107. if (strcmp(minstr,*(m_path_info.data + i)) > 0
  108. && strcmp("master",*(m_path_info.data + i)) != 0)
  109. {
  110. minstr = *(m_path_info.data + i);
  111. }
  112. }
  113. if (strcmp(minstr,pathstr.c_str()) == 0
  114. && zoo_wexists(m_zkHandle, "/master", zk_watcher, NULL, NULL) == ZNONODE)
  115. {
  116. regist_master();
  117. }
  118. deallocate_String_vector(&m_path_info);
  119. delete []p;
  120. p = NULL;
  121. }
  122. void Server::regist_master()
  123. {
  124. int errorcode = zoo_create(m_zkHandle, "/master", NULL, 0,
  125. &ZOO_OPEN_ACL_UNSAFE, ZOO_EPHEMERAL, NULL, 0);
  126. if (ZOK == errorcode)
  127. {
  128. m_run = true;
  129. }
  130. else if (ZNODEEXISTS == errorcode)
  131. {
  132. cout << "regist_master():master is exist!" << endl;
  133. }
  134. else
  135. {
  136. cout << "regist_master():other error!" << endl;
  137. }
  138. }
  139. void Server:: work()
  140. {
  141. while (1)
  142. {
  143. cout << "hello" << endl;
  144. sleep(2);
  145. }
  146. }

main.cpp

  1. #include "server.h"
  2. int main(int argc, char *argv[])
  3. {
  4. zoo_set_debug_level( ZOO_LOG_LEVEL_ERROR );
  5. const char *host = "127.0.0.1:2181";
  6. string path = "/nodes";
  7. string data = "nothing";
  8. int timeout = 10000;
  9. Server server(path, data, timeout);
  10. if (-1 == server.connect_zk(host))
  11. {
  12. return -1;
  13. }
  14. if (-1 == server.create_zk_nodes())
  15. {
  16. return -1;
  17. }
  18. server.start_work();
  19. getchar();
  20. return 0;
  21. }

makefile

  1. FLAGS = -g -I/user/local/include/zookeeper
  2. CLIBS = -lzookeeper_mt -DTHREADED -lpthread
  3. result: server.o main.o
  4. g++ $(FLAGS) server.o main.o -o result $(CLIBS)
  5. main.o:main.cpp server.h
  6. g++ -g -c main.cpp
  7. server.o:server.cpp server.h
  8. g++ -g -c server.cpp $(CLIBS)
  9. clean:
  10. rm *.o

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/976345
推荐阅读
相关标签
  

闽ICP备14008679号