zookeeper 是一个分布式协调系统
下载链接:http://mirror.cogentco.com/pub/apache/zookeeper/
1.zookeeper集群结构
(1) leader
是zk集群的主节点,客户端向zk注册数据时,都要通过leader来对整个集群中的所有从节点做数据同步
(2)follower
是zk集群的从节点,保留数据,接收leader的请求,参与leader的选举
(3)observer
是zk集群的从节点,保留数据,接收leader的请求,不参与leader的选举
2.zookeeper的选举机制
(1)集群启动的时候
server1启动,先查看集群中有没有leader,如果没有,则选举自己为leader;
server2启动,先查看集群中有没有leader,如果没有,则选举自己为leader,
第二轮投票,server1j和server2投id较大为leader, server2当选为leader;
(2)集群运行的时候
运行中,如果leader宕机,剩余的机器会进入选举状态,重新选举的依据:
优先考虑节点所持有的数据的版本号,最新的作为leader;
如果每个节点的数据都一样新,那就选举id大的为leader;
3.安装配置
解压安装
[root@Darren2 zookeeper-3.4.10]# tar -zxvf zookeeper-3.4.10.tar.gz
[root@Darren2 zookeeper-3.4.10]# mkdir -p /usr/local/zookeeper-3.4.10/data
[root@Darren2 zookeeper-3.4.10]# echo 1 > data/myid
修改配置文件
其中server.1中的1对应文件myid内容,每个zookeeper都要有myid文件
[root@Darren2 zookeeper-3.4.10]# cd /usr/local/zookeeper-3.4.10/conf/
[root@Darren2 conf]# cp zoo_sample.cfg zoo.cfg
[root@Darren2 conf]# vim zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper-3.4.10/data
clientPort=2181
server.1=Darren2:2888:3888
server.2=Darren3:2888:3888
server.3=Darren4:2888:3888
#启动zookeeper
[root@Darren2 bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper … STARTED
[root@Darren2 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: Leader
[root@Darren3 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: Follower
4.zookeeper命令
zookeeper中数据本质是key-value,zookeeper中一个数据被称为一个znode,一个znode不能太大,通常在10K以内,官方要求最大不要超过1M,如果太大,会导致zookeeper集群个节点的数据无法实时同步 ,数据保持一致性。
key使用路径表示,如:/dir1 value
znode类型
(1) persistent:默认的节点类型,一旦创建就会一直存在,除非手动删除数据;
(2)ephemeral:短暂节点,创建该数据节点的客户端如果和zk服务失去联系,该数据节点会被zk服务自动删除;
(3)sequential:带自增序号的节点,在同一个节点下创建sequential子节点,zk会给子节点名字自动拼接一个自增的序列号;
#登录zk client
[root@Darren2 bin]# ./zkCli.sh
[zk: localhost:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
stat path [watch]
set path data [version]
ls path [watch]
delquota [-n|-b] path
ls2 path [watch]
setAcl path acl
setquota -n|-b val path
history
redo cmdno
printwatches on|off
delete path [version]
sync path
listquota path
rmr path
get path [watch]
create [-s] [-e] path data acl
addauth scheme auth
quit
getAcl path
close
connect host:port
[zk: localhost:2181(CONNECTED) 13] create /dir1 a1
[zk: localhost:2181(CONNECTED) 16] ls /
[dir1, zookeeper]
[zk: localhost:2181(CONNECTED) 15] get /dir1
a1
cZxid = 0x7
ctime = Sun Nov 26 13:05:34 CST 2017
mZxid = 0x7
mtime = Sun Nov 26 13:05:34 CST 2017
pZxid = 0x7
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 0
#创建短暂的znode,一旦quit客户端,则自动删除
[zk: localhost:2181(CONNECTED) 17] create -e /dir2 b1
#创建序列znode
[zk: localhost:2181(CONNECTED) 1] create -s /dir3 c1
Created /dir30000000003
[zk: localhost:2181(CONNECTED) 2] ls /
[dir1, zookeeper, dir30000000003]
[zk: localhost:2181(CONNECTED) 3] create -s /dir3 c1
Created /dir30000000004
[zk: localhost:2181(CONNECTED) 4] ls /
[dir1, zookeeper, dir30000000003, dir30000000004]
#创建短暂序列znode
[zk: localhost:2181(CONNECTED) 5] create -s -e /dir3 c1
#事件监听
[zk: localhost:2181(CONNECTED) 4] ls /dir1 watch
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/dir1
5.zookeeper的客户端api的基本使用
package zkdemo1;
import org.apache.zookeeper.ZooKeeper;
public class TestConnection {
public static void main(String[] args) throws Exception{
ZooKeeper zk = new ZooKeeper("192.168.163.102", 2000, null);
byte[] data = zk.getData("/dir1", false, null);
System.out.println(new String(data));
zk.close();
}
}
输出:
a1
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/195268.html