码迷,mamicode.com
首页 > 其他好文 > 详细

zookeeper的单机部署与客户端使用锁

时间:2020-06-19 11:44:58      阅读:40      评论:0      收藏:0      [点我收藏+]

标签:tick   ssi   $path   classes   profile   shu   ctime   ack   ide   

jdk 的配置

tar xf jdk-8u161-linux-x64.tar.gz -C /usr/local/
mv /usr/local/{jdk1.8.0_161,jdk}
tar xf zookeeper-3.4.14.tar.gz -C /usr/local/
mv  /usr/local/{zookeeper-3.4.14,zookeeper}
cat /etc/profile.d/java.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:/usr/local/zookeeper/bin
exec bash

zk 的配置与启动

 cp zoo_sample.cfg zoo.cfg 

[root@es ~]# mkdir -pv /zookeeper/data    创建数据目录
mkdir: 已创建目录 "/zookeeper"
mkdir: 已创建目录 "/zookeeper/data"
[root@es conf]# cat 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=/zookeeper/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
[root@es ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Usin config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
 

  客户端的使用

[root@es ~]# zkCli.sh 
Connecting to localhost:2181
2020-06-04 17:47:58,152 [myid:] - INFO  [main:Environment@109] - Client environment:zookeeper.version=3.5.5-390fe37ea45dee01bf87dc1c042b5e3dcce88653, built on 05/03/2019 12:07 GMT
2020-06-04 17:47:58,161 [myid:] - INFO  [main:Environment@109] - Client environment:host.name=es
2020-06-04 17:47:58,161 [myid:] - INFO  [main:Environment@109] - Client environment:java.version=1.8.0_161
2020-06-04 17:47:58,167 [myid:] - INFO  [main:Environment@109] - Client environment:java.vendor=Oracle Corporation
2020-06-04 17:47:58,168 [myid:] - INFO  [main:Environment@109] - Client environment:java.home=/usr/local/jdk/jre
2020-06-04 17:47:58,168 [myid:] - INFO  [main:Environment@109] - Client environment:java.class.path=/usr/local/zookeeper/bin/../zookeeper-server/target/classes:/usr/local/zookeeper/bin/../bui
ld/classes:/usr/local/zookeeper/bin/../zookeeper-server/target/lib/*.jar:/usr/local/zookeeper/bin/../build/lib/*.jar:/usr/local/zookeeper/bin/../lib/zookeeper-jute-3.5.5.jar:/usr/local/zookeeper/bin/../lib/zookeeper-3.5.5.jar:/usr/local/zookeeper/bin/../lib/slf4j-log4j12-1.7.25.jar:/usr/local/zookeeper/bin/../lib/slf4j-api-1.7.25.jar:/usr/local/zookeeper/bin/../lib/netty-all-4.1.29.Final.jar:/usr/local/zookeeper/bin/../lib/log4j-1.2.17.jar:/usr/local/zookeeper/bin/../lib/json-simple-1.1.1.jar:/usr/local/zookeeper/bin/../lib/jline-2.11.jar:/usr/local/zookeeper/bin/../lib/jetty-util-9.4.17.v20190418.jar:/usr/local/zookeeper/bin/../lib/jetty-servlet-9.4.17.v20190418.jar:/usr/local/zookeeper/bin/../lib/jetty-server-9.4.17.v20190418.jar:/usr/local/zookeeper/bin/../lib/jetty-security-9.4.17.v20190418.jar:/usr/local/zookeeper/bin/../lib/jetty-io-9.4.17.v20190418.jar:/usr/local/zookeeper/bin/../lib/jetty-http-9.4.17.v20190418.jar:/usr/local/zookeeper/bin/../lib/javax.servlet-api-3.1.0.jar:/usr/local/zookeeper/bin/../lib/jackson-databind-2.9.8.jar:/usr/local/zookeeper/bin/../lib/jackson-core-2.9.8.jar:/usr/local/zookeeper/bin/../lib/jackson-annotations-2.9.0.jar:/usr/local/zookeeper/bin/../lib/commons-cli-1.2.jar:/usr/local/zookeeper/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/zookeeper/bin/../zookeeper-*.jar:/usr/local/zookeeper/bin/../zookeeper-server/src/main/resources/lib/*.jar:/usr/local/zookeeper/bin/../conf:.:/usr/local/jdk/lib/dt.jar:/usr/local/jdk/lib/tools.jar2020-06-04 17:47:58,168 [myid:] - INFO  [main:Environment@109] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2020-06-04 17:47:58,168 [myid:] - INFO  [main:Environment@109] - Client environment:java.io.tmpdir=/tmp
2020-06-04 17:47:58,168 [myid:] - INFO  [main:Environment@109] - Client environment:java.compiler=<NA>
2020-06-04 17:47:58,168 [myid:] - INFO  [main:Environment@109] - Client environment:os.name=Linux
2020-06-04 17:47:58,168 [myid:] - INFO  [main:Environment@109] - Client environment:os.arch=amd64
2020-06-04 17:47:58,169 [myid:] - INFO  [main:Environment@109] - Client environment:os.version=3.10.0-957.el7.x86_64
2020-06-04 17:47:58,169 [myid:] - INFO  [main:Environment@109] - Client environment:user.name=root
2020-06-04 17:47:58,169 [myid:] - INFO  [main:Environment@109] - Client environment:user.home=/root
2020-06-04 17:47:58,169 [myid:] - INFO  [main:Environment@109] - Client environment:user.dir=/root
2020-06-04 17:47:58,169 [myid:] - INFO  [main:Environment@109] - Client environment:os.memory.free=52MB
2020-06-04 17:47:58,172 [myid:] - INFO  [main:Environment@109] - Client environment:os.memory.max=228MB
2020-06-04 17:47:58,172 [myid:] - INFO  [main:Environment@109] - Client environment:os.memory.total=57MB
2020-06-04 17:47:58,178 [myid:] - INFO  [main:ZooKeeper@868] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWat
cher@ea307972020-06-04 17:47:58,188 [myid:] - INFO  [main:X509Util@79] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2020-06-04 17:47:58,207 [myid:] - INFO  [main:ClientCnxnSocket@237] -  
[zk: localhost:2181(CONNECTED) 2] ls  -R /
/
/zookeeper
/zookeeper/config
/zookeeper/quota
[zk: localhost:2181(CONNECTED) 3] 

[zk: localhost:2181(CONNECTED) 3] create /app1
Created /app1
[zk: localhost:2181(CONNECTED) 4] create /app2
Created /app2
[zk: localhost:2181(CONNECTED) 5] create /app2/p_1
Created /app2/p_1
[zk: localhost:2181(CONNECTED) 6] create /app2/p_2
Created /app2/p_2
[zk: localhost:2181(CONNECTED) 7] ls  -R /
/
/app1
/app2
/zookeeper
/app2/p_1
/app2/p_2
/zookeeper/config
/zookeeper/quota

   实现锁的

终端1
[zk: localhost:2181(CONNECTED) 8] create -e /log 
Created /log
终端2
[zk: localhost:2181(CONNECTED) 0] create -e /log 
Node already exists: /log   #创建失败
[zk: localhost:2181(CONNECTED) 1] stat -w /log   监控这个
cZxid = 0x7
ctime = Thu Jun 04 18:36:40 CST 2020
mZxid = 0x7
mtime = Thu Jun 04 18:36:40 CST 2020
pZxid = 0x7
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x1000036975a0000
dataLength = 0
numChildren = 0
终端1退出
[zk: localhost:2181(CONNECTED) 9] quit

WATCHER::

WatchedEvent state:Closed type:None path:null
2020-06-04 18:44:07,928 [myid:] - INFO  [main:ZooKeeper@1422] - Session: 0x1000036975a0000 closed
2020-06-04 18:44:07,929 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@524] - EventThread shut down for session: 0x1000036975a0000
终端2收到通知后创建
[zk: localhost:2181(CONNECTED) 2] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeDeleted path:/log

[zk: localhost:2181(CONNECTED) 2] create -e /log 
Created /log

 分布式锁的实现

 

终端1创建master-m1节点
[zk: localhost:2181(CONNECTED) 0] create -e /master "m1:2223"
Created /master
终端2 创建master-m2节点
[zk: localhost:2181(CONNECTED) 1] create -e /master "m2:2223"
Node already exists: /master
终端2监控master这个node节点
[zk: localhost:2181(CONNECTED) 2] stat -w /master
cZxid = 0xd
ctime = Fri Jun 05 06:30:33 CST 2020
mZxid = 0xd
mtime = Fri Jun 05 06:30:33 CST 2020
pZxid = 0xd
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x1000001d2cc0000
dataLength = 7
numChildren = 0
终端1退出;终端2收到通知
[zk: localhost:2181(CONNECTED) 1] quit

WATCHER::

WatchedEvent state:Closed type:None path:null
2020-06-05 06:35:57,874 [myid:] - INFO  [main:ZooKeeper@1422] - Session: 0x1000001d2cc0000 closed
2020-06-05 06:35:57,874 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@524] - EventThread shut down for session: 0x1000001d2cc0000
终端2收到通知后就可以继续创建
[zk: localhost:2181(CONNECTED) 3] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeDeleted path:/master

[zk: localhost:2181(CONNECTED) 3] create -e /master "m2:2223"
Created /master

  主节点如何监控从节点的状态

终端1
[zk: localhost:2181(CONNECTED) 4] create /workers  创建组
Created /workers
监控
[zk: localhost:2181(CONNECTED) 6] ls -w /workers   监控组
[]
终端2

[zk: localhost:2181(CONNECTED) 0] create -e /workers/w1 "w1:2224"
Created /workers/w1
终端1查看收到的通知
[zk: localhost:2181(CONNECTED) 7] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/workers
监控并查看这个组
[zk: localhost:2181(CONNECTED) 7] ls -w /workers
[w1]
终端2 在添加一个节点
[zk: localhost:2181(CONNECTED) 1] create -e /workers/w2 "w2:2224"
Created /workers/w2
终端1查看通知并查看监控组成员
[zk: localhost:2181(CONNECTED) 8] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/workers

[zk: localhost:2181(CONNECTED) 8] ls -w /workers
[w1, w2]

 

zookeeper的单机部署与客户端使用锁

标签:tick   ssi   $path   classes   profile   shu   ctime   ack   ide   

原文地址:https://www.cnblogs.com/rdchenxi/p/13162067.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!