zookeeper 是 google 内部项目chubby项目的开源实现,其主要功能是实现分布式系统的分布式协作。
分布式主要涉及两个问题:任务拆分与节点分工
分布式解决的是高可用高并发的问题。
集群解决的是高可用问题。
集群是从物理上来定义,分布式是一种工作方式。
例如:一个工作任务需要10个小时
分布式:10台机器,只需要1个小时就能完成
集群:10台机器,还是需要10个小时才能完成。
死锁与活锁?
分布式中遇到的问题:
1.节点必须是高可用的
2.各个节点数据必须保持一致
3.如何解决通讯异常问题
4.网络分区
zookeeper解决的问题:
1.Master节点管理:Master高可用,保证节点唯一
2.配置文件管理:统一把配置文件放到zk上,由zk统一分发
3.实现发布订阅:生产者将消息放到zk上,供消费者获取
4.分布式锁:分布式环境访问同一个资源时,由第三方配锁实现,保持数据一致性
5.集群的管理
单机伪分布式zookeeper集群搭建
安装jdk
root@ubuntu:/app# java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
下载zookeeper,解压文件夹
root@ubuntu:/app# tar -zxvf -C /user/local/
配置zookeeper的数据目录,并在目录下创建对应的myid,每个myid都赋值
[root@localhost conf]# cd /tmp/
[root@localhost tmp]# mkdir zookeeper
[root@localhost zookeeper]# mkdir zk1
[root@localhost zookeeper]# mkdir zk2
[root@localhost zookeeper]# mkdir zk3
[root@localhost zookeeper]# cd zk1
[root@localhost zk1]# touch myid
[root@localhost zk1]# echo 1 >> /tmp/zookeeper/zk1/myid
[root@localhost zk1]# cat /tmp/zookeeper/zk1/myid
1
[root@localhost zookeeper]# cd zk2
[root@localhost zk2]# ls
[root@localhost zk2]# touch myid
[root@localhost zk2]# echo 2 >> /tmp/zookeeper/zk2/myid
[root@localhost zk2]# cat /tmp/zookeeper/zk2/myid
2
[root@localhost zookeeper]# cd zk3
[root@localhost zk3]# ls
[root@localhost zk3]# touch myid
[root@localhost zk3]# ls
myid
[root@localhost zk3]# echo 3 >> /tmp/zookeeper/zk3/myid
[root@localhost zk3]# ls
myid
[root@localhost zk3]# cat /tmp/zookeeper/zk3/myid
3
配置zookeeper
[root@ubuntu /usr/local]# cd zookeeper-3.4.11/
[root@ubuntu /usr/local/zookeeper]# cd conf/
[root@ubuntu /usr/local/zookeeper/conf]# cp zoo_sample.cfg zoo1.cfg # 拷贝官方提供的模板配置文件
[root@ubuntu /usr/local/zookeeper/conf]# cp zoo_sample.cfg zoo2.cfg
[root@ubuntu /usr/local/zookeeper/conf]# cp zoo_sample.cfg zoo3.cfg
[root@ubuntu /usr/local/zookeeper/conf]# vim zoo1.cfg # 增加或修改成如下内容
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper/zk1/
clientPort=2181
4lw.commands.whitelist=*
#server.myid值=主机名:通讯端口:选举端口
#主机名查看命令 hostname
server.1=master:2888:3888
server.2=master:2889:3889
server.3=master:2890:3890
quorumListenOnAllIPs=true
[root@ubuntu /usr/local/zookeeper/conf]# cd ../
真分布式
Zookeeper真分布部署在master、slave1、slave2 三台不同的服务器上
conf配置文件不用区分zoo.cfg只有一份
myid也只有一份 /usr/local/zookeeper/zk
server配置也要多个主机地址:
server.1=master:2888:3888
server.2=slave1:2888:3888
server.3=slave2:2888:3888
启动集群
[root@ubuntu ~]# cd /usr/local/zookeeper/bin/
[root@ubuntu /usr/local/zookeeper/bin]# ./zkServer.sh start /usr/local/zookeeper/conf/zoo1.cfg # 启动第一个节点
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@ubuntu /usr/local/zookeeper/bin]# netstat -lntp |grep java # 查看监听的端口
[root@ubuntu /usr/local/zookeeper/bin]# ./zkServer.sh start /usr/local/zookeeper/conf/zoo2.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg # 启动第二个节点
Starting zookeeper ... STARTED
[root@ubuntu /usr/local/zookeeper/bin]# ./zkServer.sh start /usr/local/zookeeper/conf/zoo3.cfg # 启动第三个节点
[root@ubuntu /usr/local/zookeeper/bin]# netstat -lntp |grep java # 查看监听的端口
tcp6 0 0 :::40576 :::* LISTEN 2421/java
tcp6 0 0 :::2181 :::* LISTEN 2282/java
tcp6 0 0 :::2182 :::* LISTEN 2359/java
tcp6 0 0 :::2183 :::* LISTEN 2421/java
tcp6 0 0 :::44808 :::* LISTEN 2282/java
tcp6 0 0 127.0.0.1:2889 :::* LISTEN 2359/java
tcp6 0 0 127.0.0.1:3888 :::* LISTEN 2282/java
tcp6 0 0 127.0.0.1:3889 :::* LISTEN 2359/java
tcp6 0 0 127.0.0.1:3890 :::* LISTEN 2421/java
#通过通讯端口(2889)可以看到哪个是master服务,如图通讯端口对应的是2182服务
[root@study-01 /usr/local/zookeeper/bin]# jps # 查看进程
Will not attempt to authenticate using SASL (unknown error)异常
Will not attempt to authenticate using SASL (unknown error)
这种异常需要在 /etc/host里配置下主机名和IP映射关系
异常查看
1:请关闭防火墙 ufw status查看防火墙状态
2:如果没有监听,请查看bin目录下 zookeeper.out 日志文件
3:./zkServer.sh restart|start|stop|status命令可以 重启|启动|停止|查看状态
三个节点正常启动后,进入客户端,创建znode,看是否可以在节点间同步
root@ubuntu:/usr/local/zookeeper/bin# ./zkCli.sh -server master:2181
Connecting to localhost:2181
2019-03-26 07:43:11,989 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.11-
Welcome to ZooKeeper!
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] create /data test-data
Created /data
[zk: localhost:2181(CONNECTED) 2] ls /
[zookeeper, data]
[zk: localhost:2181(CONNECTED) 3] quit
Quitting...
root@ubuntu:/usr/local/zookeeper/bin# ./zkCli.sh -server master:2182 #查看2182节点是否同步了2181创建的znode
Connecting to localhost:2182
Welcome to ZooKeeper!
[zk: localhost:2182(CONNECTED) 0] ls /
[zookeeper, data]
[zk: localhost:2182(CONNECTED) 1] get /data
test-data
[zk: localhost:2182(CONNECTED) 2] quit
Quitting...
root@ubuntu:/usr/local/zookeeper/bin# ./zkCli.sh -server localhost:2183 #查看节点数据是否同步到2183
[zk: localhost:2183(CONNECTED) 0] ls /
[zookeeper, data]
[zk: localhost:2183(CONNECTED) 1] get /data
test-data
[zk: localhost:2183(CONNECTED) 2] quit
查看主从信息
root@ubuntu:/usr/local# cd zookeeper/bin
root@ubuntu:/usr/local/zookeeper/bin# ./zkServer.sh status /usr/local/zookeeper/conf/zoo1.cfg
ZooKeeper JMX enabled by default
Mode: follower #从节点
root@ubuntu:/usr/local/zookeeper/bin# ./zkServer.sh status /usr/local/zookeeper/conf/zoo2.cfg
ZooKeeper JMX enabled by default
Mode: leader #主节点
root@ubuntu:/usr/local/zookeeper/bin# ./zkServer.sh status /usr/local/zookeeper/conf/zoo3.cfg
ZooKeeper JMX enabled by default
Mode: follower #从节点
root@ubuntu:/usr/local/zookeeper/bin#
测试选举
zoo2是master节点
root@ubuntu:/usr/local/zookeeper/bin# ./zkServer.sh status /usr/local/zookeeper-3.4.11/conf/zoo1.cfg
ZooKeeper JMX enabled by default
Mode: follower #zoo1是slave
root@ubuntu:/usr/local/zookeeper01/bin#./zkServer.sh status /usr/local/zookeeper-3.4.11/conf/zoo2.cfg
ZooKeeper JMX enabled by default
Mode: leader #zoo2是master
root@ubuntu:/usr/local/zookeeper/bin# ./zkServer.sh status /usr/local/zookeeper-3.4.11/conf/zoo3.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper02/bin/../conf/zoo.cfg
Mode: follower #zoo3是slave
关闭zoo2,重启就会发生一次选举
[root@localhost bin]# ./zkServer.sh stop /usr/local/zookeeper-3.4.11/conf/zoo2.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.11/conf/zoo2.cfg
Stopping zookeeper ... STOPPED
[root@localhost bin]# ./zkServer.sh start /usr/local/zookeeper-3.4.11/conf/zoo2.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.11/conf/zoo2.cfg
Starting zookeeper ... STARTED
查看节点状态
[root@localhost bin]# ./zkServer.sh status /usr/local/zookeeper-3.4.11/conf/zoo2.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.11/conf/zoo2.cfg
Mode: follower #从节点
[root@localhost bin]# ./zkServer.sh status /usr/local/zookeeper-3.4.11/conf/zoo1.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.11/conf/zoo1.cfg
Mode: follower #从节点
[root@localhost bin]# ./zkServer.sh status /usr/local/zookeeper-3.4.11/conf/zoo3.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.11/conf/zoo3.cfg
Mode: leader #新的主节点