ZooKeeper学习

发布时间 2023-04-19 16:39:45作者: Ari的小跟班

参考尚硅谷07_尚硅谷_zk_本地_安装_哔哩哔哩_bilibili,感谢尚硅谷!

安装

​ 以Cent OS 7(CentOS-7-x86_64-DVD-2009)和ZooKeeper-3.5.7(https://wwrj.lanzoue.com/i7fqz0tfeula 密码是dqp9)为例。

​ 我以集群部署为例,目前有三台主机,分别为hadoop1、hadoop2和hadoop3,他们的ip地址分别为(192.168.117.128,192.168.117.129,192.168.117.130)。

  1. 安装JDK(各种方式都可以),我这里采用yum安装

    使用yum命令yum install -y java-1.8.0-openjdk.x86_64.

  2. 将ZooKeeper-3.5.7压缩包拷贝到/opt/software

  3. 使用解压缩命令,将这个压缩包解压到/opt/module下,tar -zxvf apache-zookeeper-3.5.7-bin.tar.gz -C /opt/module

  4. 因为原ZooKeeper文件名过长 ,所以进行重命名操作mv apache-zookeeper-3.5.7-bin/ zookeeper-3.5.7,此步骤也可以省略。

  5. 进入zookeeper-3.5.7文件夹,新建一个zkData目录,用于存储ZooKeeper的信息,即/opt/module/zookeeper-3.5.7/zkData.

    1. 在该目录下创建一个文件myid,随后向myid写入编号,该编号唯一标识集群中的ZooKeeper,这里,hadoop1,2,3的myid分别为1,2,3
  6. 进入/opt/module.zookeeper-3.5.7/conf目录下,使用命令mv zoo_sample.cfg zoo.cfg,将sample的配置文件改成正式的

  7. 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=/opt/module/zookeeper-3.5.7/zkData
    # 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.117.128:2888:3888
    server.2=192.168.117.129:2888:3888
    server.3=192.168.117.130:2888:3888
    
  8. 其中server.1标识的就是myid为1的ZooKeeper服务器的地址和具体端口。

​ 三台服务器的配置都是一样的,除了/opt/module/zookeeper-3.5.7/zkData文件夹下myid文件的内容不一致。例如hadoop1服务器下的myid是:

启动集群

​ 分别到hadoop1,2,3中,ZooKeeper的bin目录下,使用zkServer.sh start即可。