zookeeper的简单集群搭建

1.zookeeper安装和使用
包下载地址 :http://zookeeper.apache.org/releases.html#download

2.在本机建立三个文件夹:server1 server2 server3

3.在每个文件夹下建立文件:data datalog zookeeper-3.4.6
inter12@inter12:~/soft/zk/server1$ ll
总用量 24
drwxrwxr-x 6 inter12 inter12 4096 9月 10 11:49 ./
drwxrwxr-x 5 inter12 inter12 4096 9月 10 13:44 ../
drwxrwxr-x 2 inter12 inter12 4096 9月 10 11:51 data/
drwxrwxr-x 2 inter12 inter12 4096 9月 10 11:49 datalog/
drwxr-xr-x 10 inter12 inter12 4096 9月 10 11:48 zookeeper-3.4.6/

4.在每一个data目录下建一个myid文件。
echo “1” >> myid 每个文件夹下的myid文件 输入一个数值
inter12@inter12:~/soft/zk/server1/data$ more myid
1

5.在/home/inter12/soft/zk/server1/zookeeper-3.4.6/conf 目录下
cp zoo_sample.cfg zoo.cfg

将一下内容复制到文件中:
tickTime=2000
initLimit=5
syncLimit=2
dataDir=xxxx/zookeeper/server1/data # 数据文件路径
dataLogDir=xxx/zookeeper/server1/dataLog # 数据日志路径
clientPort=2181
server.1=127.0.0.1:2888:3888 # 1就是myid中数值
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890

server后面的1,2,3就是在data目录下myid中的数值
启动:
cd /home/inter12/soft/zk/server1/zookeeper-3.4.6/bin
./zkServer.sh start
查看状态:
inter12@inter12:~/soft/zk/server1/zookeeper-3.4.6/bin$ ./zkServer.sh status
JMX enabled by default
Using config: /home/inter12/soft/zk/server1/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower
inter12@inter12:~/soft/zk/server2/zookeeper-3.4.6/bin$ ./zkServer.sh status
JMX enabled by default
Using config: /home/inter12/soft/zk/server2/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: leader

inter12@inter12:~/soft/zk/server3/zookeeper-3.4.6/bin$ ./zkServer.sh status
JMX enabled by default
Using config: /home/inter12/soft/zk/server3/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower

看到日志上:第二台是leader。

客户端连接上去查看一些信息:
inter12@inter12:~/soft/zk/server3/zookeeper-3.4.6/bin$ ./zkCli.sh -server localhost:2181
Connecting to localhost:2181
2014-09-10 14:29:25,670 [myid:] – INFO [main:Environment@100] – Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
2014-09-10 14:29:25,678 [myid:] – INFO [main:Environment@100] – Client environment:host.name=inter12
2014-09-10 14:29:25,678 [myid:] – INFO [main:Environment@100] – Client environment:java.version=1.7.0_60
2014-09-10 14:29:25,683 [myid:] – INFO [main:Environment@100] – Client environment:java.vendor=Oracle Corporation
2014-09-10 14:29:25,683 [myid:] – INFO [main:Environment@100] – Client environment:java.home=/home/inter12/soft/jdk/jdk/jre
2014-09-10 14:29:25,683 [myid:] – INFO [main:Environment@100] – Client environment:java.class.path=/home/inter12/soft/zk/server3/zookeeper-3.4.6/bin/../build/classes:/home/inter12/soft/zk/server3/zookeeper-3.4.6/bin/../build/lib/*.jar:/home/inter12/soft/zk/server3/zookeeper-3.4.6/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/inter12/soft/zk/server3/zookeeper-3.4.6/bin/../lib/slf4j-api-1.6.1.jar:/home/inter12/soft/zk/server3/zookeeper-3.4.6/bin/../lib/netty-3.7.0.Final.jar:/home/inter12/soft/zk/server3/zookeeper-3.4.6/bin/../lib/log4j-1.2.16.jar:/home/inter12/soft/zk/server3/zookeeper-3.4.6/bin/../lib/jline-0.9.94.jar:/home/inter12/soft/zk/server3/zookeeper-3.4.6/bin/../zookeeper-3.4.6.jar:/home/inter12/soft/zk/server3/zookeeper-3.4.6/bin/../src/java/lib/*.jar:/home/inter12/soft/zk/server3/zookeeper-3.4.6/bin/../conf:.:/usr/local/jdk/lib/dt.jar:/usr/local/jdk/lib/tools.jar
2014-09-10 14:29:25,683 [myid:] – INFO [main:Environment@100] – Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2014-09-10 14:29:25,683 [myid:] – INFO [main:Environment@100] – Client environment:java.io.tmpdir=/tmp
2014-09-10 14:29:25,684 [myid:] – INFO [main:Environment@100] – Client environment:java.compiler=<NA>
2014-09-10 14:29:25,684 [myid:] – INFO [main:Environment@100] – Client environment:os.name=Linux
2014-09-10 14:29:25,684 [myid:] – INFO [main:Environment@100] – Client environment:os.arch=amd64
2014-09-10 14:29:25,684 [myid:] – INFO [main:Environment@100] – Client environment:os.version=3.2.0-65-generic
2014-09-10 14:29:25,684 [myid:] – INFO [main:Environment@100] – Client environment:user.name=inter12
2014-09-10 14:29:25,684 [myid:] – INFO [main:Environment@100] – Client environment:user.home=/home/inter12
2014-09-10 14:29:25,685 [myid:] – INFO [main:Environment@100] – Client environment:user.dir=/home/inter12/soft/zk/server3/zookeeper-3.4.6/bin
2014-09-10 14:29:25,687 [myid:] – INFO [main:ZooKeeper@438] – Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@c81739c
Welcome to ZooKeeper!
2014-09-10 14:29:25,834 [myid:] – INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@975] – Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2014-09-10 14:29:25,857 [myid:] – INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@852] – Socket connection established to localhost/127.0.0.1:2181, initiating session
JLine support is enabled
2014-09-10 14:29:25,915 [myid:] – INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1235] – Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x1485e3c8b480000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]

可以通过help命令来查看一些东西
[zk: localhost:2181(CONNECTED) 3] help
ZooKeeper -server host:port cmd args
connect host:port
get path [watch]
ls path [watch]
set path data [version]
rmr path
delquota [-n|-b] path
quit
printwatches on|off
create [-s] [-e] path data acl
stat path [watch]
close
ls2 path [watch]
history
listquota path
setAcl path acl
getAcl path
sync path
redo cmdno
addauth scheme auth
delete path [version]
setquota -n|-b val path

当前的根目录是:
[zk: localhost:2181(CONNECTED) 4] ls /
[zookeeper]
停止第二台服务器:
Using config: /home/inter12/soft/zk/server2/zookeeper-3.4.6/bin/../conf/zoo.cfg
Stopping zookeeper … STOPPED

第三台成为了leader。
inter12@inter12:~/soft/zk/server3/zookeeper-3.4.6/bin$ ./zkServer.sh status
JMX enabled by default
Using config: /home/inter12/soft/zk/server3/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: leader
查看状态信息:
echo stat | nc localhost 2181

参考资料:
http://shiyanjun.cn/archives/469.html
http://shiyanjun.cn/archives/474.html
淘宝的zk指南:http://jm-blog.aliapp.com/?p=2318

作者: inter12

在这苦短的人生中,追求点自己的简单快乐

发表评论

电子邮件地址不会被公开。 必填项已用*标注