ZooKeeper Client API 安装及使用指北

下载

wget https://archive.apache.org/dist/zookeeper/zookeeper-3.5.4-beta/zookeeper-3.5.4-beta.tar.gz

解压

tar -zxf zookeeper-3.5.4-beta.tar.gz

安装

cd zookeeper-3.5.4-beta/src/c/
./configure
make
sudo make install

make 这一步大概率会出现报错:

User
make[1]: Entering directory '/root/zookeeper-3.5.4-beta/src/c'
/bin/bash ./libtool  --tag=CC   --mode=compile gcc -DHAVE_CONFIG_H -I.  -I./include -I./tests -I./generated   -Wall -Werror -Wdeclaration-after-statement -g -O2 -D_GNU_SOURCE -MT zookeeper.lo -MD -MP -MF .deps/zookeeper.Tpo -c -o zookeeper.lo `test -f 'src/zookeeper.c' || echo './'`src/zookeeper.c
libtool: compile:  gcc -DHAVE_CONFIG_H -I. -I./include -I./tests -I./generated -Wall -Werror -Wdeclaration-after-statement -g -O2 -D_GNU_SOURCE -MT zookeeper.lo -MD -MP -MF .deps/zookeeper.Tpo -c src/zookeeper.c  -fPIC -DPIC -o .libs/zookeeper.o
src/zookeeper.c: In function ‘print_completion_queue’:
src/zookeeper.c:2530:5: error: argument 1 null where non-null expected [-Werror=nonnull]
 2530 |     fprintf(LOGSTREAM,"Completion queue: ");
      |     ^~~~~~~
In file included from ./include/zookeeper.h:35,
                 from src/zookeeper.c:28:
/usr/include/stdio.h:312:12: note: in a call to function ‘fprintf’ declared ‘nonnull’
  312 | extern int fprintf (FILE *__restrict __stream,
      |            ^~~~~~~
src/zookeeper.c:2532:9: error: argument 1 null where non-null expected [-Werror=nonnull]
 2532 |         fprintf(LOGSTREAM,"empty\n");
      |         ^~~~~~~
/usr/include/stdio.h:312:12: note: in a call to function ‘fprintf’ declared ‘nonnull’
  312 | extern int fprintf (FILE *__restrict __stream,
      |            ^~~~~~~
src/zookeeper.c:2538:9: error: argument 1 null where non-null expected [-Werror=nonnull]
 2538 |         fprintf(LOGSTREAM,"%d,",cptr->xid);
      |         ^~~~~~~
/usr/include/stdio.h:312:12: note: in a call to function ‘fprintf’ declared ‘nonnull’
  312 | extern int fprintf (FILE *__restrict __stream,
      |            ^~~~~~~
src/zookeeper.c:2541:5: error: argument 1 null where non-null expected [-Werror=nonnull]
 2541 |     fprintf(LOGSTREAM,"end\n");
      |     ^~~~~~~
/usr/include/stdio.h:312:12: note: in a call to function ‘fprintf’ declared ‘nonnull’
  312 | extern int fprintf (FILE *__restrict __stream,
      |            ^~~~~~~
cc1: all warnings being treated as errors
Makefile:1025: recipe for target 'zookeeper.lo' failed
make[1]: *** [zookeeper.lo] Error 1
make[1]: Leaving directory '/root/zookeeper-3.5.4-beta/src/c'
Makefile:684: recipe for target 'all' failed
make: *** [all] Error 2

主要原因还是linux的版本问题,这边用的是ubuntu18+gcc13,网上找了一圈解决方法少之又少,基本是通过换centos解决,其实只需要将当前目录 Makefile 文件中 -Werror 选项去除即可,这样报错就会变成警告。

本地集群搭建

新建三个文件夹,将原先zookeeper-3.5.4-beta中的所有文件分别复制到三个文件夹中。
在这里插入图片描述
修改zoo.cfg中的datDirclientPort,同时添加集群的配置信息:
server.A = B:C:D
A是一个数字,代表是哪台机器,对应myid里面的值。
B是指这台服务器地址。
C是leader和follow之间进行交换信息的端口号。
D是当集群中的leader节点挂掉后,要进行重新选举leader,这个端口号就是用来执行选举的时候进行互相通信的端口号。
在这里插入图片描述
zkData文件夹中新建myid文件,标识是哪台机器,文件里填写数字即可。

通过bin文件夹下zkServer.sh start/stop/status管理服务端。zkCli.sh管理客户端。

什么是ZooKeeper?

Zookeeper实际上运行在Zab协议(ZooKeeper Atomic Broadcast)之上,Zab几乎与Raft是一样的。从Raft说起,Raft实际上就是一个库。你可以在一些更大的多副本系统中使用Raft库。但是Raft不是一个你可以直接交互的独立的服务,你必须要设计你自己的应用程序来与Raft库交互。所以这里有一个有趣的问题:是否有一些有用的,独立的,通用的系统可以帮助人们构建分布式系统?是否有这样的服务可以包装成一个任何人都可以使用的独立服务,并且极大的减轻构建分布式应用的痛苦?所以,对于一个通用的服务,API应该是怎样?Zookeeper可以被认为是一个通用的协调服务(General-Purpose Coordination Service)。

Zookeeper 允许客户端将读请求发送给任意副本,并由副本根据自己的状态来响应读请求。副本的 Log 可能并没有拥有最新的条目,所以尽管系统中可能有一些更新的数据,这个副本可能还是会返回旧的数据。

Zookeeper 的一致性保证:写请求是线性一致的、 FIFO 客户端序列(所有客户端发送的请求以一个特定的序列执行,通过 zxid 维护)。

使用场景

TODO

Client API

TODO

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值