赞
踩
zookeeper,是一个分布式服务框架,是Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务、状态同步服务、集群管理、分布式应用配置项的管理等。
每个子目录项如 NameService 都被称作为 znode(目录节点),和文件系统一样,我们能够自由的增加、删除znode,在一个znode下增加、删除子znode,唯一的不同在于znode是可以存储数据的。
有四种类型的znode:
客户端与zookeeper断开连接后,该节点依旧存在
客户端与zookeeper断开连接后,该节点依旧存在,只是Zookeeper给该节点名称进行顺序编号
客户端与zookeeper断开连接后,该节点被删除
客户端与zookeeper断开连接后,该节点被删除,只是Zookeeper给该节点名称进行顺序编号
下载地址
- https://downloads.apache.org/zookeeper/zookeeper-3.5.8/
- wget https://downloads.apache.org/zookeeper/zookeeper-3.5.8/apache-zookeeper-3.5.8-bin.tar.gz
cfg.zoo配置
- # 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=/home/hadoop/soft/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=1
- server.1=192.168.153.131:28888:3888
- server.2=192.168.153.134:28888:3888
- server.3=192.168.153.135:28888:3888
配置参数说明
参数 | 参数说明 |
tickTime | Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,每个 tickTime 时间就会发送一个心跳 |
initLimit | 配置 Zookeeper 接受客户端(这里所说的客户端不是用户连接 Zookeeper 服务器的客户端,而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数,当已经超过 10个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 10*2000=20 秒 |
syncLimit | 标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 5*2000=10秒 |
dataDir | Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里 |
clientPort | 客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求 |
server.A=B:C:D | 其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号 |
标识Server ID
- [hadoop@master conf]$ mkdir /home/hadoop/soft/zookeeper
- [hadoop@master conf]$ cd /home/hadoop/soft/zookeeper
- [hadoop@master zookeeper]$ echo 1 >myid
- [hadoop@master zookeeper]$ cat myid
- 1
复制zookeeper文件到其他服务器
- scp -r /home/hadoop/soft/apache-zookeeper-3.5.8 slave1:/home/hadoop/soft
- scp -r /home/hadoop/soft/apache-zookeeper-3.5.8 slave2:/home/hadoop/soft
修改其他服务器的Server ID
- [hadoop@slave1 ~]$ mkdir /home/hadoop/soft/zookeeper
- [hadoop@slave1 ~]$ echo 2 >myid
- [hadoop@slave1 ~]$ cat myid
-
- [hadoop@slave2 ~]$ mkdir /home/hadoop/soft/zookeeper
- [hadoop@slave2 ~]$ echo 3 >myid
启动
- [hadoop@master apache-zookeeper-3.5.8]$ ./bin/zkServer.sh start
- ZooKeeper JMX enabled by default
- Using config: /home/hadoop/soft/apache-zookeeper-3.5.8/bin/../conf/zoo.cfg
- Starting zookeeper ... STARTED
- [hadoop@master apache-zookeeper-3.5.8]$ pwd
- /home/hadoop/soft/apache-zookeeper-3.5.8
- [hadoop@master apache-zookeeper-3.5.8]$ ./bin/zkServer.sh status
- ZooKeeper JMX enabled by default
- Using config: /home/hadoop/soft/apache-zookeeper-3.5.8/bin/../conf/zoo.cfg
- Client port found: 2181. Client address: localhost.
- Mode: follower
-
-
- [hadoop@slave2 apache-zookeeper-3.5.8]$ ./bin/zkServer.sh start
- ZooKeeper JMX enabled by default
- Using config: /home/hadoop/soft/apache-zookeeper-3.5.8/bin/../conf/zoo.cfg
- Starting zookeeper ... STARTED
- [hadoop@slave2 apache-zookeeper-3.5.8]$ ./bin/zkServer.sh status
- ZooKeeper JMX enabled by default
- Using config: /home/hadoop/soft/apache-zookeeper-3.5.8/bin/../conf/zoo.cfg
- Client port found: 2181. Client address: localhost.
- Mode: leader
-
- [hadoop@slave1 apache-zookeeper-3.5.8]$ ./bin/zkServer.sh start
- ZooKeeper JMX enabled by default
- Using config: /home/hadoop/soft/apache-zookeeper-3.5.8/bin/../conf/zoo.cfg
- Starting zookeeper ... STARTED
- [hadoop@slave1 apache-zookeeper-3.5.8]$ ./bin/zkServer.sh status
- ZooKeeper JMX enabled by default
- Using config: /home/hadoop/soft/apache-zookeeper-3.5.8/bin/../conf/zoo.cfg
- Client port found: 2181. Client address: localhost.
- Mode: follower
查看进程
- [hadoop@slave1 apache-zookeeper-3.5.8]$ jps
- 1700 QuorumPeerMain
- 1782 ZooKeeperMain
- 1885 Jps
-
- [hadoop@slave2 apache-zookeeper-3.5.8]$ jps
- 1562 Jps
- 1374 QuorumPeerMain
-
- [hadoop@master apache-zookeeper-3.5.8]$ jps
- 1586 QuorumPeerMain
- 1764 Jps
查看zookeeper目录
- [hadoop@slave1 apache-zookeeper-3.5.8]$ ./bin/zkCli.sh
- [zk: localhost:2181(CONNECTED) 0] ls /
- [zookeeper]
- [zk: localhost:2181(CONNECTED) 1] create /zmw
- Created /zmw
- [zk: localhost:2181(CONNECTED) 2] ls /
- [zmw, zookeeper]
- [zk: localhost:2181(CONNECTED) 3] create /test testData
- Created /test
- [zk: localhost:2181(CONNECTED) 4] ls /
- [test, zmw, zookeeper]
- [zk: localhost:2181(CONNECTED) 5] get /test
- testData
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。