安装
zookeeper-3.4.9.tar.gz
下载:
https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/zookeeper-3.4.9.tar.gz
为了方便管理,我放在目录/opt/中
解压:
tar zxf zookeeper-3.4.9.tar.gz
进入配置目录:
cd zookeeper-3.4.9/conf
创建配置(直接clone样例配置):
cp zoo_sample.cfg zoo.cfg
注:zoo_sample.cfg是一个用来demo的例子配置
先配置单机模式:
修改配置:
# 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/zookeeper-3.4.9/data // 日志文件 dataLogDir=/opt/zookeeper-3.4.9/logs # 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
配置环境变量:
打开系统配置文件:
vim /etc/profile
加入:
export ZOOKEEPER_HOME=/opt/zookeeper-3.4.9/
追加到环境变量$Path中:
export PATH=$PATH:….$ZOOKEEPER_HOME/bin/
退出:
:wq
重新激活环境变量配置:
[[email protected]_105_239 bin]# source /etc/profile
启动ZooKeeper:
[[email protected]_105_239 bin]# zkServer.sh start ZooKeeper JMX enabled by default Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
查询zk的状态:
[[email protected]_105_239 bin]# zkServer.sh status ZooKeeper JMX enabled by default Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg Mode: standalone
关闭ZooKeeper:
[[email protected]_105_239 bin]# zkServer.sh stop ZooKeeper JMX enabled by default Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg Stopping zookeeper ... STOPPED
重启ZooKeeper(由于当前是关闭状态,所以会有下面no zookeeper的信息):
[[email protected]_105_239 bin]# zkServer.sh restart ZooKeeper JMX enabled by default Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg ZooKeeper JMX enabled by default Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg Stopping zookeeper ... no zookeeper to stop (could not find file /opt/zookeeper-3.4.9/data /opt/zookeeper-3.4.9/logs/zookeeper_server.pid) ZooKeeper JMX enabled by default Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
客户端测试链接服务端:
[[email protected]_105_239 bin]# zkCli.sh -server 127.0.0.1:2181 Connecting to 127.0.0.1:2181 2016-11-01 22:20:40,226 [myid:] - INFO [main:[email protected]] - Client environment:zookeeper.version=3.4.9-1757313, built on 08/23/2016 06:50 GMT 2016-11-01 22:20:40,229 [myid:] - INFO [main:[email protected]] - Client environment:host.name=<NA> 2016-11-01 22:20:40,229 [myid:] - INFO [main:[email protected]] - Client environment:java.version=1.8.0_111 2016-11-01 22:20:40,230 [myid:] - INFO [main:[email protected]] - Client environment:java.vendor=Oracle Corporation 2016-11-01 22:20:40,231 [myid:] - INFO [main:[email protected]] - Client environment:java.home=/usr/java/jdk1.8.0_111/jre 2016-11-01 22:20:40,231 [myid:] - INFO [main:[email protected]] - Client environment:java.class.path=/opt/zookeeper-3.4.9/bin/../build/classes:/opt/zookeeper-3.4.9/bin/../build/lib/*.jar:/opt/zookeeper-3.4.9/bin/../lib/slf4j-log4j12-1.6.1.jar:/opt/zookeeper-3.4.9/bin/../lib/slf4j-api-1.6.1.jar:/opt/zookeeper-3.4.9/bin/../lib/netty-3.10.5.Final.jar:/opt/zookeeper-3.4.9/bin/../lib/log4j-1.2.16.jar:/opt/zookeeper-3.4.9/bin/../lib/jline-0.9.94.jar:/opt/zookeeper-3.4.9/bin/../zookeeper-3.4.9.jar:/opt/zookeeper-3.4.9/bin/../src/java/lib/*.jar:/opt/zookeeper-3.4.9/bin/../conf: 2016-11-01 22:20:40,231 [myid:] - INFO [main:[email protected]] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 2016-11-01 22:20:40,231 [myid:] - INFO [main:[email protected]] - Client environment:java.io.tmpdir=/tmp 2016-11-01 22:20:40,231 [myid:] - INFO [main:[email protected]] - Client environment:java.compiler=<NA> 2016-11-01 22:20:40,231 [myid:] - INFO [main:[email protected]] - Client environment:os.name=Linux 2016-11-01 22:20:40,231 [myid:] - INFO [main:[email protected]] - Client environment:os.arch=amd64 2016-11-01 22:20:40,231 [myid:] - INFO [main:[email protected]] - Client environment:os.version=3.10.0-327.el7.x86_64 2016-11-01 22:20:40,231 [myid:] - INFO [main:[email protected]] - Client environment:user.name=root 2016-11-01 22:20:40,231 [myid:] - INFO [main:[email protected]] - Client environment:user.home=/root 2016-11-01 22:20:40,231 [myid:] - INFO [main:[email protected]] - Client environment:user.dir=/opt/zookeeper-3.4.9/bin 2016-11-01 22:20:40,232 [myid:] - INFO [main:[email protected]] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 [email protected] Welcome to ZooKeeper! JLine support is enabled 2016-11-01 22:20:40,345 [myid:] - INFO [main-SendThread(127.0.0.1:2181):[email protected]] - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) 2016-11-01 22:20:40,505 [myid:] - WARN [main-SendThread(127.0.0.1:2181):[email protected]] - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect java.net.ConnectException: Connection refused at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361) at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1141)
解决:重启Zookeeper服务 - zkServer.sh restart
从新测试客户端链接服务端:
[[email protected]_105_239bin]# zkCli.sh Connecting tolocalhost:2181 2016-11-0217:16:01,054 [myid:] - INFO [main:[email protected]] - Clientenvironment:zookeeper.version=3.4.9-1757313, built on 08/23/2016 06:50 GMT 2016-11-0217:16:01,062 [myid:] - INFO [main:[email protected]] - Client environment:host.name=Centos_105_239 2016-11-0217:16:01,062 [myid:] - INFO [main:[email protected]] - Client environment:java.version=1.8.0_111 2016-11-0217:16:01,066 [myid:] - INFO [main:[email protected]] - Client environment:java.vendor=OracleCorporation 2016-11-0217:16:01,066 [myid:] - INFO [main:[email protected]] - Clientenvironment:java.home=/usr/java/jdk1.8.0_111/jre 2016-11-0217:16:01,066 [myid:] - INFO [main:[email protected]] - Clientenvironment:java.class.path=/opt/zookeeper-3.4.9/bin/../build/classes:/opt/zookeeper-3.4.9/bin/../build/lib/*.jar:/opt/zookeeper-3.4.9/bin/../lib/slf4j-log4j12-1.6.1.jar:/opt/zookeeper-3.4.9/bin/../lib/slf4j-api-1.6.1.jar:/opt/zookeeper-3.4.9/bin/../lib/netty-3.10.5.Final.jar:/opt/zookeeper-3.4.9/bin/../lib/log4j-1.2.16.jar:/opt/zookeeper-3.4.9/bin/../lib/jline-0.9.94.jar:/opt/zookeeper-3.4.9/bin/../zookeeper-3.4.9.jar:/opt/zookeeper-3.4.9/bin/../src/java/lib/*.jar:/opt/zookeeper-3.4.9/bin/../conf: 2016-11-0217:16:01,067 [myid:] - INFO [main:[email protected]] - Clientenvironment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 2016-11-0217:16:01,067 [myid:] - INFO [main:[email protected]] - Client environment:java.io.tmpdir=/tmp 2016-11-0217:16:01,067 [myid:] - INFO [main:[email protected]] - Client environment:java.compiler=<NA> 2016-11-0217:16:01,067 [myid:] - INFO [main:[email protected]] - Client environment:os.name=Linux 2016-11-0217:16:01,067 [myid:] - INFO [main:[email protected]] - Client environment:os.arch=amd64 2016-11-0217:16:01,067 [myid:] - INFO [main:[email protected]] - Clientenvironment:os.version=3.10.0-327.el7.x86_64 2016-11-0217:16:01,068 [myid:] - INFO [main:[email protected]] - Client environment:user.name=root 2016-11-0217:16:01,068 [myid:] - INFO [main:[email protected]] - Client environment:user.home=/root 2016-11-0217:16:01,068 [myid:] - INFO [main:[email protected]] - Clientenvironment:user.dir=/opt/zookeeper-3.4.9/bin 2016-11-0217:16:01,073 [myid:] - INFO [main:[email protected]] - Initiating client connection,connectString=localhost:2181 sessionTi[email protected]446cdf90 Welcome toZooKeeper! JLine support isenabled 2016-11-0217:16:06,165 [myid:] - INFO [main-SendThread(localhost:2181):[email protected]] - Openingsocket connection to server localhost/127.0.0.1:2181. Will not attempt toauthenticate using SASL (unknown error) 2016-11-0217:16:06,306 [myid:] - INFO [main-SendThread(localhost:2181):[email protected]] - Socketconnection established to localhost/127.0.0.1:2181, initiating session 2016-11-0217:16:06,328 [myid:] - INFO [main-SendThread(localhost:2181):[email protected]] - Sessionestablishment complete on server localhost/127.0.0.1:2181, sessionid =0x15823b834e60008, negotiated timeout = 30000 WATCHER:: WatchedEventstate:SyncConnected type:None path:null [zk: localhost:2181(CONNECTED)0]#
见到最后一行才算是成功。
测试
创建一个节点
[zk:localhost:2181(CONNECTED) 3] create /node helloword Created /node [zk:localhost:2181(CONNECTED) 4] ls / [node, zookeeper]
获取节点
[zk:localhost:2181(CONNECTED) 8] get /node helloword cZxid = 0x17 ctime = Wed Nov 0217:17:25 CST 2016 mZxid = 0x17 mtime = Wed Nov 0217:17:25 CST 2016 pZxid = 0x17 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 9 numChildren = 0
设置节点
[zk:localhost:2181(CONNECTED) 9] set /node helloword! cZxid = 0x17 ctime = Wed Nov 0217:17:25 CST 2016 mZxid = 0x1d mtime = Wed Nov 0217:19:40 CST 2016 pZxid = 0x17 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 10 numChildren = 0
再次获取节点,看看变化:
[zk:localhost:2181(CONNECTED) 10] get /node helloword! cZxid = 0x17 ctime = Wed Nov 0217:17:25 CST 2016 mZxid = 0x1d mtime = Wed Nov 0217:19:40 CST 2016 pZxid = 0x17 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 10 numChildren = 0
测试删除节点:
[zk:localhost:2181(CONNECTED) 14] ls / [node, zookeeper,test] [zk:localhost:2181(CONNECTED) 15] delete /test [zk:localhost:2181(CONNECTED) 16] ls / [node, zookeeper]
配置集群
再搭建三个同样的虚拟环境,最简单的是直接clone上面的单机,不过要记住重新初始化网卡信息,具体的不再讲怎么操作了,有问题请直接留言。
修改zookeeper的参数:
# 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 server.1=192.168.1.156:2888:3888 server.2=192.168.1.158:2888:3888 server.3=192.168.1.159:2888:3888
端口说明:
2888:集群中服务器的通讯
3888:选领导
具体的测试就不再叙述,在搭建中遇到的问题,请参阅《zookeeper环境搭建中遇到的问题》
参考:http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/#ibm-pcon
时间: 2024-10-19 02:28:29