zk的单机部署,与客户端的使用
下载zk
1 | wget https: //archive .apache.org /dist/zookeeper/stable/apache-zookeeper-3 .5.5-bin. tar .gz |
安装jdk
1 2 3 4 5 6 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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的环境变量
1 2 3 4 5 6 7 | [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 |
启动
1 | [root@master conf] # zkServer.sh start |
客户端登录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | [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 |
客户端创建分布式锁
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | [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 |
草都可以从石头缝隙中长出来更可况你呢
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏