一、安装

ZooKeeper集群的配置和单机的配置并无多大差别,只需要修改一下配置文件,具体安装信息参考http://www.cnblogs.com/LyndonYoung/articles/8025010.html

二、修改hosts文件

首先修改集群中每台机器的/etc/hosts文件,将每台机器的信息写入,前面为ip,后面为主机名,我的配置如下:

192.168.1.106 master
192.168.1.107 slave1
192.168.1.108 slave2

ZooKeeper采用一种称为Leader election的选举算法。在整个集群运行过程中,只有一个Leader,其他的都是Follower,

如果ZooKeeper集群在运行过程中Leader出了问题,系统会采用该算法重新选出一个Leader。因此,各个结点之间要能够保证互相连接,必须配置上述映射。

三、修改配置文件

tickTime=2000   
dataDir=/home/Lyndon/zookeeper   
clientPort=12182  
initLimit=5   
syncLimit=2  

server.1=master:2887:3887
server.2=slave1:2888:3888
server.3=slave2:2889:3889
参数说明:
①tickTime:心跳时间,毫秒为单位。
②initLimit:这个配置项是用来配置 Zookeeper 接受客户端(这里所说的客户端不是用户连接 Zookeeper服务器的客户端,而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过 10 个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 10*2000=20 秒。
③syncLimit:这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 5*2000=10 秒。
④dataDir:存储内存中数据库快照的位置。
⑤clientPort:监听客户端连接的端口
⑥server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。

四、增加myid文件

dataDir目录下创建myid文件,将内容设置为上⑥中的A值,用来标识不同的服务器。
五、测试ZooKeeper集群
1)各节点上启动:./zkServer.sh start
2)jps查看进程
22605 QuorumPeerMain
3)查看master状态
./zkServer.sh status
JMX enabled by default
Using config: /home/Lyndon/zookeeper-3.4.5/bin/../conf/zoo.cfg
Mode: follower
看slave1状态

JMX enabled by default
Using config: /home/Lyndon/zookeeper-3.4.5/bin/../conf/zoo.cfg
Mode: leader

slave2与master相同。

六、客户端连接服务器(我从三台机器中随机选了一台)

连接1号机器:

Lyndon@slave1:~/zookeeper-3.4.5/bin$ ./zkCli.sh -server master:12182
Connecting to master:12182
2017-12-13 21:33:16,043 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT
2017-12-13 21:33:16,066 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=slave1
2017-12-13 21:33:16,067 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.7.0_79
2017-12-13 21:33:16,068 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2017-12-13 21:33:16,069 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/home/Lyndon/jdk1.7/jdk1.7.0_79/jre
2017-12-13 21:33:16,072 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/home/Lyndon/zookeeper-3.4.5/bin/../build/classes:/home/Lyndon/zookeeper-3.4.5/bin/../build/lib/*.jar:/home/Lyndon/zookeeper-3.4.5/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/Lyndon/zookeeper-3.4.5/bin/../lib/slf4j-api-1.6.1.jar:/home/Lyndon/zookeeper-3.4.5/bin/../lib/netty-3.2.2.Final.jar:/home/Lyndon/zookeeper-3.4.5/bin/../lib/log4j-1.2.15.jar:/home/yanghao/zookeeper-3.4.5/bin/../lib/jline-0.9.94.jar:/home/Lyndon/zookeeper-3.4.5/bin/../zookeeper-3.4.5.jar:/home/Lyndon/zookeeper-3.4.5/bin/../src/java/lib/*.jar:/home/Lyndon/zookeeper-3.4.5/bin/../conf:.://home/Lyndon/jdk1.7/jdk1.7.0_79/lib:/home/Lyndon/jdk1.7/jdk1.7.0_79/jre/lib:.://home/Lyndon/jdk1.7/jdk1.7.0_79/lib:/home/Lyndon/jdk1.7/jdk1.7.0_79/jre/lib:.://home/Lyndon/jdk1.7/jdk1.7.0_79/lib:/home/Lyndon/jdk1.7/jdk1.7.0_79/jre/lib:
2017-12-13 21:33:16,074 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2017-12-13 21:33:16,075 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2017-12-13 21:33:16,076 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2017-12-13 21:33:16,077 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2017-12-13 21:33:16,077 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2017-12-13 21:33:16,078 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.13.0-24-generic
2017-12-13 21:33:16,079 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=Lyndon
2017-12-13 21:33:16,079 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/home/Lyndon
2017-12-13 21:33:16,080 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/home/Lyndon/zookeeper-3.4.5/bin
2017-12-13 21:33:16,082 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=master:12182 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@5409396f
Welcome to ZooKeeper!
2017-12-13 21:33:16,159 [myid:] - INFO  [main-SendThread(master:12182):ClientCnxn$SendThread@966] - Opening socket connection to server master/192.168.1.100:12182. Will not attempt to authenticate using SASL (unknown error)
2017-12-13 21:33:16,181 [myid:] - INFO  [main-SendThread(master:12182):ClientCnxn$SendThread@849] - Socket connection established to master/192.168.1.100:12182, initiating session
JLine support is enabled
2017-12-13 21:33:16,212 [myid:] - INFO  [main-SendThread(master:12182):ClientCnxn$SendThread@1207] - Session establishment complete on server master/192.168.1.100:12182, sessionid = 0x16050133dc80001, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: master:12182(CONNECTED) 0] 

 连接3号机器:

Lyndon@slave1:~/zookeeper-3.4.5/bin$ ./zkCli.sh -server slave2:12182
Connecting to slave2:12182
2017-12-13 21:37:11,995 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT
2017-12-13 21:37:12,016 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=slave1
2017-12-13 21:37:12,020 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.7.0_79
2017-12-13 21:37:12,021 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2017-12-13 21:37:12,022 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/home/Lyndon/jdk1.7/jdk1.7.0_79/jre
2017-12-13 21:37:12,023 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/home/Lyndon/zookeeper-3.4.5/bin/../build/classes:/home/Lyndon/zookeeper-3.4.5/bin/../build/lib/*.jar:/home/Lyndon/zookeeper-3.4.5/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/Lyndon/zookeeper-3.4.5/bin/../lib/slf4j-api-1.6.1.jar:/home/Lyndon/zookeeper-3.4.5/bin/../lib/netty-3.2.2.Final.jar:/home/Lyndon/zookeeper-3.4.5/bin/../lib/log4j-1.2.15.jar:/home/Lyndon/zookeeper-3.4.5/bin/../lib/jline-0.9.94.jar:/home/Lyndon/zookeeper-3.4.5/bin/../zookeeper-3.4.5.jar:/home/Lyndon/zookeeper-3.4.5/bin/../src/java/lib/*.jar:/home/Lyndon/zookeeper-3.4.5/bin/../conf:.://home/Lyndon/jdk1.7/jdk1.7.0_79/lib:/home/Lyndon/jdk1.7/jdk1.7.0_79/jre/lib:.://home/Lyndon/jdk1.7/jdk1.7.0_79/lib:/home/Lyndon/jdk1.7/jdk1.7.0_79/jre/lib:.://home/yanghao/jdk1.7/jdk1.7.0_79/lib:/home/Lyndon/jdk1.7/jdk1.7.0_79/jre/lib:
2017-12-13 21:37:12,030 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2017-12-13 21:37:12,031 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2017-12-13 21:37:12,033 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2017-12-13 21:37:12,034 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2017-12-13 21:37:12,035 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2017-12-13 21:37:12,035 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.13.0-24-generic
2017-12-13 21:37:12,036 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=Lyndon
2017-12-13 21:37:12,037 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/home/Lyndon
2017-12-13 21:37:12,038 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/home/Lyndon/zookeeper-3.4.5/bin
2017-12-13 21:37:12,050 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=slave2:12182 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@5409396f
Welcome to ZooKeeper!
2017-12-13 21:37:12,120 [myid:] - INFO  [main-SendThread(slave2:12182):ClientCnxn$SendThread@966] - Opening socket connection to server slave2/192.168.1.102:12182. Will not attempt to authenticate using SASL (unknown error)
2017-12-13 21:37:12,218 [myid:] - INFO  [main-SendThread(slave2:12182):ClientCnxn$SendThread@849] - Socket connection established to slave2/192.168.1.102:12182, initiating session
JLine support is enabled
2017-12-13 21:37:12,260 [myid:] - INFO  [main-SendThread(slave2:12182):ClientCnxn$SendThread@1207] - Session establishment complete on server slave2/192.168.1.102:12182, sessionid = 0x36050133db60001, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: slave2:12182(CONNECTED) 0] 

连接本机:

Lyndon@slave1:~/zookeeper-3.4.5/bin$ ./zkCli.sh -server slave1:12182
Connecting to slave1:12182
2017-12-13 21:41:53,827 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT
2017-12-13 21:41:53,846 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=slave1
2017-12-13 21:41:53,847 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.7.0_79
2017-12-13 21:41:53,848 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2017-12-13 21:41:53,854 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/home/yanghao/jdk1.7/jdk1.7.0_79/jre
2017-12-13 21:41:53,855 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/home/Lyndon/zookeeper-3.4.5/bin/../build/classes:/home/Lyndon/zookeeper-3.4.5/bin/../build/lib/*.jar:/home/Lyndon/zookeeper-3.4.5/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/yanghao/zookeeper-3.4.5/bin/../lib/slf4j-api-1.6.1.jar:/home/Lyndon/zookeeper-3.4.5/bin/../lib/netty-3.2.2.Final.jar:/home/Lyndon/zookeeper-3.4.5/bin/../lib/log4j-1.2.15.jar:/home/Lyndon/zookeeper-3.4.5/bin/../lib/jline-0.9.94.jar:/home/Lyndon/zookeeper-3.4.5/bin/../zookeeper-3.4.5.jar:/home/Lyndon/zookeeper-3.4.5/bin/../src/java/lib/*.jar:/home/Lyndon/zookeeper-3.4.5/bin/../conf:.://home/Lyndon/jdk1.7/jdk1.7.0_79/lib:/home/Lyndon/jdk1.7/jdk1.7.0_79/jre/lib:.://home/Lyndon/jdk1.7/jdk1.7.0_79/lib:/home/Lyndon/jdk1.7/jdk1.7.0_79/jre/lib:.://home/Lyndon/jdk1.7/jdk1.7.0_79/lib:/home/Lyndon/jdk1.7/jdk1.7.0_79/jre/lib:
2017-12-13 21:41:53,857 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2017-12-13 21:41:53,858 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2017-12-13 21:41:53,859 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2017-12-13 21:41:53,860 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2017-12-13 21:41:53,861 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2017-12-13 21:41:53,862 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.13.0-24-generic
2017-12-13 21:41:53,862 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=Lyndon
2017-12-13 21:41:53,877 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/home/Lyndon
2017-12-13 21:41:53,877 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/home/Lyndon/zookeeper-3.4.5/bin
2017-12-13 21:41:53,880 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=slave1:12182 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@5409396f
Welcome to ZooKeeper!
2017-12-13 21:41:53,993 [myid:] - INFO  [main-SendThread(slave1:12182):ClientCnxn$SendThread@966] - Opening socket connection to server slave1/192.168.1.101:12182. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2017-12-13 21:41:54,036 [myid:] - INFO  [main-SendThread(slave1:12182):ClientCnxn$SendThread@849] - Socket connection established to slave1/192.168.1.101:12182, initiating session
2017-12-13 21:41:54,115 [myid:] - INFO  [main-SendThread(slave1:12182):ClientCnxn$SendThread@1207] - Session establishment complete on server slave1/192.168.1.101:12182, sessionid = 0x260501354840001, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: slave1:12182(CONNECTED) 0] 

七、查看日志信息

可以通过zookeeper的bin目录下的zookeeper.out查看日志信息
tail -f zookeeper.out

其他、Zookeeper有三种模式,leader,follower,observer,设置observer的方法如下:

在所有节点的配置文件中修改此处:

server.3=slave2:2889:3889:observer即可


参考链接:http://blog.csdn.net/hwwn2009/article/details/40000881

posted on 2017-12-12 22:15  LyndonYoung  阅读(220)  评论(0编辑  收藏  举报