zookeeper学习之集群环境搭建

一、安装环境

zookeeper:3.4.6

JDK:1.8

linux:centos6.5  64位

主机:

server0:192.168.0.224
server1:192.168.0.225
server2:192.168.0.226

二、安装

1、将ZK解压到/usr/local/zookeeper下
2、修改配置文件(配置环境变量) /etc/profile
JAVA_HOME=/usr/local/jdk1.8 ZOOKEEPER_HOME=/usr/local/zookeeper PATH=$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin:$PATH CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME ZOOKEEPER_HOME PATH CLASSPATH
3、source /etc/profile 让配置生效
4、修改zk的配置文件名 /usr/local/zookeeper/conf/zoo_sample.cfg [root@rocketmq-nameserver1 conf]# mv zoo_sample.cfg zoo.cfg
5、修改配置 zoo.cfg #存放数据的路径 dataDir=/usr/local/zookeeper/data
6、在/usr/local/zookeeper 下 创建 myid文件 server0:192.168.0.224 myid=0 server1:192.168.0.225 myid=1 server2:192.168.0.226 myid=2
7、zoo.cfg 配置集群节点 在该文件末尾添加 server.0=192.168.0.224:2888:3888 server.1=192.168.0.225:2888:3888 server.2=192.168.0.226:2888:3888 8、zk的启动 对于配置环境变量的启动 : zkServer.sh start 没有配置环境变量 可以在 zk 的bin 目录中启动 [root@rocketmq-nameserver1 bin]# ./zkServer.sh 启动这三台服务器

1、启动异常:

[root@localhost bin]# ./zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.

如果遇到该情况试着开启2181端口(或者关闭防火墙)

 /sbin/iptables -I INPUT -p tcp --dport 2181 -j ACCEPT   写入修改
 /etc/init.d/iptables save   保存修改
 service iptables restart    重启防火墙,修改生效

service iptables stop 关闭防火墙

 2、查看是否启动成功:三个服务器分别查看

[root@localhost bin]# ./zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower

三、配置文件

# The number of milliseconds of each tick
#tickTime这个时间是作为zookeeper服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是说每个tickTime时间就会发送一个心跳。
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take

#initLimit这个配置项是用来配置zookeeper接受客户端(这里所说的客户端不是用户连接zookeeper服务器的客户端,而是zookeeper服务器集群中连接到leader的follower 服务器)
初始化连接时最长能忍受多少个心跳时间间隔数。 #当已经超过10个心跳的时间(也就是tickTime)长度后 zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 10*2000=20秒。 initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement #syncLimit这个配置项标识leader与follower之间发送消息,请求和应答时间长度,最长不能超过多少个tickTime的时间长度,总的时间长度就是5*2000=10秒。 syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. #dataDir顾名思义就是zookeeper保存数据的目录,默认情况下zookeeper将写数据的日志文件也保存在这个目录里; dataDir=/usr/local/zookeeper/data # the port at which the clients will connect #clientPort这个端口就是客户端连接Zookeeper服务器的端口,Zookeeper会监听这个端口接受客户端的访问请求; 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.A=B:C:D中的A是一个数字,表示这个是第几号服务器,B是这个服务器的IP地址,C第一个端口用来集群成员的信息交换,表示这个服务器与集群中的leader服务器交换信息的端口,
D是在leader挂掉时专门用来进行选举leader所用的端口。 server.0=192.168.0.224:2888:3888 server.1=192.168.0.225:2888:3888 server.2=192.168.0.226:2888:3888

日志文件配置:cat /usr/local/zookeeper/bin/zkEnv.sh 

ZOOBINDIR="${ZOOBINDIR:-/usr/bin}"
ZOOKEEPER_PREFIX="${ZOOBINDIR}/.."

if [ "x$ZOOCFGDIR" = "x" ]
then
  if [ -e "${ZOOKEEPER_PREFIX}/conf" ]; then
    ZOOCFGDIR="$ZOOBINDIR/../conf"
  else
    ZOOCFGDIR="$ZOOBINDIR/../etc/zookeeper"
  fi
fi

if [ -f "${ZOOCFGDIR}/zookeeper-env.sh" ]; then
  . "${ZOOCFGDIR}/zookeeper-env.sh"
fi

if [ "x$ZOOCFG" = "x" ]
then
    ZOOCFG="zoo.cfg"
fi

ZOOCFG="$ZOOCFGDIR/$ZOOCFG"

if [ -f "$ZOOCFGDIR/java.env" ]
then
    . "$ZOOCFGDIR/java.env"
fi

if [ "x${ZOO_LOG_DIR}" = "x" ]
then
    ZOO_LOG_DIR="/usr/local/zookeeper/logs"
fi

if [ "x${ZOO_LOG4J_PROP}" = "x" ]
then
    ZOO_LOG4J_PROP="INFO,CONSOLE"

 四、常用命令

1、登录服务器  :zkCli.sh -server localhost:2181

2、查看帮助  help:

[zk: 192.168.1.13:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
    stat path [watch]
    set path data [version]
    ls path [watch]
    delquota [-n|-b] path
    ls2 path [watch]
    setAcl path acl
    setquota -n|-b val path
    history 
    redo cmdno
    printwatches on|off
    delete path [version]
    sync path
    listquota path
    rmr path
    get path [watch]
    create [-s] [-e] path data acl
    addauth scheme auth
    quit 
    getAcl path
    close 
    connect host:port

3、查看更目录的节点:

[zk: 192.168.1.13:2181(CONNECTED) 1] ls /
[zookeeper]

4、创建节点: 创建一个新的Znode节点"zkTest",以及和它相关内容myData,

[zk: 192.168.1.13:2181(CONNECTED) 2] create /zkTest myData
Created /zkTest

5、查看节点中的内容

[zk: 192.168.1.13:2181(CONNECTED) 4] get /zkTest
myData
cZxid = 0x300000002
ctime = Wed Aug 16 19:48:01 CST 2017
mZxid = 0x300000002
mtime = Wed Aug 16 19:48:01 CST 2017
pZxid = 0x300000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0

6、修改节点内容

[zk: 192.168.1.13:2181(CONNECTED) 5] set /zkTest testSet
cZxid = 0x300000002
ctime = Wed Aug 16 19:48:01 CST 2017
mZxid = 0x300000003
mtime = Wed Aug 16 19:52:22 CST 2017
pZxid = 0x300000002
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0

7、删除节点

[zk: 192.168.1.13:2181(CONNECTED) 7] delete /zkTest
[zk: 192.168.1.13:2181(CONNECTED) 9] ls /
[zookeeper]

 

posted @ 2017-08-15 21:17  小禾点点  阅读(710)  评论(1编辑  收藏  举报