环境准备
-
zookeeper
安装包:http://www-eu.apache.org/dist/zookeeper/stable/zookeeper-3.4.12.tar.gz -
centos + java
运行环境,这里我分别准备三台centos虚拟机192.168.146.151,192.168.146.152,192.168.146.153
安装配置
-
解压zookeeper安装包,
tar -zxvf zookeeper-3.4.12.tar.gz
,解压后目录 -
添加zookeeper配置,进入conf目录,使用
cp zoo_sample.cfg zoo.cfg
复制zoo_sample.cfg
为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=/tmp/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.146.151:2188:3181 server.2=192.168.146.152:2188:3181 server.3=192.168.146.153:2188:3181
主要配置说明
clientPort
运行端口号dataDir
数据存储位置myid设置
当安装zookeeper集群环境时要在
dataDir
目录下创建当前zookeeper的myid
如我在192.168.146.151机器上创建在/tmp/zookeeper/myid文件,文件内容为1,依次类推
。添加server集群配置
server.1=192.168.146.151:2188:3181
-->server.myid=ip:clientPort:prot,有几台机器添加几条配置 ,注意两个端口号不可以重复(如果重复会出现端口号重复的问题),如上配置。 -
启动运行,进入每台机器zookeeper目录中的bin文件夹,运行
zkServer.sh
脚本,如sh zkServer.sh start
,运行时可在启动目录下查看zookeeper.out
控制台日志。 -
启动好了可通过运行bin目录下的
zkCli.sh
脚本连接zookeeper,如也可通过
sh zkServer.sh status
查看当前zookeeper是什么角色
注:zookeeper 集群角色分为leader、follower、observer,这里我配置的只有leader和follower,要台添加一台observer机器,在server配置后面标记为observer
如:server.4=192.168.146.154:2188:3181:observer
我的博客地址