下载地址:http://apache.dataguru.cn/zookeeper
选择一个稳定版本进行下载,我这里下载的是zookeeper-3.4.6版本。
伪分布式集群:在一台Server中,启动多个ZooKeeper的实例。
cd /usr rz -by tar xf zookeeper-3.4.6.tar.gz
cd zookeeper-3.4.6/conf cp zoo_sample.cfg zoo1.cfg cp zoo_sample.cfg zoo2.cfg cp zoo_sample.cfg zoo3.cfg
实例1的配置:
vi zoo1.cfg tickTime=2000 initLimit=10 syncLimit=5 dataDir=/tmp/zookeeper/d_1 clientPort=2181 dataLogDir=/usr/zookeeper-3.4.6/logs_1 server.1=localhost:2887:3887 server.2=localhost:2888:3888 server.3=localhost:2889:3889
实例2的配置:
vi zoo2.cfg tickTime=2000 initLimit=10 syncLimit=5 dataDir=/tmp/zookeeper/d_2 clientPort=2182 dataLogDir=/usr/zookeeper-3.4.6/logs_2 server.1=localhost:2887:3887 server.2=localhost:2888:3888 server.3=localhost:2889:3889
实例3的配置:
vi zoo3.cfg tickTime=2000 initLimit=10 syncLimit=5 dataDir=/tmp/zookeeper/d_3 clientPort=2183 dataLogDir=/usr/zookeeper-3.4.6/logs_3 server.1=localhost:2887:3887 server.2=localhost:2888:3888 server.3=localhost:2889:3889
mkdir /tmp/zookeeper/d_1 mkdir /tmp/zookeeper/d_2 mkdir /tmp/zookeeper/d_3 mkdir /usr/zookeeper-3.4.6/logs_1 mkdir /usr/zookeeper-3.4.6/logs_2 mkdir /usr/zookeeper-3.4.6/logs_3 echo "1" > /tmp/zookeeper/d_1/myid echo "2" > /tmp/zookeeper/d_2/myid echo "3" > /tmp/zookeeper/d_3/myid
/usr/zookeeper-3.4.6/bin/zkServer.sh start zoo1.cfg /usr/zookeeper-3.4.6/bin/zkServer.sh start zoo2.cfg /usr/zookeeper-3.4.6/bin/zkServer.sh start zoo3.cfg
jps #看到类似下面的进程就表示3个实例均启动成功 13419 QuorumPeerMain 13460 QuorumPeerMain 13561 Jps 13392 QuorumPeerMain #如果未成功启动,可以到zookeeper.out文件中查看启动失败的日志信息。
[root@localhost ~]# /usr/zookeeper-3.4.6/bin/zkServer.sh status zoo1.cfg JMX enabled by default Using config: /usr/zookeeper-3.4.6/bin/../conf/zoo1.cfg Mode: follower [root@localhost ~]# /usr/zookeeper-3.4.6/bin/zkServer.sh status zoo2.cfg JMX enabled by default Using config: /usr/zookeeper-3.4.6/bin/../conf/zoo2.cfg Mode: leader [root@localhost ~]# /usr/zookeeper-3.4.6/bin/zkServer.sh status zoo3.cfg JMX enabled by default Using config: /usr/zookeeper-3.4.6/bin/../conf/zoo3.cfg Mode: follower #发现实例2为leader,实例1和实例3均为follower
至此,一个ZK的伪分布式集群搭建完毕。
需要引入zookeeper-3.4.6.jar和slf4j-api-1.61.jar这两个jar包。
package com.cjw.demo; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.ZooDefs.Ids; public class ZooKeeperClient { public static void main(String[] args) throws Exception { Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { System.out.println(event.toString()); } }; ZooKeeper zk = new ZooKeeper("192.168.157.22:2181", 3000, watcher); System.out.println("====创建节点"); zk.create("/cjw", "znode1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println("====查看节点是否安装成功"); System.out.println(new String(zk.getData("/cjw", false, null))); System.out.println("====修改节点的数据"); zk.setData("/cjw", "cjw2015".getBytes(), -1); System.out.println("====查看修改的节点是否成功"); System.out.println(new String(zk.getData("/cjw", false, null))); System.out.println("====删除节点"); zk.delete("/cjw", -1); System.out.println("====查看节点是否被删除"); System.out.println("节点状态:" + zk.exists("/cjw", false)); zk.close(); } }
运行结果:
====创建节点 WatchedEvent state:SyncConnected type:None path:null ====查看节点是否安装成功 znode1 ====修改节点的数据 ====查看修改的节点是否成功 cjw2015 ====删除节点 ====查看节点是否被删除 节点状态:null
initLimit: follower连接并同步到leader的初始化连接时间,它是通过tickTime的倍数表示。当初始化连接 时间超过设置的时间时,则连接失败。 syncLimit: follower和leader之间发送消息时请求和应答的时间长度,如果follower在设置的时间范围内 不能和leader通信,那么该follower将被丢弃,它也是按tickTime的倍数进行配置的。 tickTime: 定义心跳的时间间隔, 注:zk的client和server之间也有和web开发类似的session的概念,而zk里最小的session过期时间 就是tickTime的两倍 dataDir: 存储在内存中数据库快照功能。 clientPort: 监听客户端连接的端口号 dataLogDir: zk运行的相关日志写入目录,设定了配置,那么dataLog里日志的目录将无效,专门的日志存放 路径,对zk的性能和稳定性有好处。