我是靠谱客的博主 彪壮宝贝,最近开发中收集的这篇文章主要介绍ZooKeeper入门指南Apache ZooKeeper什么是ZooKeeper?基本概念下载安装配置开始使用,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Apache ZooKeeper

  • 官方文档

zk入门指南

阅读本文档能使您快速了解并使用ZooKeeper。 主要内容包含:

  • 了解ZooKeeper的相关的概念
  • 下载安装ZooKeeper服务
  • Zookeeper的配置、启动与客户端连接
  • 了解ZooKeeper的一些简单命令

什么是ZooKeeper?

ZooKeeper翻译成中文的意思是动物管理员,它是一个分布式服务框架,用于维护配置信息,提供统一命名,分布式状态同步,集群管理等服务。

基本概念

为了更快的掌握zookeeper,我们首先需要了解zookeeper中的一些基本概念,例如集群角色(leader、follower、observer),会话(session),数据节点(ZNode),状态信息,事务操作,事件监听器(watcher),ACL

集群角色

  • leader:负责进行投票的发起和决议,更新系统状态
  • follower:接收客户端请求,参与投票
  • observer:接收客户端请求,将写请求转发给leader,不参加投票过程,只同步leader的状态,observer的目的是为了扩展系统,提高读取速度

Session

session是指客户端和zookeeper服务器之间的一个TCP长连接。从第一次建立连接开始,客户端session会话生命周期就开始了。client可以通过心跳监测和服务器保持有效的会话,同时也可以通过该连接接收服务器的watch事件通知。

ZNode

zookeeper中的ZNode称为数据节点。是指数据模型中的数据单元。ZNode是一个树,由斜杠(/)分隔路径,例如 :/hbash/master/ 其中,hbashmaster分别都是一个ZNode,在自己的ZNode中保存自己的数据。可以简单的将ZNode理解为Unix的文件系统。

  • 3.1 节点类型

持久节点:必须主动删除才会失效

临时节点:会话失效时就会失效。

  • 3.2 版本

zookeeper会为每个ZNode维护一个Stat的数据结构,在该结构中记录了ZNode的三个版本

  1. version - 当前版本
  2. cversion - 当前ZNode的子节点的版本
  3. aversion - 当前ZNode的ACL版本 (ACL : 访问控制列表)

事务

在zookeeper中,把改变zookeeper服务器状态的操作称为事务操作。一般包括ZNode节点的创建与删除、数据内容的更新、客户端session的建立与失效等操作。每一个事务请求,zk都会为其分配一个全局的事务ID,用ZXID表示,通常是一个64位的数字(它高32位是epoch用来标识 leader关系是否改变,每次一个leader被选出来,它都会有一个新的epoch,标识当前属于那个leader的统治时期。低32位用于递增计数。)。每一个ZXID对应的一次更新操作,从这些ZXID中可以间接识别出zk处理这些事务操作的全局顺序一致性

Watcher

watcher即事件监听器,是zk中的一个重要的分布式协调的特性。zk允许用户在指定节点注册一些watcher,并在特定事件触发时,zk会将通知到指定客户端上。

ACL

ACL全称为access control lists ,是zk的一种权限控制策略。zk定义了如下5中权限:

  • create : 创建子节点的权限
  • read:获取节点数据和子节点列表
  • write:更新节点数据
  • delete:删除子节点
  • admain:设置节点ACL权限

下载

因为zookeeper依赖JDK,所以需要先下载安装JDK。 没有安装JDK的同学移步教程---->>> 一分钟搞定JDK的安装与配置

JDK8:JDK8的下载地址

Apache最新稳定版:zookeeper官网镜像地址

TIPS:选择版本下载一定要注意选择带bin的安装包,否则下载错文件可能导致zkServer.sh start无法启动 。

或点击直接下载:apache-zookeeper-3.6.2-bin.tar.gz

安装

下载好apache-zookeeper-3.6.2.tar.gz后,在根目录创建servers目录,我们稍后把zk安装到这里方便管理。

  1. 解压
  • 执行命令,tar -xvzf 解压,加-C ~/servers的作用是指定目录
tar -xvzf apache-zookeeper-3.6.2.tar.gz -C ~/servers/ ```

2. 为zk建立软连接
* 进入servers目录
```bash
[lb@centos-linux servers]$ ln -s apache-zookeeper-3.6.2 zookeeper
[lb@centos-linux servers]$ ls
apache-zookeeper-3.6.2  zookeeper
  • 到此为止,我们的zk就安装成功了。看一下目录结构
[lb@centos-linux zookeeper]$ pwd
/home/lb/servers/zookeeper
[lb@centos-linux zookeeper]$ ls
bin                         excludeFindBugsFilter.xml  pom.xml              zookeeper-compatibility-tests  zookeeper-recipes
checkstyle-simple.xml       Jenkinsfile                README.md            zookeeper-contrib              zookeeper-server
checkstyle-strict.xml       Jenkinsfile-PreCommit      README_packaging.md  zookeeper-docs
checkstyleSuppressions.xml  LICENSE.txt                zk-merge-pr.py       zookeeper-it
conf                        NOTICE.txt                 zookeeper-assembly   zookeeper-jute
dev                         owaspSuppressions.xml      zookeeper-client     zookeeper-metrics-providers
[lb@centos-linux zookeeper]$

配置

  1. 进入/zookeeper/conf/目录,拷贝zoo_sample.cfg 文件,拷贝为一个新的配置:zoo.cfg
cp zoo_sample.cfg zoo.cfg
  1. 编辑zoo.cfg,修改dataDir配置,wq保存并退出
dataDir=/home/lb/servers/zookeeper/data
  1. 返回zookeeper目录,新建data文件夹

-p, --parents 需要时创建目标目录的上层目录,但即使这些目录已存在也不当作错误处理 -v, --verbose 每次创建新目录都显示信息

mkdir -p -v data
  1. 配zk的环境变量
  • 编辑bash_profile ,新增如下配置:
export ZOOKEEPER_HOME=/home/lb/servers/zookeeper
PATH=$ZOOKEEPER_HOME/bin:$PATH
  • 最终的配置如下:
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
export JAVA_HOME=/home/lb/package/java
export ZOOKEEPER_HOME=/home/lb/servers/zookeeper

PATH=$PATH:$HOME/.local/bin:$HOME/bin
PATH=$JAVA_HOME/bin:$PATH
PATH=$ZOOKEEPER_HOME/bin:$PATH

export PATH
  • 使配置生效 source ~/.bash_profile

开始使用

启动zkServer

  • 执行命令 zkServer.sh start
# 查看zkServer有哪些命令
[lb@centos-linux conf]$ zkServer.sh
ZooKeeper JMX enabled by default
Using config: /home/lb/servers/zookeeper/bin/../conf/zoo.cfg
Usage: /home/lb/servers/zookeeper/bin/zkServer.sh [--config <conf-dir>] {start|start-foreground|stop|version|restart|status|print-cmd}
# 启动zk服务
[lb@centos-linux conf]$ zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/lb/servers/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[lb@centos-linux conf]$
# 查看zk服务状态
[lb@centos-linux conf]$ zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/lb/servers/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: standalone # 单机版
[lb@centos-linux conf]$

客户端连接zkServer

  • 直接执行zkCli.sh
zkCli.sh
Connecting to localhost:2181
2020-12-18 09:59:43,848 [myid:] - INFO  [main:Environment@98] - Client environment:zookeeper.version=3.6.2--803c7f1a12f85978cb049af5e4ef23bd8b688715, built on 09/04/2020 12:44 GMT
2020-12-18 09:59:43,850 [myid:] - INFO  [main:Environment@98] - Client environment:host.name=centos-linux.shared
2020-12-18 09:59:43,850 [myid:] - INFO  [main:Environment@98] - Client environment:java.version=1.8.0_271
2020-12-18 09:59:43,851 [myid:] - INFO  [main:Environment@98] - Client environment:java.vendor=Oracle Corporation
2020-12-18 09:59:43,851 [myid:] - INFO  [main:Environment@98] - Client environment:java.home=/home/lb/package/jdk1.8.0_271/jre
2020-12-18 09:59:43,851 [myid:] - INFO  [main:Environment@98] - Client environment:java.class.path=/home/lb/servers/zookeeper/bin/../zookeeper-server/target/classes:/home/lb/servers/zookeeper/bin/../build/classes:/home/lb/servers/zookeeper/bin/../zookeeper-server/target/lib/*.jar:/home/lb/servers/zookeeper/bin/../build/lib/*.jar:/home/lb/servers/zookeeper/bin/../lib/zookeeper-prometheus-metrics-3.6.2.jar:/home/lb/servers/zookeeper/bin/../lib/zookeeper-jute-3.6.2.jar:/home/lb/servers/zookeeper/bin/../lib/zookeeper-3.6.2.jar:/home/lb/servers/zookeeper/bin/../lib/snappy-java-1.1.7.jar:/home/lb/servers/zookeeper/bin/../lib/slf4j-log4j12-1.7.25.jar:/home/lb/servers/zookeeper/bin/../lib/slf4j-api-1.7.25.jar:/home/lb/servers/zookeeper/bin/../lib/simpleclient_servlet-0.6.0.jar:/home/lb/servers/zookeeper/bin/../lib/simpleclient_hotspot-0.6.0.jar:/home/lb/servers/zookeeper/bin/../lib/simpleclient_common-0.6.0.jar:/home/lb/servers/zookeeper/bin/../lib/simpleclient-0.6.0.jar:/home/lb/servers/zookeeper/bin/../lib/netty-transport-native-unix-common-4.1.50.Final.jar:/home/lb/servers/zookeeper/bin/../lib/netty-transport-native-epoll-4.1.50.Final.jar:/home/lb/servers/zookeeper/bin/../lib/netty-transport-4.1.50.Final.jar:/home/lb/servers/zookeeper/bin/../lib/netty-resolver-4.1.50.Final.jar:/home/lb/servers/zookeeper/bin/../lib/netty-handler-4.1.50.Final.jar:/home/lb/servers/zookeeper/bin/../lib/netty-common-4.1.50.Final.jar:/home/lb/servers/zookeeper/bin/../lib/netty-codec-4.1.50.Final.jar:/home/lb/servers/zookeeper/bin/../lib/netty-buffer-4.1.50.Final.jar:/home/lb/servers/zookeeper/bin/../lib/metrics-core-3.2.5.jar:/home/lb/servers/zookeeper/bin/../lib/log4j-1.2.17.jar:/home/lb/servers/zookeeper/bin/../lib/json-simple-1.1.1.jar:/home/lb/servers/zookeeper/bin/../lib/jline-2.14.6.jar:/home/lb/servers/zookeeper/bin/../lib/jetty-util-9.4.24.v20191120.jar:/home/lb/servers/zookeeper/bin/../lib/jetty-servlet-9.4.24.v20191120.jar:/home/lb/servers/zookeeper/bin/../lib/jetty-server-9.4.24.v20191120.jar:/home/lb/servers/zookeeper/bin/../lib/jetty-security-9.4.24.v20191120.jar:/home/lb/servers/zookeeper/bin/../lib/jetty-io-9.4.24.v20191120.jar:/home/lb/servers/zookeeper/bin/../lib/jetty-http-9.4.24.v20191120.jar:/home/lb/servers/zookeeper/bin/../lib/javax.servlet-api-3.1.0.jar:/home/lb/servers/zookeeper/bin/../lib/jackson-databind-2.10.3.jar:/home/lb/servers/zookeeper/bin/../lib/jackson-core-2.10.3.jar:/home/lb/servers/zookeeper/bin/../lib/jackson-annotations-2.10.3.jar:/home/lb/servers/zookeeper/bin/../lib/commons-lang-2.6.jar:/home/lb/servers/zookeeper/bin/../lib/commons-cli-1.2.jar:/home/lb/servers/zookeeper/bin/../lib/audience-annotations-0.5.0.jar:/home/lb/servers/zookeeper/bin/../zookeeper-*.jar:/home/lb/servers/zookeeper/bin/../zookeeper-server/src/main/resources/lib/*.jar:/home/lb/servers/zookeeper/bin/../conf:
2020-12-18 09:59:43,851 [myid:] - INFO  [main:Environment@98] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2020-12-18 09:59:43,852 [myid:] - INFO  [main:Environment@98] - Client environment:java.io.tmpdir=/tmp
2020-12-18 09:59:43,852 [myid:] - INFO  [main:Environment@98] - Client environment:java.compiler=<NA>
2020-12-18 09:59:43,852 [myid:] - INFO  [main:Environment@98] - Client environment:os.name=Linux
2020-12-18 09:59:43,852 [myid:] - INFO  [main:Environment@98] - Client environment:os.arch=amd64
2020-12-18 09:59:43,852 [myid:] - INFO  [main:Environment@98] - Client environment:os.version=3.10.0-327.el7.x86_64
2020-12-18 09:59:43,852 [myid:] - INFO  [main:Environment@98] - Client environment:user.name=lb
2020-12-18 09:59:43,852 [myid:] - INFO  [main:Environment@98] - Client environment:user.home=/home/lb
2020-12-18 09:59:43,852 [myid:] - INFO  [main:Environment@98] - Client environment:user.dir=/home/lb/servers/apache-zookeeper-3.6.2-bin/conf
2020-12-18 09:59:43,852 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.free=23MB
2020-12-18 09:59:43,853 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.max=228MB
2020-12-18 09:59:43,853 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.total=29MB
2020-12-18 09:59:43,857 [myid:] - INFO  [main:ZooKeeper@1006] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@1175e2db
2020-12-18 09:59:43,863 [myid:] - INFO  [main:X509Util@77] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2020-12-18 09:59:43,872 [myid:] - INFO  [main:ClientCnxnSocket@239] - jute.maxbuffer value is 1048575 Bytes
2020-12-18 09:59:43,881 [myid:] - INFO  [main:ClientCnxn@1716] - zookeeper.request.timeout value is 0. feature enabled=false
Welcome to ZooKeeper!
2020-12-18 09:59:43,892 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1167] - Opening socket connection to server localhost/127.0.0.1:2181.
2020-12-18 09:59:43,892 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1169] - SASL config status: Will not attempt to authenticate using SASL (unknown error)
2020-12-18 09:59:43,896 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@999] - Socket connection established, initiating session, client: /127.0.0.1:39308, server: localhost/127.0.0.1:2181
JLine support is enabled
2020-12-18 09:59:43,923 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1433] - Session establishment complete on server localhost/127.0.0.1:2181, session id = 0x10000fc83a10000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]

[zk: localhost:2181(CONNECTED) 0]说明连接成功了!

一些简单命令

查看节点目录

命令:ls <节点名称>

  • 查看根目录ZNode
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
  • 查看zookeeper节点下有哪些节点
[zk: localhost:2181(CONNECTED) 1] ls /zookeeper
[config, quota]
  • 查看quota节点的子节点,结果为[ ] 说明没有子节点
[zk: localhost:2181(CONNECTED) 8]  ls /zookeeper/quota
[]
查看节点属性

命令:get -s <节点名称>

  • 查看zookeeper节点有哪些属性
[zk: localhost:2181(CONNECTED) 17] get -s /zookeeper
                      # 节点内容,这里为空
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = -2
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 2
  • 查看quota数据节点有哪些属性
[zk: localhost:2181(CONNECTED) 21] get -s /zookeeper/quota

cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 0
添加数据节点

命令:create <数据节点路径及名称> <数据节点内容>

  • 在根目录添加一个home节点,节点内容为123
[zk: localhost:2181(CONNECTED) 22] create /home 123
Created /home
#查看home节点属性
[zk: localhost:2181(CONNECTED) 23] get -s /home 
123 #节点内容
cZxid = 0x2
ctime = Fri Dec 18 10:23:11 CST 2020
mZxid = 0x2
mtime = Fri Dec 18 10:23:11 CST 2020
pZxid = 0x2
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
修改数据节点

命令:set <数据节点名称> <数据内容>

  • 修改数据节点home的值为123456
[zk: localhost:2181(CONNECTED) 24] set /home 123456
[zk: localhost:2181(CONNECTED) 25] get -s /home
123456
cZxid = 0x2
ctime = Fri Dec 18 10:23:11 CST 2020
mZxid = 0x3 #mZxid+1
mtime = Fri Dec 18 10:25:46 CST 2020 # 时间更新
pZxid = 0x2
cversion = 0
dataVersion = 1 #版本+1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6 #数据长度更新
numChildren = 0
删除数据节点

语法:delete <数据节点路径>

tips:只有没有子节点的节点才允许删除,例如如下案例:

  • home节点下创建了一个n1节点
[zk: localhost:2181(CONNECTED) 36] create /home/n1 ''
Created /home/n1
[zk: localhost:2181(CONNECTED) 38] ls /home
[n1]
  • 删除home节点,会提示Node not empty: /home
[zk: localhost:2181(CONNECTED) 40] delete /home
Node not empty: /home
  • 删除n1节点,没有问题,删除成功
[zk: localhost:2181(CONNECTED) 41] delete /home/n1
[zk: localhost:2181(CONNECTED) 42] ls /home
[]

恭喜你!关于zookeeper的简单入门就到到此为止啦!

如果想了解更多关于ZooKeeper的高阶知识,请阅读:

  • ZooKeeper的核心原理
  • ZooKeeper的集群安装与配置

最后

以上就是彪壮宝贝为你收集整理的ZooKeeper入门指南Apache ZooKeeper什么是ZooKeeper?基本概念下载安装配置开始使用的全部内容,希望文章能够帮你解决ZooKeeper入门指南Apache ZooKeeper什么是ZooKeeper?基本概念下载安装配置开始使用所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部