【1】集群环境搭建分布式
① 首先按照Zookeeper基础入门与安装部署中在三个服务器上安装配置单机版Zookeeper。
② 在Zookeeper的数据目录下创建名字为myid的文件,内容为其集群唯一编号。
如这里数据目录均在/home/softinstall/apache-zookeeper-3.5.5-bin/zkData下,这个路径可以自定义,配置在zoo.cfg中。
如下所示在服务器一中创建myid文件
[root@localhost apache-zookeeper-3.5.5-bin]# cd zkData/ [root@localhost zkData]# touch myid [root@localhost zkData]# vim myid [root@localhost zkData]# more myid 1
③ 修改集群中每个Zookeeper的zoo.cfg文件
添加集群配置:
server.1=192.168.0.100:2888:3888 server.2=192.168.0.101:2888:3888 server.3=192.168.0.110:2888:3888
配置参数说明
server.A=B:C:D
A是一个数字,表示这是第几号服务器。集群模式下配置一个文件myid,这个文件在dataDir目录下,这文件里面的数据就是A的值。Zookeeper启动时读取此文件,拿到里面的数据与zoo.cfg里面的配置信息比对从而判断到底是哪个server。
B是服务器IP地址。
C是这个服务器与集群中leader服务器交换信息的端口。
D是万一集群中的leader服务器挂了,需要一个端口来重新进行选举,选出一个新的leader。这个端口就是用来执行选举时服务器相互通信的端口。
④ 启动server1
看下面日志,是不是出错了?
[root@localhost apache-zookeeper-3.5.5-bin]# vim conf/zoo.cfg [root@localhost apache-zookeeper-3.5.5-bin]# bin/zkServer.sh start /usr/bin/java ZooKeeper JMX enabled by default Using config: /home/softinstall/apache-zookeeper-3.5.5-bin/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [root@localhost apache-zookeeper-3.5.5-bin]# bin/zkServer.sh status /usr/bin/java ZooKeeper JMX enabled by default Using config: /home/softinstall/apache-zookeeper-3.5.5-bin/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Error contacting service. It is probably not running. [root@localhost apache-zookeeper-3.5.5-bin]#
⑤ 启动server2
//如下是server2 [root@localhost apache-zookeeper-3.5.5-bin]# vim conf/zoo.cfg [root@localhost apache-zookeeper-3.5.5-bin]# bin/zkServer.sh start /usr/bin/java ZooKeeper JMX enabled by default Using config: /home/softinstall/apache-zookeeper-3.5.5-bin/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [root@localhost apache-zookeeper-3.5.5-bin]# bin/zkServer.sh status /usr/bin/java ZooKeeper JMX enabled by default Using config: /home/softinstall/apache-zookeeper-3.5.5-bin/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: leader [root@localhost apache-zookeeper-3.5.5-bin]#
server2成功晋级为leader,此时再次查看server1的status会发现其成为了follower:
//如下是server1 [root@localhost apache-zookeeper-3.5.5-bin]# bin/zkServer.sh status /usr/bin/java ZooKeeper JMX enabled by default Using config: /home/softinstall/apache-zookeeper-3.5.5-bin/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: follower
⑥ 启动server3
server3成为server2的follower。
⑦ 客户端命令行操作
可以使用help查看客户端有哪些命令可操作:
ZooKeeper -server host:port cmd args addauth scheme auth close config [-c] [-w] [-s] connect host:port create [-s] [-e] [-c] [-t ttl] path [data] [acl] delete [-v version] path deleteall path delquota [-n|-b] path get [-s] [-w] path getAcl [-s] path history listquota path ls [-s] [-w] [-R] path ls2 path [watch] printwatches on|off quit reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*] redo cmdno removewatches path [-c|-d|-a] [-l] rmr path set [-s] [-v version] path data setAcl [-s] [-v version] [-R] path acl setquota -n|-b val path stat [-w] path sync path
- 查看当前znode中包含的内容
ls /
[zk: localhost:2181(CONNECTED) 5] ls / [zookeeper]
- 查看当前节点详细数据
ls2 /
[zk: localhost:2181(CONNECTED) 6] ls2 / 'ls2' has been deprecated. Please use 'ls [-s] path' instead. [zookeeper] cZxid = 0x0 ctime = Thu Jan 01 08:00:00 CST 1970 mZxid = 0x0 mtime = Thu Jan 01 08:00:00 CST 1970 pZxid = 0x0 cversion = -1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 1
该命令提示过期建议使用ls [-s] path
[zk: localhost:2181(CONNECTED) 7] ls -s / [zookeeper]cZxid = 0x0 ctime = Thu Jan 01 08:00:00 CST 1970 mZxid = 0x0 mtime = Thu Jan 01 08:00:00 CST 1970 pZxid = 0x0 cversion = -1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 1
【2】单机搭建伪分布式
同上面一样,只不过是在一台机器组件zookeeper集群,如下图所示:
3181配置文件如下:
clientPort=3181 admin.serverPort=28881 server.1=127.0.0.1:2881:3881 server.2=127.0.0.1:2882:3882 server.3=127.0.0.1:2883:3883
3182配置文件如下:
clientPort=3182 admin.serverPort=28882 server.1=127.0.0.1:2881:3881 server.2=127.0.0.1:2882:3882 server.3=127.0.0.1:2883:3883
3183配置文件如下:
clientPort=3183 admin.serverPort=28883 server.1=127.0.0.1:2881:3881 server.2=127.0.0.1:2882:3882 server.3=127.0.0.1:2883:3883
同样,在每个data目录下有个myid文件,里面配置了自己的编号。