💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
第一种:独立模式 [root@distr01 conf]# cp zoo_sample.cfg zoo.cfg [root@distr01 conf]# vim zoo.cfg # The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/tmp/zookeeper # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval= 解释: tickTime:Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳,单位毫秒 initLimit:Zookeeper的Leader 接受客户端(Follower)初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过 5个心跳的时间(也就是tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 5*2000=10 秒 syncLimit:表示 Leader 与 Follower 之间发送消息时请求和应答时间长度,最长不能超过多少个tickTime 的时间长度,总的时间长度就是 2*2000=4 秒。 clientport: 客户端连接端口(server提供给客户端连接的) dataDir: 数据目录 dataLogDir: 日志目录 第二种:集群模式 除了以上参数外,还需要额外增加如下: server.n=host:port1:port 数字n必须是myid中的值 port1: leader端口,供follower连接的端口 port2: 选举端口 选举leader时供其他follower连接的端口 环境: 三台机器: A: 10.2.13.191 distr01.51yuki.cn B: 10.2.13.190 distr02.51yuki.cn C: 10.2.13.188 distr03.51yuki.cn 第一步:安装zookeeper脚本 ~~~ #!/bin/bash #decsription install zookeeper SOFTDIR=/usr/local/src ZKVER=3.4.9 DATADIR=/opt/data/zookeeper DATALOGDIR=/opt/log/zookeeper.log #create zk datadir [ ! -d $DATADIR ] && mkdir -p $DATADIR [ ! -d $DATALOGDIR ] && mkdir -p $DATALOGDIR #install java environment cd $SOFTDIR && [ ! -f jdk-8u131-linux-x64.rpm ] && wget http://soft.51yuki.cn/jdk-8u131-linux-x64.rpm && yum -y localinstall jdk-8u131-linux-x64.rpm #install zookeeper cd $SOFTDIR && [ ! -f zookeeper-${ZKVER}.tar.gz ] && wget http://soft.51yuki.cn/zookeeper-${ZKVER}.tar.gz cd /usr/local if [ ! -d zookeeper-${ZKVER} ];then cd $SOFTDIR && tar xf zookeeper-${ZKVER}.tar.gz -C /usr/local ln -s /usr/local/zookeeper-${ZKVER} /usr/local/zookeeper fi #configure zookeeper env variables echo -e "export ZOOKEEPER_HOME=/usr/local/zookeeper\n export PATH=${ZOOKEEPER_HOME}/bin:$PATH" > /etc/profile.d/zookeeper.sh source /etc/profile.d/zookeeper.sh #configure zookeeper cd /usr/local/zookeeper/conf cat >zoo.cfg <<EOF tickTime=2000 initLimit=10 syncLimit=5 dataDir=/opt/data/zookeeper dataLogDir=/opt/log/zookeeper clientPort=2181 #maxClientCnxns=60 #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1 EOF ~~~ 第二步:填写myid [root@distr01 conf]# cd /opt/data/zookeeper/ [root@distr01 zookeeper]# echo 01 > myid [root@distr02 ~]# cd /opt/data/zookeeper/ [root@distr02 zookeeper]# echo 02 > myid [root@distr03 ~]# cd /opt/data/zookeeper/ [root@distr03 zookeeper]# echo 03 > myid 第三步:配置zoo.cfg (三台机器一样) [root@distr01 conf]# cat zoo.cfg |grep -v ^# tickTime=2000 initLimit=10 syncLimit=5 dataDir=/opt/data/zookeeper dataLogDir=/opt/log/zookeeper.log clientPort=2181 server.01=distr01:2888:3888 server.02=distr02:2888:3888 server.03=distr03:2888:3888 [root@distr01 conf]# scp zoo.cfg root@10.2.13.190:/usr/local/zookeeper/conf/ root@10.2.13.190's password: zoo.cfg 100% 1046 1.0KB/s 00:00 [root@distr01 conf]# scp zoo.cfg root@10.2.13.188:/usr/local/zookeeper/conf/ root@10.2.13.188's password: zoo.cfg 第四步:启动zookeeper 服务器 distr01和distr02类似操作 [root@distr03 zookeeper]# vim /etc/profile.d/zookeeper.sh [root@distr03 zookeeper]# source /etc/profile.d/zookeeper.sh [root@distr03 zookeeper]# zkServer.sh start ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [root@distr03 zookeeper]# 查看: [root@distr03 zookeeper]# zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Mode: leader [root@distr02 ~]# zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Mode: follower [root@distr01 ~]# zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Mode: follower 模拟distr03机器故障 [root@distr03 ~]# zkServer.sh stop 这个时候distr01就变成leader咯 [root@distr01 ~]# zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Mode: leader 然后distr03修复后,然后上线,就是follower [root@distr03 ~]# zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Mode: follower 测试: zk四字符客户端指令 [root@huancun02 ~]# echo conf | nc 10.2.13.191 2181 clientPort=2181 dataDir=/opt/data/zookeeper/version-2 dataLogDir=/opt/log/zookeeper.log/version-2 tickTime=2000 maxClientCnxns=60 minSessionTimeout=4000 maxSessionTimeout=40000 serverId=1 initLimit=10 syncLimit=5 electionAlg=3 electionPort=3888 quorumPort=2888 peerType=0 [root@huancun02 ~]# echo ruok | nc 10.2.13.191 2181 imok [root@huancun02 ~]# echo envi | nc 10.2.13.191 2181 Environment: zookeeper.version=3.4.9-1757313, built on 08/23/2016 06:50 GMT host.name=distr01.51yuki.cn java.version=1.8.0_131 java.vendor=Oracle Corporation java.home=/usr/java/jdk1.8.0_131/jre java.class.path=/usr/local/zookeeper/bin/../build/classes:/usr/local/zookeeper/bin/../build/lib/*.jar:/usr/local/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper/bin/../lib/netty-3.10.5.Final.jar:/usr/local/zookeeper/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper/bin/../zookeeper-3.4.9.jar:/usr/local/zookeeper/bin/../src/java/lib/*.jar:/usr/local/zookeeper/bin/../conf: java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib java.io.tmpdir=/tmp java.compiler=<NA> os.name=Linux os.arch=amd64 os.version=3.10.0-514.el7.x86_64 user.name=root user.home=/root user.dir=/root [root@huancun02 ~]# echo cons |nc 10.2.13.191 2181 /10.2.13.197:54306[0](queued=0,recved=1,sent=0) [root@huancun02 ~]# echo reqs |nc 10.2.13.191 2181 [root@huancun02 ~]# echo stat |nc 10.2.13.191 2181 Zookeeper version: 3.4.9-1757313, built on 08/23/2016 06:50 GMT Clients: /10.2.13.197:54310[0](queued=0,recved=1,sent=0) Latency min/avg/max: 0/0/0 Received: 12 Sent: 11 Connections: 1 Outstanding: 0 Zxid: 0x200000000 Mode: leader Node count: 4 利用客户端zkCli.sh (客户端需要安装zookeeper不要启动服务,而使用客户端) [root@huancun02 ~]# zkCli.sh -server 10.2.13.191:2181 ~~~ WatchedEvent state:SyncConnected type:None path:null [zk: 10.2.13.191:2181(CONNECTED) 0] [zk: 10.2.13.191:2181(CONNECTED) 0] [zk: 10.2.13.191:2181(CONNECTED) 0] clear ZooKeeper -server host:port cmd args stat path [watch] set path data [version] ls path [watch] delquota [-n|-b] path ls2 path [watch] setAcl path acl setquota -n|-b val path history redo cmdno printwatches on|off delete path [version] sync path listquota path rmr path get path [watch] create [-s] [-e] path data acl addauth scheme auth quit getAcl path close connect host:port [zk: 10.2.13.191:2181(CONNECTED) 1] h ZooKeeper -server host:port cmd args stat path [watch] set path data [version] ls path [watch] delquota [-n|-b] path ls2 path [watch] setAcl path acl setquota -n|-b val path history redo cmdno printwatches on|off delete path [version] sync path listquota path rmr path get path [watch] create [-s] [-e] path data acl addauth scheme auth quit getAcl path close connect host:port [zk: 10.2.13.191:2181(CONNECTED) 2] ls / [zookeeper] [zk: 10.2.13.191:2181(CONNECTED) 3] cd /zookeeper ZooKeeper -server host:port cmd args stat path [watch] set path data [version] ls path [watch] delquota [-n|-b] path ls2 path [watch] setAcl path acl setquota -n|-b val path history redo cmdno printwatches on|off delete path [version] sync path listquota path rmr path get path [watch] create [-s] [-e] path data acl addauth scheme auth quit getAcl path close connect host:port [zk: 10.2.13.191:2181(CONNECTED) 4] ls /zookeeper [quota] [zk: 10.2.13.191:2181(CONNECTED) 5] ls /zookeeper/quota [] [zk: 10.2.13.191:2181(CONNECTED) 6] ll ZooKeeper -server host:port cmd args stat path [watch] set path data [version] ls path [watch] delquota [-n|-b] path ls2 path [watch] setAcl path acl setquota -n|-b val path history redo cmdno printwatches on|off delete path [version] sync path listquota path rmr path get path [watch] create [-s] [-e] path data acl addauth scheme auth quit getAcl path close connect host:port [zk: 10.2.13.191:2181(CONNECTED) 7] ls /zookeeper/quota [] [zk: 10.2.13.191:2181(CONNECTED) 8] create /mydata helloozk Created /mydata [zk: 10.2.13.191:2181(CONNECTED) 9] ls / [zookeeper, mydata] [zk: 10.2.13.191:2181(CONNECTED) 10] ls /mydata [] [zk: 10.2.13.191:2181(CONNECTED) 11] get /mydata helloozk cZxid = 0x200000002 ctime = Tue Feb 27 15:24:12 CST 2018 mZxid = 0x200000002 mtime = Tue Feb 27 15:24:12 CST 2018 pZxid = 0x200000002 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 8 numChildren = 0 [zk: 10.2.13.191:2181(CONNECTED) 12] set /mydata myworld cZxid = 0x200000002 ctime = Tue Feb 27 15:24:12 CST 2018 mZxid = 0x200000003 mtime = Tue Feb 27 15:25:49 CST 2018 pZxid = 0x200000002 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 0 [zk: 10.2.13.191:2181(CONNECTED) 13] create /mydata/distr01 distr01_data Created /mydata/distr01 [zk: 10.2.13.191:2181(CONNECTED) 14] create /mydata/distr02 distr02_data Created /mydata/distr02 [zk: 10.2.13.191:2181(CONNECTED) 15] create /mydata/distr03 distr03_data Created /mydata/distr03 [zk: 10.2.13.191:2181(CONNECTED) 16] ls /root Node does not exist: /root [zk: 10.2.13.191:2181(CONNECTED) 17] ls /mydata [distr02, distr03, distr01] [zk: 10.2.13.191:2181(CONNECTED) 18] get /mydata/distr0 distr02 distr03 distr01 [zk: 10.2.13.191:2181(CONNECTED) 18] get /mydata/distr01 distr01_data cZxid = 0x200000004 ctime = Tue Feb 27 15:27:00 CST 2018 mZxid = 0x200000004 mtime = Tue Feb 27 15:27:00 CST 2018 pZxid = 0x200000004 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 12 numChildren = 0 [zk: 10.2.13.191:2181(CONNECTED) 19] get /mydata/distr02 distr02_data cZxid = 0x200000005 ctime = Tue Feb 27 15:27:06 CST 2018 mZxid = 0x200000005 mtime = Tue Feb 27 15:27:06 CST 2018 pZxid = 0x200000005 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 12 numChildren = 0 [zk: 10.2.13.191:2181(CONNECTED) 20] get /mydata/distr03 distr03_data cZxid = 0x200000006 ctime = Tue Feb 27 15:27:11 CST 2018 mZxid = 0x200000006 mtime = Tue Feb 27 15:27:11 CST 2018 pZxid = 0x200000006 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 12 numChildren = 0 [zk: 10.2.13.191:2181(CONNECTED) 22] delete /mydata/distr0 distr02 distr03 distr01 [zk: 10.2.13.191:2181(CONNECTED) 22] delete /mydata/distr03 [zk: 10.2.13.191:2181(CONNECTED) 23] ls /mydata [distr02, distr01] ~~~ 客户端API调用 ![](https://box.kancloud.cn/0030b0e6edfd8f11a8903274dd69fb0a_814x535.png) 通过eclispe创建一个java项目,然后在该java项目上创建一个类库lib,然后把服务器上这些目录 zookeeper-3.4.9.jar jline-0.9.94.jar log4j-1.2.16.jar netty-3.10.5.Final.jar slf4j-api-1.6.1.jar slf4j-log4j12-1.6.1.jar 拷贝到Lib文件上 ,然后右击build ![](https://box.kancloud.cn/12bf14c75946cb5d2462fe3767741c6e_580x199.png) 第二种:伪集群案例(伪分布)