zk的单机部署,与客户端的使用
阅读原文时间:2023年07月12日阅读:1

下载zk

wget https://archive.apache.org/dist/zookeeper/stable/apache-zookeeper-3.5.5-bin.tar.gz

安装jdk

tar xf jdk-12.0.2_linux-x64_bin.tar.gz -C /usr/local/
vim /etc/profile.d/jdk.sh
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH
ln -s /usr/local/jdk-12.0.2 /usr/local/jdk

部署zk

mkdir /data/zk -pv
tar xf apache-zookeeper-3.5.5-bin.tar.gz -C /data/zk/
cp /data/zk/apache-zookeeper-3.5.5-bin/bin/../conf/zoo_sample.cfg /data/zk/apache-zookeeper-3.5.5-bin/bin/../conf/zoo.cfg
创建数据目录 mkdir /data/zk/data
[root@master conf]# vim zoo.cfg

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=/data/zk/data 数据目录

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

  配置zk的环境变量

[root@master conf]# vim /etc/profile.d/zk.sh

export ZOOKEEPER_HOME=/data/zk/apache-zookeeper-3.5.5-bin
export ZOOBINDIR=/data/zk/apache-zookeeper-3.5.5-bin/bin
export PATH=$ZOOKEEPER_HOME/bin:$PATH

[root@master conf]# exec bash

  启动

[root@master conf]# zkServer.sh start

  客户端登录

[root@master conf]# zkCli.sh
[zk: localhost:2181(CONNECTED) 0] help 帮助
ZooKeeper -server host:port cmd args
addauth scheme auth
close
config [-c] [-w] [-s]
connect host:port
create [-s] [-e] [-c] [-t ttl] path [data] [acl]
delete [-v version] path
deleteall path
delquota [-n|-b] path
get [-s] [-w] path
getAcl [-s] path
history
listquota path
ls [-s] [-w] [-R] path
ls2 path [watch]
printwatches on|off
quit
reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,…]*]] | [-add serverId=host:port1:port2;port3[,…]]* [-remove serverId[,…]*]
redo cmdno
removewatches path [-c|-d|-a] [-l]
rmr path
set [-s] [-v version] path data
setAcl [-s] [-v version] [-R] path acl
setquota -n|-b val path
stat [-w] path
sync path
Command not found: Command not found help
[zk: localhost:2181(CONNECTED) 1] create /app4 创建node
Created /app4
[zk: localhost:2181(CONNECTED) 2] create /app4/p-1
Created /app4/p-1
[zk: localhost:2181(CONNECTED) 3] create /app4/p-2
Created /app4/p-2
[zk: localhost:2181(CONNECTED) 4] create /app4/p-3
Created /app4/p-3
[zk: localhost:2181(CONNECTED) 5] ls -R / 查看node
/
/app1
/app2
/app4
/zookeeper
/app1/p_1
/app1/p_2
/app1/p_3
/app4/p-1
/app4/p-2
/app4/p-3
/zookeeper/config
/zookeeper/quota

  客户端创建分布式锁

[zk: localhost:2181(CONNECTED) 6] create -e /log
Created /log
[root@master data]# zkCli.sh
[zk: localhost:2181(CONNECTED) 2] create -e /log
Node already exists: /log
[zk: localhost:2181(CONNECTED) 3] stat -w /log 监控这个锁
cZxid = 0x14
ctime = Wed Sep 11 16:31:46 CST 2019
mZxid = 0x14
mtime = Wed Sep 11 16:31:46 CST 2019
pZxid = 0x14
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x100014034170002
dataLength = 0
numChildren = 0
退出第一个客户端;释放锁
[zk: localhost:2181(CONNECTED) 7] quit

WATCHER::

WatchedEvent state:Closed type:None path:null
2019-09-11 16:35:02,053 [myid:] - INFO [main:ZooKeeper@1422] - Session: 0x100014034170002 closed
2019-09-11 16:35:02,053 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@524] - EventThread shut down for session: 0x100014034170002
释放锁后
[zk: localhost:2181(CONNECTED) 4]
WATCHER::

WatchedEvent state:SyncConnected type:NodeDeleted path:/log

[zk: localhost:2181(CONNECTED) 4] create -e /log 第二的
Created /log

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章