当前位置:   article > 正文

zookeeper总结

zookeeper总结

​​​​​​zookeeper,是一个分布式服务框架,是Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务、状态同步服务、集群管理、分布式应用配置项的管理等。

每个子目录项如 NameService 都被称作为 znode(目录节点),和文件系统一样,我们能够自由的增加、删除znode,在一个znode下增加、删除子znode,唯一的不同在于znode是可以存储数据的。

有四种类型的znode:

  • PERSISTENT-持久化目录节点

       客户端与zookeeper断开连接后,该节点依旧存在

  • PERSISTENT_SEQUENTIAL-持久化顺序编号目录节点

       客户端与zookeeper断开连接后,该节点依旧存在,只是Zookeeper给该节点名称进行顺序编号

  • EPHEMERAL-临时目录节点

       客户端与zookeeper断开连接后,该节点被删除

  • EPHEMERAL_SEQUENTIAL-临时顺序编号目录节点

       客户端与zookeeper断开连接后,该节点被删除,只是Zookeeper给该节点名称进行顺序编号

下载地址

  1. https://downloads.apache.org/zookeeper/zookeeper-3.5.8/
  2. wget https://downloads.apache.org/zookeeper/zookeeper-3.5.8/apache-zookeeper-3.5.8-bin.tar.gz

cfg.zoo配置

  1. # The number of milliseconds of each tick
  2. tickTime=2000
  3. # The number of ticks that the initial
  4. # synchronization phase can take
  5. initLimit=10
  6. # The number of ticks that can pass between
  7. # sending a request and getting an acknowledgement
  8. syncLimit=5
  9. # the directory where the snapshot is stored.
  10. # do not use /tmp for storage, /tmp here is just
  11. # example sakes.
  12. dataDir=/home/hadoop/soft/zookeeper
  13. # the port at which the clients will connect
  14. clientPort=2181
  15. # the maximum number of client connections.
  16. # increase this if you need to handle more clients
  17. #maxClientCnxns=60
  18. #
  19. # Be sure to read the maintenance section of the
  20. # administrator guide before turning on autopurge.
  21. #
  22. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
  23. #
  24. # The number of snapshots to retain in dataDir
  25. #autopurge.snapRetainCount=3
  26. # Purge task interval in hours
  27. # Set to "0" to disable auto purge feature
  28. #autopurge.purgeInterval=1
  29. server.1=192.168.153.131:28888:3888
  30. server.2=192.168.153.134:28888:3888
  31. server.3=192.168.153.135:28888:3888

配置参数说明

参数参数说明
tickTimeZookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,每个 tickTime 时间就会发送一个心跳
initLimit配置 Zookeeper 接受客户端(这里所说的客户端不是用户连接 Zookeeper 服务器的客户端,而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数,当已经超过 10个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 10*2000=20 秒
syncLimit标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 5*2000=10秒
dataDirZookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里
clientPort客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求
server.A=B:C:D其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号

标识Server ID

  1. [hadoop@master conf]$ mkdir /home/hadoop/soft/zookeeper
  2. [hadoop@master conf]$ cd /home/hadoop/soft/zookeeper
  3. [hadoop@master zookeeper]$ echo 1 >myid
  4. [hadoop@master zookeeper]$ cat myid
  5. 1

复制zookeeper文件到其他服务器

  1. scp -r /home/hadoop/soft/apache-zookeeper-3.5.8 slave1:/home/hadoop/soft
  2. scp -r /home/hadoop/soft/apache-zookeeper-3.5.8 slave2:/home/hadoop/soft

修改其他服务器的Server ID

  1. [hadoop@slave1 ~]$ mkdir /home/hadoop/soft/zookeeper
  2. [hadoop@slave1 ~]$ echo 2 >myid
  3. [hadoop@slave1 ~]$ cat myid
  4. [hadoop@slave2 ~]$ mkdir /home/hadoop/soft/zookeeper
  5. [hadoop@slave2 ~]$ echo 3 >myid

启动

  1. [hadoop@master apache-zookeeper-3.5.8]$ ./bin/zkServer.sh start
  2. ZooKeeper JMX enabled by default
  3. Using config: /home/hadoop/soft/apache-zookeeper-3.5.8/bin/../conf/zoo.cfg
  4. Starting zookeeper ... STARTED
  5. [hadoop@master apache-zookeeper-3.5.8]$ pwd
  6. /home/hadoop/soft/apache-zookeeper-3.5.8
  7. [hadoop@master apache-zookeeper-3.5.8]$ ./bin/zkServer.sh status
  8. ZooKeeper JMX enabled by default
  9. Using config: /home/hadoop/soft/apache-zookeeper-3.5.8/bin/../conf/zoo.cfg
  10. Client port found: 2181. Client address: localhost.
  11. Mode: follower
  12. [hadoop@slave2 apache-zookeeper-3.5.8]$ ./bin/zkServer.sh start
  13. ZooKeeper JMX enabled by default
  14. Using config: /home/hadoop/soft/apache-zookeeper-3.5.8/bin/../conf/zoo.cfg
  15. Starting zookeeper ... STARTED
  16. [hadoop@slave2 apache-zookeeper-3.5.8]$ ./bin/zkServer.sh status
  17. ZooKeeper JMX enabled by default
  18. Using config: /home/hadoop/soft/apache-zookeeper-3.5.8/bin/../conf/zoo.cfg
  19. Client port found: 2181. Client address: localhost.
  20. Mode: leader
  21. [hadoop@slave1 apache-zookeeper-3.5.8]$ ./bin/zkServer.sh start
  22. ZooKeeper JMX enabled by default
  23. Using config: /home/hadoop/soft/apache-zookeeper-3.5.8/bin/../conf/zoo.cfg
  24. Starting zookeeper ... STARTED
  25. [hadoop@slave1 apache-zookeeper-3.5.8]$ ./bin/zkServer.sh status
  26. ZooKeeper JMX enabled by default
  27. Using config: /home/hadoop/soft/apache-zookeeper-3.5.8/bin/../conf/zoo.cfg
  28. Client port found: 2181. Client address: localhost.
  29. Mode: follower

查看进程

  1. [hadoop@slave1 apache-zookeeper-3.5.8]$ jps
  2. 1700 QuorumPeerMain
  3. 1782 ZooKeeperMain
  4. 1885 Jps
  5. [hadoop@slave2 apache-zookeeper-3.5.8]$ jps
  6. 1562 Jps
  7. 1374 QuorumPeerMain
  8. [hadoop@master apache-zookeeper-3.5.8]$ jps
  9. 1586 QuorumPeerMain
  10. 1764 Jps

查看zookeeper目录

  1. [hadoop@slave1 apache-zookeeper-3.5.8]$ ./bin/zkCli.sh
  2. [zk: localhost:2181(CONNECTED) 0] ls /
  3. [zookeeper]
  4. [zk: localhost:2181(CONNECTED) 1] create /zmw
  5. Created /zmw
  6. [zk: localhost:2181(CONNECTED) 2] ls /
  7. [zmw, zookeeper]
  8. [zk: localhost:2181(CONNECTED) 3] create /test testData
  9. Created /test
  10. [zk: localhost:2181(CONNECTED) 4] ls /
  11. [test, zmw, zookeeper]
  12. [zk: localhost:2181(CONNECTED) 5] get /test
  13. testData

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/603062
推荐阅读
相关标签
  

闽ICP备14008679号