zookeeper

文章目录

    • 简介
    • 下载
    • 解压
    • 修改配置
    • 启动
      • 方式一
      • 方式二
    • 伪集群搭建
      • 修改配置
        • zookeeper_1的zoo.cfg
        • zookeeper_2的zoo_2.cfg
        • zookeeper_3的zoo_3.cfg
      • 创建myid文件
      • 注意:
      • 启动伪集群
      • 启动过程:
      • zoo.cfg参数说明

简介

ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。

ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。

ZooKeeper包含一个简单的原语集,提供Java和C的接口。

下载

http://mirrors.hust.edu.cn/apache/zookeeper/

解压

解压到指定目录下 D:\tools\zookeeper\zookeeper-3.4.10
解压后如图:本文只关心bin目录和conf目录。

修改配置

  • 修改/conf下的:zoo_sample.cfg文件名,改为:zoo.cfg
  • 修改:zoo.cfg默认配置
dataDir=D:\\tools\\zookeeper\\data
dataLogDir=D:\\tools\\zookeeper\\data\\dataLog

启动

方式一

进入bin目录,双击:zkServer.cmd,即可。
看到如下画面,表示运行成功

方式二

也可以在cmd命令中连接一下
进入cmd下进bin目录,输入:zkCli.cmd 127.0.0.1:2181
zookeeper_第1张图片
这样就启动成功了

伪集群搭建

将单示例复制三份,分别命名为zookeeper_1、zookeeper_2、zookeeper_3

修改配置

  1. 分别修改zoo.cfg文件
  • 添加server.x配置
  • 修改dataDir
  • 修改clientPort

zookeeper_1的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=D:\\tools\\zookeeper\\data\\d_1
dataLogDir=D:\\tools\\zookeeper\\data\\dataLog
server.1=localhost:2888:3888
server.2=localhost:2888:3888
server.3=localhost:2888:3888
# 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

zookeeper_2的zoo_2.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=D:\\tools\\zookeeper\\data\\d_2
dataLogDir=D:\\tools\\zookeeper\\data\\dataLog
server.1=localhost:2888:3888
server.2=localhost:2888:3888
server.3=localhost:2888:3888
# the port at which the clients will connect
clientPort=2182
# 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

zookeeper_3的zoo_3.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=D:\\tools\\zookeeper\\data\\d_3
dataLogDir=D:\\tools\\zookeeper\\data\\dataLog
server.1=localhost:2888:3888
server.2=localhost:2888:3888
server.3=localhost:2888:3888
# the port at which the clients will connect
clientPort=2183
# 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

创建myid文件

分别在zoo.cfg配置的dataDir路径下创建myid文件输入本实例对应的server.x的x,这里的x只能是数字

注意:

同一IP上搭建多个节点的集群时,必须要注意端口问题,端口必须不一致才行;

创建多个节点集群时,在dataDir目录下必须创建myid文件,myid文件用于zookeeper验证server序号等,myid文件只有一行,并且为当前server的序号,例如server.1的myid就是1,server2的myid就是2等。

启动伪集群

分别cmd进入示例的bin目录下,运行各自的zkserver.cmd,zkserver.cmd,zkserver.cmd

启动过程:

启动顺序为server1、server2、server3。在启动server1,server2时zk会报错,当所有节点全部启动时错误会消失。

zoo.cfg参数说明

#   心跳检查的时间 2秒  
tickTime=2000  
# 初始化时 连接到服务器端的间隔次数,总时间10*2=20秒  
initLimit=10  
# ZK Leader 和follower 之间通讯的次数,总时间5*2=10秒   
syncLimit=5  
# 存储内存中数据库快照的位置,如果不设置参数,更新事务日志将被存储到默认位置。  
dataDir=F:\\zk\\tmp\\zookeeper  
# 错误日志的存放位置  
dataLogDir=F:\\zk\\logs\\zookeeper  
  
# ZK 服务器端的监听端口  
clientPort=2181

你可能感兴趣的:(分布式技术栈)