1.master节点选举
,主节点挂了以后,从节点就会接手工作,并且保证这个节点是唯一的,这也是所谓的首脑模式,从而保证我们的集群是高可用的。
2.统一配置文件管理
,即只需要部署一台服务器,则可以把相同的配网文件同步更新到其他所有服务器,此操作在云计算中用的特别多。
3.发布与订阅
类似消息队列的MQ(amq,rmq...),dubbo发布者把数据存在znode上,订阅者会读取这个数据。
4. 提供分布式锁
分布式环境中不同进程之间争夺资源,类似于多线程中的锁。
5 集群管理
集群中保证数据的强一致性。
下载地址https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/
wget http://mirrors.shu.edu.cn/apache/zookeeper/stable/zookeeper-3.4.12.tar.gz
解压安装
tar -zxvf zookeeper-3.4.12.tar.gz -C /usr/local/
配置全局变量
vim .bash_profile
增加ZK_HOME
export ZK_HOME=/usr/local/zookeeper-3.4.12
export PATH=$ZK_HOME/bin:$PATH
立即生效
source .bash_profile
mac os 系统可以直接 brew reinstall zookeeper
brew reinstall zookeeper
....
To have launchd start zookeeper now and restart at login:
brew services start zookeeper
Or, if you don't want/need a background service you can just run:
zkServer start
==> Summary
? /usr/local/Cellar/zookeeper/3.4.13: 244 files, 33.4MB
目录结构
$ tree -d -L 1
.
├── bin
├── include
├── lib
└── libexec
4 directories
bin 主要运行命令 conf 存放配置文件
# 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=/usr/local/var/run/zookeeper
dataLogDir=/usr/local/var/log/zookeeper
# 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
tickTime:基本事件单元,以毫秒为单位,这个时间作为 Zookeeper 服务器之间或客户端之间维持心跳的时间间隔 dataDir:存储内存中数据库快照的位置,顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存到这个目录里 clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求 initLimit:这个配置项是用来配置 Zookeeper 接受客户端初始化连接时最长能忍受多少个心跳时间间隔 当已经超过 10 个心跳的时间也就是(ticktime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败,总的时间长度就是:102000 = 20s syncLimit:这个配置项表示 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是:52000 = 10s server.A = B:C:D A:表示这是第几号服务器 B:服务器的 IP 地址 C:服务器与集群中的 Leader 服务器交换信息的端口 D:一旦集群中的 Leader 服务器挂了,需要一个端口重新进行选举,选出一个新的 Leader 2181:对外提供端口 2888:内部同步端口 3888:节点挂了,选举端口
修改zookerper 配置文件,修改数据存储目录 默认路径是tmp目录 需要修改
$cd $ZK_HOME/conf
$cp zoo_sample.cfg zoo.cfg
$vim zoo.cfg
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/local/var/run/zookeeper/data
dataLogDir=/usr/local/var/log/zookeeper
删除zookeeper下bin的Windows下以cmd结尾的文件
-rwxr-xr-x 1 baxiang baxiang 232 Mar 27 12:32 README.txt
-rwxr-xr-x 1 baxiang baxiang 1937 Mar 27 12:32 zkCleanup.sh
-rwxr-xr-x 1 baxiang baxiang 1534 Mar 27 12:32 zkCli.sh
-rwxr-xr-x 1 baxiang baxiang 2696 Mar 27 12:32 zkEnv.sh
-rwxr-xr-x 1 baxiang baxiang 6773 Mar 27 12:32 zkServer.sh
开启zookeeper
$ ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.12/bin/../conf/zoo.cfg
查看zookeeper状态
zkServer status
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Mode: standalone
Zookeeper是树形结构,类似于Linux的文件目录。 每一个节点都称之为znode,它可以有子节点,也可以有数据。 每个节点分为临时节点和永久节点,临时节点在客户端断开后消失。 每个zk节点都各自的版本号,可以通过命令行来显示节点信息。 每当节点数据发生变化,那么该节点的版本号version会累加(乐观锁)。 删除/修改过时的节点,版本号不匹配则会报错。 每个zk节点存储的数据不宜过大,几K即可。 节点可以设置权限acl,可以通过权限来限制用户访问
./zkCli.sh
Connecting to localhost:2181
ls /
[zookeeper]
创建节点
create /zk_test test_data
Created /zk_test
[zk: localhost:2181(CONNECTED) 3] ls /
[zookeeper, zk_test]
删除节点
delete /zk_test