CentOS 7 基于C 连接ZooKeeper 客户端

前提条件:CentOS 7 编译ZooKeeper 客户端,请参考:CentOS 7 编译ZooKeeper 客户端

1、Docker 安装ZooKeeper

复制代码
# docker 获取zookeeper 最新版本
docker pull zookeeper

# docker 容器包含镜像查看
docker iamges

# 准备zookeeper 镜像文件挂载对应文件目录
将它部署在 /usr/local/zookeeper 目录下:
cd /usr/local && mkdir zookeeper && cd zookeeper

创建data目录,用于挂载容器中的数据目录:
mkdir data

# docker 运行zookeeper
docker run -d -e TZ="Asia/Shanghai" -p 2181:2181 -v $PWD/data:/data --name zookeeper --restart always zookeeper

# 运行参数说明
-e TZ="Asia/Shanghai" # 指定上海时区 
-d # 表示在一直在后台运行容器
-p 2181:2181 # 对端口进行映射,将本地2181端口映射到容器内部的2181端口
--name # 设置创建的容器名称
-v # 将本地目录(文件)挂载到容器指定目录;
--restart always #始终重新启动zookeeper

# docker 查看zookeeper 运行状态
docker ps

2、CentOS 7 基于C 连接ZooKeeper Demo

在 /usr/local/source_code/zookeeper_demo/ 目录下,新增zookeeper_demo.c 文件,内容如下:

复制代码
[root@localhost source_code]# cd zookeeper_demo/
[root@localhost zookeeper_demo]# ll
总用量 0
[root@localhost zookeeper_demo]# vi zookeeper_demo.c
[root@localhost zookeeper_demo]# ll
总用量 4
-rw-r--r--. 1 root root 575 10月 10 12:46 zookeeper_demo.c

#include <zookeeper/zookeeper.h>

   int main() {
      zhandle_t *zh;
      char buffer[512];
      int bufferlen=sizeof(buffer);

      // 初始化zookeeper客户端
      zh = zookeeper_init("localhost:2181", NULL, 30000, 0, 0, 0);

      if (zh == NULL) {
          printf("zookeeper 连接失败! \n");
      }else {
         printf("zookeeper 连接成功! \n");
        }

      // 获取节点数据
      int ret = zoo_get(zh, "/test", 0, buffer, &bufferlen, NULL);

      if (ret != ZOK) {
         // 处理错误
         printf("zookeeper 获取/test 节点数据异常! \n");
      }

      // do something with the node data

      // 关闭zookeeper客户端
      zookeeper_close(zh);

      return 0;
   }
  • 编译代码的时候需要加链接的库及库的路径,那么编译命令如下

    gcc zookeeper_demo.c -o zookeeper_demo -L/usr/local/lib/ -lzookeeper_st

在执行的时候如果出现动态库无法加载,请进行如下配置。

复制代码
在 /etc/ld.so.conf.d/ 目录下新建文件 usr-libs.conf ,内容是: /usr/local/lib

vim /etc/ld.so.conf.d/usr-libs.conf
  • 然后使用命令 /sbin/ldconfig 更新一下配置即可。

    sbin/ldconfig

知识拓展:Linux 编译zookeeper 默认填充库文件地址和头文件地址

zookeeper 库文件地址:在/usr/local/lib目录下的libzookeeper_mt(集群模式)/libzookeeper_st(单列模式)

复制代码
[root@localhost zookeeper_demo]# cd /usr/local/lib
[root@localhost lib]# ll
总用量 6820
-rw-r--r--. 1 root root  532172 10月 10 09:44 libhiredis.a
lrwxrwxrwx. 1 root root      19 10月 10 09:44 libhiredis.so -> libhiredis.so.1.1.0
lrwxrwxrwx. 1 root root      19 10月 10 09:44 libhiredis.so.1 -> libhiredis.so.1.1.0
-rwxr-xr-x. 1 root root  318840 10月 10 09:44 libhiredis.so.1.1.0
-rw-r--r--. 1 root root 2262492 9月   4 15:28 libjpeg.a
-rwxr-xr-x. 1 root root     918 9月   4 15:28 libjpeg.la
lrwxrwxrwx. 1 root root      16 9月   4 15:28 libjpeg.so -> libjpeg.so.9.5.0
lrwxrwxrwx. 1 root root      16 9月   4 15:28 libjpeg.so.9 -> libjpeg.so.9.5.0
-rwxr-xr-x. 1 root root 1237096 9月   4 15:28 libjpeg.so.9.5.0
-rw-r--r--. 1 root root  883484 8月  31 16:56 libzookeeper_mt.a
-rwxr-xr-x. 1 root root     987 8月  31 16:56 libzookeeper_mt.la
lrwxrwxrwx. 1 root root      24 8月  31 16:56 libzookeeper_mt.so -> libzookeeper_mt.so.2.0.0
lrwxrwxrwx. 1 root root      24 8月  31 16:56 libzookeeper_mt.so.2 -> libzookeeper_mt.so.2.0.0
-rwxr-xr-x. 1 root root  453944 8月  31 16:56 libzookeeper_mt.so.2.0.0
-rw-r--r--. 1 root root  835950 8月  31 16:56 libzookeeper_st.a
-rwxr-xr-x. 1 root root     977 8月  31 16:56 libzookeeper_st.la
lrwxrwxrwx. 1 root root      24 8月  31 16:56 libzookeeper_st.so -> libzookeeper_st.so.2.0.0
lrwxrwxrwx. 1 root root      24 8月  31 16:56 libzookeeper_st.so.2 -> libzookeeper_st.so.2.0.0
-rwxr-xr-x. 1 root root  433840 8月  31 16:56 libzookeeper_st.so.2.0.0
drwxr-xr-x. 2 root root      42 10月 10 09:44 pkgconfig

zookeeper 头文件地址:在/usr/local/include目录下的zookeeper目录中。

复制代码
[root@localhost zookeeper_demo]# cd /usr/local/include/
[root@localhost include]# ll
总用量 88
drwxr-xr-x. 3 root root   116 10月 10 09:44 hiredis
-rw-r--r--. 1 root root  3301 9月   4 15:28 jconfig.h
-rw-r--r--. 1 root root 14588 9月   4 15:28 jerror.h
-rw-r--r--. 1 root root 14925 9月   4 15:28 jmorecfg.h
-rw-r--r--. 1 root root 49408 9月   4 15:28 jpeglib.h
drwxr-xr-x. 2 root root   132 8月  31 16:56 zookeeper
[root@localhost include]# cd zookeeper/
[root@localhost zookeeper]# ll
总用量 108
-rw-r--r--. 1 root root  1361 8月  31 16:56 proto.h
-rw-r--r--. 1 root root  3077 8月  31 16:56 recordio.h
-rw-r--r--. 1 root root 72869 8月  31 16:56 zookeeper.h
-rw-r--r--. 1 root root 20328 8月  31 16:56 zookeeper.jute.h
-rw-r--r--. 1 root root  1747 8月  31 16:56 zookeeper_log.h
-rw-r--r--. 1 root root  1055 8月  31 16:56 zookeeper_version.h

3、ZooKeeper 核心方法

  • 查看ZooKeeper.h 头文件涉及核心方法

    [root@localhost zookeeper]# pwd
    /usr/local/include/zookeeper
    [root@localhost zookeeper]# cat zookeeper.h


个人认为的核心方法如下:

  1. create():此方法由客户端使用来创建新的znode。它需要znode的路径和数据作为参数。

  2. exists():这个方法由客户端用来检查特定的znode是否存在。它需要znode的路径作为参数。

  3. getData():此方法用于从特定的znode获取数据。它需要znode路径作为参数。

  4. setData():这个方法用于设置特定znode的数据。它需要znode路径作为参数。

  5. getChildren():此方法用于得到特定znode的所有子节点。它需要znode路径作为参数。

  6. delete():这个方法允许客户端删除一个特定的znode。它需要znode的路径作为参数。

  7. sync():此方法用于同步znode的状态到当前客户端。

  8. close():此方法用于关闭客户端与ZooKeeper服务的连接。

  9. addAuthInfo():在此ZooKeeper会话上添加授权信息。

  10. getState():返回ZooKeeper客户端的状态。

以上这些方法的目的是让客户端能够在ZooKeeper的znode树中浏览、读写数据,监控znode状态的改变等等。

备注:ZooKeeper API中的所有读方法get*在成功返回时都会提供一个Stat对象。Stat对象包含了关于znode的元数据,比如它的czxid、mzxid、pzxid、version等。

相关推荐
lang20150928几秒前
Linux命令行:cat、more、less终极指南
linux·chrome·less
攒钱植发1 小时前
嵌入式Linux——“大扳手”与“小螺丝”:为什么不该用信号量(Semaphore)去模拟“完成量”(Completion)
linux·服务器·c语言
三五度1 小时前
vmware的ubuntu20.04无网络图标
linux·ubuntu
菜鸟祥哥2 小时前
xfs文件系统磁盘损坏修复
linux
Y淑滢潇潇2 小时前
RHCE Day2 时间管理服务器 NFS服务器
linux·运维·服务器
铭哥的编程日记2 小时前
【Linux网络】五种IO模型与非阻塞IO
linux·服务器·网络·tcp/ip·udp
liu****3 小时前
12.线程同步和生产消费模型
linux·服务器·开发语言·c++·1024程序员节
snakecy3 小时前
常用命令记录
linux·运维·github
cccyi73 小时前
Linux Socket 编程全解析:UDP 与 TCP 实现及应用
linux·tcp socket·udp socket
小苏兮4 小时前
【把Linux“聊”明白】自动化构建-make/Makefile详解
linux·服务器·学习·自动化·1024程序员节