致谢:https://www.bilibili.com/video/BV1PW411r7iP?from=search&seid=4125939798101478321
QUESTION:我的ZK是3.5.9版本,会自动占用8080端口,但是我之前起了hadoop等任务,该端口被占用了,在zoo.cfg文件中配置
admin.serverPort=8887
(保证8887没被占用可以查询一下)
即可
一、Zookeeper是一个开源的分布式,为分布式提供协调应用的Apache项目
观察者模式设计的分布式管理框架:负责存储和管理大家都关心的数据,然后接受观察者的注册,一旦数据状态发生变化,Zookeeper负责通知注册了的接收者。
ZK=文件系统+通知机制
二、Zookeeper特点:
1.zookeeper中一个领导者,多个跟随者。
2.集群中有半数以上节点存活,Zookeeper集群能正常服务
3.全局数据一致,每个server存储存储一些核心数据,数据量并不大。
4.同一个客户端的更新请求按顺序执行
5.数据更新原子性,要么成功要么失败
6.实时性
三、数据结构
Zookeeper数据模型与Unix文件系统类似,每个Znode默认存储1MB的数据
四、应用场景
统一命名管理
统一配置管理
同意集群管理:可以根据节点实时状态进行挑战,可以将节点信息写入一个Znode,监听这个Znode获取实时变化
服务器动态上下线:服务器上下线客户端都可以接收
软负载均衡:记录每台服务器的访问数,让访问数最少的服务器去处理最新的客户端请求。
四、分布式安装部署
每台机器都要进行
1.下载:https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.5.9/apache-zookeeper-3.5.9-bin.tar.gz
2.解压到/opt/目录 sudo tar -zxvf apache-zookeeper-3.5.9-bin.tar.gz -C /opt/
3.配置服务器编号
3.1 在zookeeper中创建 zkData文件夹,创建myid文件
3.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/apache-zookeeper-3.5.9-bin/zkData
# the port at which the clients will connect
clientPort=2181
admin.serverPort=8887
# 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
##############################cluster##########################
server.2=master:2888:3888
server.3=slave1:2888:3888
server.4=slave2:2888:3888
2888端口交换zookeeper的数据信息,3888端口交换leader挂了之后的选举信息。
3.3启动
bin/zkServer.sh start
五、创建客户端
./bin/zkCli.sh
[zk: localhost:2181(CONNECTED) 4] create /sanguo "jinlian"
Created /sanguo
[zk: localhost:2181(CONNECTED) 5] create /sanguo/shuoguo "liubei"
Created /sanguo/shuoguo
[zk: localhost:2181(CONNECTED) 6] ls /sanguo
[shuoguo]
[zk: localhost:2181(CONNECTED) 7] get /sanguo/shuoguo
liubei
[zk: localhost:2181(CONNECTED) 8] create /sanguo/wuguo "sunquan"
Created /sanguo/wuguo
[zk: localhost:2181(CONNECTED) 9] create -e /sanguo/wuguo "sunquan"
Node already exists: /sanguo/wuguo
[zk: localhost:2181(CONNECTED) 10] create -e /sanguo/wuguo "zhouyu"
Node already exists: /sanguo/wuguo
[zk: localhost:2181(CONNECTED) 11] create -e /sanguo/hanguo "zhouyu" #短暂节点
Created /sanguo/hanguo
[zk: localhost:2181(CONNECTED) 12] quit
[zk: localhost:2181(CONNECTED) 1] create -s /sanguo/weiguo "caocao" #带序号
Created /sanguo/weiguo0000000003
[zk: localhost:2181(CONNECTED) 4] set /sanguo/shuoguo "diaochan" # 修改数据值
[zk: localhost:2181(CONNECTED) 5] get /sanguo/shuoguo
diaochan
[zk: localhost:2181(CONNECTED) 20] get -s -w /sanguo #监听节点
jinlian
cZxid = 0x100000047
ctime = Wed Apr 14 23:44:40 PDT 2021
mZxid = 0x100000047
mtime = Wed Apr 14 23:44:40 PDT 2021
pZxid = 0x100000051
cversion = 7
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 5
[zk: localhost:2181(CONNECTED) 21]
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/sanguo
[zk: localhost:2181(CONNECTED) 16] ls -w /sanguo #监听ls
[shuoguo, weiguo0000000003, weiguo0000000004, weiguo0000000005, wuguo]
[zk: localhost:2181(CONNECTED) 17]
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/sanguo
[zk: localhost:2181(CONNECTED) 3] delete /sanguo/meiguo #删除
[zk: localhost:2181(CONNECTED) 4]
六、监听器原理
https://www.bilibili.com/video/BV1PW411r7iP?p=14&spm_id_from=pageDriver
6)listener线程内部调用了process() 程序员编写
七、写数据流程
leader server 同步
八、Zookeeper实战
mark 用的时候再写
https://www.bilibili.com/video/BV1PW411r7iP?p=16&spm_id_from=pageDriver