Zookeeper是什么
ZooKeeper 是 Apache 软件基金会的一个软件项目,为分布式应用提供协调服务,保证数据一致性。它为大型分布式计算提供开源的分布式配置管理服务、服务注册发现。
ZooKeeper 的架构通过冗余服务实现高可用性。
一个典型的分布式数据一致性的解决方案,分布式应用程序可以基于它实现诸如数据发布/订阅、负载均衡、命名服务、分布式协调/通知、集群管理、Master 选举、分布式锁和分布式队列等功能。
 
CAP理论
CAP 理论指出对于一个分布式计算系统来说,不可能同时满足以下三点:
- 一致性:在分布式环境中,一致性是指数据在多个副本之间是否能够保持一致的特性,等同于所有节点访问同一份最新的数据副本。在一致性的需求下,当一个系统在数据一致的状态下执行更新操作后,应该保证系统的数据仍然处于一致的状态。
- 可用性:每次请求都能获取到正确的响应,但是不保证获取的数据为最新数据。
- 分区容错性:分布式系统在遇到任何网络分区故障的时候,仍然需要能够保证对外提供满足一致性和可用性的服务,除非是整个网络环境都发生了故障。
一个分布式系统最多只能同时满足一致性(Consistency)、可用性(Availability)和分区容错性(Partition tolerance)这三项中的两项。
在这三个基本需求中,最多只能同时满足其中的两项,P 是必须的,因此只能在 CP 和 AP 中选择,zookeeper 保证的是 CP,对比 spring cloud 系统中的注册中心 eruka 实现的是 AP。

Zookeeper安装
zookeeper 下载地址为: https://zookeeper.apache.org/releases.html。  下载后解压:  将 conf 目录下的 zoo_sample.cfg 文件,复制一份,重命名为 zoo.cfg: 
在安装目录下面新建一个空的 data 文件夹: 
修改 zoo.cfg 配置文件,将 dataDir=/tmp/zookeeper 修改成 zookeeper 安装目录所在的 data 文件夹:

启动程序: 
启动客户端: 
查看启动状态: 
搭建Zookeeper集群

zookeeper 的三个端口作用
1、2181 : 对 client 端提供服务 2、2888 : 集群内机器通信使用 3、3888 : 选举 leader 使用
三台zookeeper修改配置文件:  在三台zookeeper的data目录下新建myid文件,并添加zookeeper标识号:  查看状态:   
数据模型

节点类型
1.持久化节点 2.持久化顺序节点 -s 3.临时节点 -e 4.临时顺序节点 -e -s
客户端命令
ls命令
ls 命令用于查看某个路径下目录列表。
格式:
ls path

ls2命令

create 命令
create 命令用于创建节点并赋值。
格式:
create [-s] [-e] path data acl

get命令
get 命令用于获取节点数据和状态信息。
格式:
get path [watch] 
set命令
set 命令用于修改节点存储的数据。
格式:
set path data [version]

delete命令
delete 命令用于删除某节点。
格式:
delete path [version]

deleteall命令
deleteall 命令用于删除带子节点的节点。
deleteall path
Zookeeper 节点特性
1、同一级节点 key 名称是唯一的 2、创建节点时,必须要带上全路径 3、session 关闭,临时节点清除 4、自动创建顺序节点 5、watch 机制,监听节点变化 6、delete 命令只能一层一层删除
|