ZooKeeper是一个集中式服务,用于维护配置信息、命名、提供分布式同步和提供组服务。所有这些类型的服务都以某种形式被分布式应用程序使用。每次实现它们时,都有大量的工作要去修复不可避免的bug和争用条件。由于实现这些类型的服务的难度,应用程序最初通常会对这些服务略施小计,这会使它们在发生变化时变得脆弱,并且难以管理。即使正确地执行这些服务,这些服务的不同实现也会导致部署应用程序时的管理复杂性。
一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chuby一个开源的实现,是Hadoop和Hbase的重要组件。ZooKeeper使用Java所编写,但是支持Java和C两种编程语言。
https://zookeeper.apache.org/
节点在被创建后就一直存在直到主动删除(即使客户端消失也存在)
节点在被创建后就一直存在直到主动删除,在ZK中,每个父节点会为他的第一级子节点维护一份时序,会记录每个子节点创建的先后顺序。基于这个特性,在创建子节点的时候,可以设置这个属性,那么在创建节点过程中,ZK会自动为给定节点名加上一个数字后缀,作为新的节点名。这个数字后缀的范围是整型的最大值。
生命周期和客户端一致,客户端消亡,节点自动清除且不能有子节点
生命周期和客户端一致,客户端消亡,节点自动清除且不能有子节点,每个父节点会为他的第一级子节点维护一份时序
我使用的docker安装方式
docker search zookeeper
//安装Java
docker pull adoptopenjdk/openjdk8
//安装zookeeper
docker pull zookeeper
docker run --name zk -p 2181:2181 -d zookeeper
docker exec -it 4284ec625c83 bash
找到bin目录下的zkCli.sh
启动
./zkCli.sh
# 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=/tmp/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## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
意思是指定/root/dockerfiles/zookeeperconf
目录作为zookeeper的conf目录
docker run --name zk -p 2181:2181 -v /root/dockerfiles/zookeeperconf:/conf -d zookeeper
./zkCli.sh -server 地址:2181
ls /
ls /节点名称
create -节点类型 /节点名称 数据
//持久节点
create -p /node1 1
//持久顺序
create -s /node2 2
//临时
create -e /node3 3
//临时顺序
create -s -e /node4 4
delete /节点名称
递归删除全部
rmr /节点名称
quit
stat /节点名称
get /节点名称
set /节点名称 数据