zookeeper
1.ZooKeeper是一个高可用的分布式系统协调框架,保证了分布式环境中数据的强一致性。当部署了多台应用服务器的时候,需要能够做到在一台机器上修改配置文件或者变量,然后在同步到所有应用服务器
2.用途:配置中心(发布订阅)、注册中心(ip,端口,服务名提交给zookeeper,维护服务列表,客户端拉取缓存到本地),分布式锁
3.特性
-
顺序一致性 - 客户端的更新将按发送顺序进行应用。
-
原子性 - 更新要么成功,要么失败。没有部分结果。
-
单一系统映像 - 客户将看到相同的服务视图,无论它连接到的服务器。
-
可靠性 - 一旦应用了更新,它将一直持续到客户端覆盖更新。
-
及时性 - 保证系统的客户视图在一定时间内更新。不能保证两个客户端能及时获得刚刚更新的数据,读数据前调用sync() 接口
4.zookeeper角色
leader:一个集群有且只有一个leader节点,处理写请求,并负责进行发起投票和决议,更新系统状态。每次 处理写请求的时候都会发起投票,只有过半的节点通过才能写入数据。
follower:用来处理读请求,leader会根据算法落实到某个follower节点。follower除了有投票权还具有选举权,当leader挂掉之后,follower之间就会发起投票,选举出下一任的leader。
observer:用来处理读请求,可以当成没有投票和选举资格的follower。其存在的目的就是为了协助follower来处理读请求。因为一个集群只有leader和follower的话每次节点变化都需要投票,这是很耗时间的;有这么一个只干活不投票的打酱油角色可以提高读取的吞吐量。
5.投票: zookeeper 集群有半数以上的服务器启动集群才能正常工作
leader选出来后,新加入的服务都是follower
投票信息包含(zxid,myid),zxid大的胜出,相同再比较myid
zxid 64为,高32位epoch(每次选举出新的leader+1),低32位事务id(节点数据变化+1)
6.节点类型:临时节点(客户端和服务端断开连接则消失),永久节点只能通过delete删除。
顺序节点 test0000000002, test0000000003, test0000000005
create -e -s /test (-e 临时节点,-s 顺序节点)
7.session:客户端连接服务端会产生一个session
8.持久化
9.api 常用的响应式编程,watch监听事件发生,如果有事件发生调用callback回调方法
10.分布式锁:多个客户端创建临时顺序节点,临时节点不会被覆盖,比较编号的大小,获得锁
11.配置文件 zoo.cfg 2888是数据同步端口,3888是选举端口
# 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=/home/cwy/zookeeper_date
# 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
server.1=192.168.1.102:2888:3888
server.2=192.168.1.103:2888:3888
server.3=192.168.1.104:2888:3888
server.4=192.168.1.105:2888:3888
============
安装:
解压,conf目录,复制zoo.cfg,设置次持久化目录,在目录里面准备myid文件,配置环境变量
zkServer.sh 启动
zkCli.sh 连接
目录树结构,create 创建节点 create -e 临时节点,客户端退出消失
help 帮助命令
==================