一、准备至少三台Linux服务器及对应的jdk环境
1、服务器及jdk环境准备
服务器:至少三台Linux服务器
JDK环境:三台Linux服务器上都需要安装好jdk环境(jdk环境安装参考我的博客:https://blog.csdn.net/qq_26420601/article/details/109710153)
192.168.209.131 Linux服务器
192.168.209.133 Linux服务器
192.168.209.134 Linux服务器
2、zookeeper环境准备
分别在三台Linux服务器上安装Zookeeper。
请参考我的博客:Zookeeper之Linux本地模式安装部署(Zookeeper之Linux本地模式安装部署_梦想天涯~路在脚下的博客-CSDN博客)
只有在Linux系统中安装好了Zookeeper之后才能继续下面的分布式安装部署。
二、分布式安装部署
1、分布式集群规划
在192.168.209.131、192.168.209.133、192.168.209.134三台服务器(作为Zookeeper的三个节点)上分别部署Zookeeper。
2、配置服务器编号
192.168.209.131服务器配置
在192.168.209.131服务器的 opt/module/apache-zookeeper-3.6.2-bin/zkData/ 目录下创建myid文件,并在文件中添加与server对应的编号 2(这个编号可以根据自己的需要去定义)
192.168.209.133服务器配置
在192.168.209.133服务器的 opt/module/apache-zookeeper-3.6.2-bin/zkData/ 目录下创建myid文件,并在文件中添加与server对应的编号 2(这个编号可以根据自己的需要去定义)
192.168.209.134服务器配置
在192.168.209.134服务器的 opt/module/apache-zookeeper-3.6.2-bin/zkData/ 目录下创建myid文件,并在文件中添加与server对应的编号 2(这个编号可以根据自己的需要去定义)
2、配置zoo.cfg文件
(1)192.168.209.131服务器zoo.cfg配置文件修改
重命名opt/module/apache-zookeeper-3.6.2-bin/conf/目录下的zoo_sample.cfg 为 zoo.cfg
执行命令:mv zoo_sample.cfg zoo.cfg
(2)修改zoo.cfg配置文件中内容
# 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/module/apache-zookeeper-3.6.2-bin/zkData
# 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
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
#######################cluster##########################
server.2=192.168.209.131:2888:3888
server.3=192.168.209.133:2888:3888
server.4=192.168.209.134:2888:3888
(3)配置参数解读
#######################cluster##########################
server.2=192.168.209.131:2888:3888
server.3=192.168.209.133:2888:3888
server.4=192.168.209.134:2888:3888
三、集群操作
ZooKeeper JMX enabled by default
Using config: /opt/module/apache-zookeeper-3.6.2-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Error contacting service. It is probably not running.
![](https://i-blog.csdnimg.cn/blog_migrate/d36ceae3ab47f18915d774f71f35b0eb.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cb7c70bad08fa3c66a09dffb91517f5a.png)
如果没有开启2888端口和3888端口需要手动去开启一下防火墙端口
执行增加防火墙需要开启端口的命令:
firewall-cmd --add-port=2888/tcp --permanent
firewall-cmd --add-port=3888/tcp --permanent
然后重启防火墙,执行命令:firewall-cmd --reload
查看防火墙端口开启列表,执行命令:firewall-cmd --list-all
分别启动三台服务器上的Zookeeper服务
192.168.209.131服务器Zookeeper启动并查看启动状态
192.168.209.133服务器Zookeeper启动并查看启动状态
192.168.209.134服务器Zookeeper启动并查看启动状态
四、Zookeeper客户端启动及shell命令操作
Zookeeper客户端命令基本语法
基本语法命令 | 功能描述 |
heip | 显示所有命令操作 |
ls path[watch] | 使用ls命令来查看当前znode节点中所包含的内容 |
ls2 path[watch] | 查看当前节点数据并能看到更新次数等数据 |
create | 普通创建 -s 含有序列 -e 临时(重启或者超时小时) |
get path[watch] | 获得节点的值 |
set | 设置节点的具体值 |
stat | 查看节点状态 |
delete | 删除节点 |
rmr | 递归删除节点 |
1、启动Zookeeper客户端命令
./zkCli.sh
2、显示所有操作命令
help
3、查看当前znode中所包含的内容
ls /
4、查看当前节点详细数据,在最新版的Zookeeper该命令已经变为 ls -s path
ls2 /
[zk: localhost:2181(CONNECTED) 1] ls2 /
[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
5、分别创建2个普通节点
[zk: localhost:2181(CONNECTED) 3] create /sanguo "jinlian"
Created /sanguo
[zk: localhost:2181(CONNECTED) 4] create /sanguo/shuguo
"liubei"
Created /sanguo/shuguo
6、获得节点的值
[zk: localhost:2181(CONNECTED) 5] get /sanguo
jinlian
cZxid = 0x100000003
ctime = Wed Aug 29 00:03:23 CST 2018
mZxid = 0x100000003
mtime = Wed Aug 29 00:03:23 CST 2018
pZxid = 0x100000004
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 1
[zk: localhost:2181(CONNECTED) 6]
[zk: localhost:2181(CONNECTED) 6] get /sanguo/shuguo
liubei
cZxid = 0x100000004
ctime = Wed Aug 29 00:04:35 CST 2018
mZxid = 0x100000004
mtime = Wed Aug 29 00:04:35 CST 2018
pZxid = 0x100000004
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
7、创建短暂节点
[zk: localhost:2181(CONNECTED) 7] create -e /sanguo/wuguo
"zhouyu"
Created /sanguo/wuguo
(1)在当前客户端是能查看到的
[zk: localhost:2181(CONNECTED) 3] ls /sanguo
[wuguo, shuguo]
(2)退出当前客户端然后再重启客户端
[zk: localhost:2181(CONNECTED) 12] quit
[atguigu@hadoop104 zookeeper-3.4.10]$ bin/zkCli.sh
(3)再次查看根目录下短暂节点已经删除
[zk: localhost:2181(CONNECTED) 0] ls /sanguo
8、创建带序号的节点
(1)先创建一个普通的根节点/sanguo/weiguo
[zk: localhost:2181(CONNECTED) 1] create /sanguo/weiguo
"caocao"
Created /sanguo/weiguo
(2)创建带序号的节点
[zk: localhost:2181(CONNECTED) 2] create -s
/sanguo/weiguo/xiaoqiao "jinlian"
Created /sanguo/weiguo/xiaoqiao0000000000
[zk: localhost:2181(CONNECTED) 3] create -s
/sanguo/weiguo/daqiao "jinlian"
Created /sanguo/weiguo/daqiao0000000001
[zk: localhost:2181(CONNECTED) 4] create -s
/sanguo/weiguo/diaocan "jinlian"
Created /sanguo/weiguo/diaocan0000000002
如果原来没有序号节点,序号从 0 开始依次递增。如果原节点下已有 2 个节点,则再排序时从 2 开始,以此类推。
9、修改节点数据值
[zk: localhost:2181(CONNECTED) 6] set /sanguo/weiguo "simayi"
10、节点的值变化监听
(1)在 192.168.209.133 主机上注册监听/sanguo 节点数据变化
[zk: localhost:2181(CONNECTED) 8] get /sanguo watch
(2)在 192.168.209.131 从机上修改/sanguo 节点的数据
[zk: localhost:2181(CONNECTED) 1] set /sanguo "xisi"
(3)观察 192.168.209.133 主机收到数据变化的监听
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged
path:/sanguo
11、节点删除
[zk: localhost:2181(CONNECTED) 4] delete /sanguo/jin
12、递归删除节点
[zk: localhost:2181(CONNECTED) 15] rmr /sanguo/shuguo
13、查看节点状态
[zk: localhost:2181(CONNECTED) 17] stat /sanguo
cZxid = 0x100000003
ctime = Wed Aug 29 00:03:23 CST 2018
mZxid = 0x100000011
mtime = Wed Aug 29 00:21:23 CST 2018
pZxid = 0x100000014
cversion = 9
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 1