一、安装
docker pull zookeeper
docker run -d -p 2181 :2181 --restart= always --name zk zookeeper:latest
docker exec -it 69f0622fdd4e /bin/bash
二、服务端命令
本文章讲解docker安装zk,因此运行容器后自动启动了zk,不需要重新启动服务端,启动一个客户端连接服务端就行了 如果需要启动服务端,则命令如下:
root@69f0622fdd4e:/apache-zookeeper-3.8.0-bin/bin
root@69f0622fdd4e:/apache-zookeeper-3.8.0-bin/bin
root@69f0622fdd4e:/apache-zookeeper-3.8.0-bin/bin
三、客户端命令
持久节点
create /zoo 'hello'
create /zoo/child3 'msg'
create /zoo/child1
create /zoo/child1/gran1 'grand'
[ zk: localhost:2181( CONNECTED) 11 ] get /zoo/child3
msg3
ls /zoo
[ zk: localhost:2181( CONNECTED) 16 ] delete /zoo/child2
[ zk: localhost:2181( CONNECTED) 17 ] ls /zoo
[ child1, child3]
[ zk: localhost:2181( CONNECTED) 18 ] delete /zoo/child3
[ zk: localhost:2181( CONNECTED) 19 ] ls /zoo
[ child1]
[ zk: localhost:2181( CONNECTED) 20 ] delete /zoo/child1
Node not empty: /zoo/child1
[ zk: localhost:2181( CONNECTED) 29 ] deleteall /zoo
[ zk: localhost:2181( CONNECTED) 30 ] ls /zoo
Node does not exist: /zoo
临时节点
[ zk: localhost:2181( CONNECTED) 42 ] create -e /znode 'msg'
Created /znode
[ zk: localhost:2181( CONNECTED) 43 ] get /znode
msg
[ zk: localhost:2181( CONNECTED) 44 ] create /znode/child
Ephemerals cannot have children: /znode/child
[ zk: localhost:2181( CONNECTED) 45 ] create -e /znode/child
Ephemerals cannot have children: /znode/child
临时节点在客户端会话结束或者发生故障的时候被 ZooKeeper 系统自动清除。创建第二个客户端连接,查看节点信息
[ zk: localhost:2181( CONNECTED) 1 ]
WATCHER::
WatchedEvent state:SyncConnected type:NodeDeleted path:/znode
[ zk: localhost:2181( CONNECTED) 0 ] stat /znode true
'stat path [watch]' has been deprecated. Please use 'stat [-w] path' instead.
cZxid = 0x1a
ctime = Fri May 20 08:14:18 UTC 2022
mZxid = 0x1a
mtime = Fri May 20 08:14:18 UTC 2022
pZxid = 0x1a
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x1004aba99840002
dataLength = 3
numChildren = 0
顺序节点
[ zk: localhost:2181( CONNECTED) 15 ] create /test
Created /test
[ zk: localhost:2181( CONNECTED) 16 ] create -s /test/seq1 'seq1'
Created /test/seq10000000000
[ zk: localhost:2181( CONNECTED) 17 ] create -s /test/seq1 'seq2'
Created /test/seq10000000001
[ zk: localhost:2181( CONNECTED) 18 ] create -s /test/seq1 'seq3'
Created /test/seq10000000002
[ zk: localhost:2181( CONNECTED) 19 ] create -s /test/seq1 'seq4'
Created /test/seq10000000003
[ zk: localhost:2181( CONNECTED) 20 ] ls /test
[ seq10000000000, seq10000000001, seq10000000002, seq10000000003]
delete /test/seq10000000000
[ zk: localhost:2181( CONNECTED) 42 ] create /test
Created /test
[ zk: localhost:2181( CONNECTED) 43 ] create -s -e /test/linshi1 'linshi1'
Created /test/linshi10000000000
[ zk: localhost:2181( CONNECTED) 44 ] create -e -s /test/linshi2 'linshi2'
Created /test/linshi20000000001
四、JavaAPI操作
< dependency>
< groupId> org.apache.curator</ groupId>
< artifactId> curator-framework</ artifactId>
< version> 4.2.0</ version>
</ dependency>
< dependency>
< groupId> org.apache.curator</ groupId>
< artifactId> curator-recipes</ artifactId>
< version> 4.2.0</ version>
</ dependency>
学到新的test注解,此时启动会报错,需要在测试类加上@TestInstance(TestInstance.Lifecycle.PER_CLASS)
private CuratorFramework client;
@BeforeAll
void testConnect ( ) {
RetryPolicy retryPolicy = new ExponentialBackoffRetry ( 3000 , 10 ) ;
client = CuratorFrameworkFactory . newClient (
"120.79.157.16:2181" , 60 * 1000 , 15 * 1000 , retryPolicy) ;
client. start ( ) ;
}
@AfterAll
void close ( ) {
if ( client != null ) {
client. close ( ) ;
}
}
创建连接
@Test
void testConnect ( ) {
RetryPolicy retryPolicy = new ExponentialBackoffRetry ( 3000 , 10 ) ;
CuratorFramework client = CuratorFrameworkFactory . newClient (
"120.79.157.16:2181" , 60 * 1000 , 15 * 1000 , retryPolicy) ;
client. start ( ) ;
}
@Test
void testConn ( ) {
RetryPolicy retryPolicy = new ExponentialBackoffRetry ( 3000 , 10 ) ;
CuratorFramework build = CuratorFrameworkFactory . builder ( ) . connectString ( "120.79.157.16:2181" )
. sessionTimeoutMs ( 60 * 1000 )
. connectionTimeoutMs ( 15 * 1000 )
. retryPolicy ( retryPolicy) . build ( ) ;
build. start ( ) ;
}
创建节点
@Test
void testCreate ( ) throws Exception {
String s = client. create ( ) . forPath ( "/api" ) ;
System . out. println ( s) ;
}
@Test
void testCreate1 ( ) throws Exception {
String data = client. create ( ) . forPath ( "/data" , "hehe" . getBytes ( ) ) ;
System . out. println ( data) ;
}
@Test
void testCreate2 ( ) throws Exception {
String data = client. create ( ) . withMode ( CreateMode . EPHEMERAL) . forPath ( "/EPHEMERAL" , "EPHEMERAL" . getBytes ( ) ) ;
System . out. println ( data) ;
}
@Test
void testCreate3 ( ) throws Exception {
String data = client. create ( ) . withMode ( CreateMode . PERSISTENT_SEQUENTIAL) . forPath ( "/PERSISTENT_SEQUENTIAL" , "PERSISTENT_SEQUENTIAL" . getBytes ( ) ) ;
System . out. println ( data) ;
}
@Test
void testCreate4 ( ) throws Exception {
String data = client. create ( ) . withMode ( CreateMode . EPHEMERAL_SEQUENTIAL) . forPath ( "/EPHEMERAL_SEQUENTIAL" , "EPHEMERAL_SEQUENTIAL" . getBytes ( ) ) ;
System . out. println ( data) ;
}
@Test
void testCreate5 ( ) throws Exception {
String data = client. create ( ) . creatingParentsIfNeeded ( ) . forPath ( "/parent/child" ) ;
System . out. println ( data) ;
}
查询节点
@Test
void get1 ( ) throws Exception {
byte [ ] data = client. getData ( ) . forPath ( "/data" ) ;
System . out. println ( new String ( data) ) ;
}
@Test
void get2 ( ) throws Exception {
List < String > strings = client. getChildren ( ) . forPath ( "/" ) ;
System . out. println ( strings) ;
}
@Test
void get3 ( ) throws Exception {
Stat stat = new Stat ( ) ;
System . out. println ( stat) ;
System . out. println ( "------------------" ) ;
client. getData ( ) . storingStatIn ( stat) . forPath ( "/data" ) ;
System . out. println ( stat) ;
}
修改节点
@Test
void update1 ( ) throws Exception {
Stat stat = client. setData ( ) . forPath ( "/data" , "update2" . getBytes ( ) ) ;
System . out. println ( stat) ;
}
@Test
void update2 ( ) throws Exception {
Stat stat = new Stat ( ) ;
client. getData ( ) . storingStatIn ( stat) . forPath ( "/data" ) ;
int version = stat. getVersion ( ) ;
System . out. println ( "数据版本:" + version) ;
client. setData ( ) . withVersion ( version) . forPath ( "/data" , "update3" . getBytes ( ) ) ;
System . out. println ( stat) ;
}
删除节点
@Test
void del1 ( ) throws Exception {
client. delete ( ) . forPath ( "/test0000000009" ) ;
}
@Test
void del2 ( ) throws Exception {
client. delete ( ) . deletingChildrenIfNeeded ( ) . forPath ( "/parent" ) ;
}
@Test
void del3 ( ) throws Exception {
client. delete ( ) . guaranteed ( ) . forPath ( "/api" ) ;
}
@Test
void del4 ( ) throws Exception {
client. delete ( ) . guaranteed ( ) . inBackground ( new BackgroundCallback ( ) {
@Override
public void processResult ( CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
System . out. println ( "我被删除了!" ) ;
System . out. println ( curatorEvent) ;
}
} ) . forPath ( "/del" ) ;
}
Watch事件监听
zookeeper提供了三种watcher:
NodeCache:只是监听某一个特定的节点 PathChildCache:监控一个ZNode的子节点 TreeCache:可以监控整个树上的所有节点,就是将前面两种整合
curator引入cache来实现对zookeeper服务端事件的监听
@Test
void nodeCache ( ) throws Exception {
NodeCache nodeCache = new NodeCache ( client, "/data/nodecache" ) ;
nodeCache. getListenable ( ) . addListener ( new NodeCacheListener ( ) {
@Override
public void nodeChanged ( ) throws Exception {
System . out. println ( "节点变化了" ) ;
byte [ ] data = nodeCache. getCurrentData ( ) . getData ( ) ;
System . out. println ( new String ( data) ) ;
}
} ) ;
nodeCache. start ( true ) ;
while ( true ) {
}
}
@Test
void childCache ( ) throws Exception {
PathChildrenCache pathChildrenCache = new PathChildrenCache ( client, "/parent" , true ) ;
pathChildrenCache. getListenable ( ) . addListener ( new PathChildrenCacheListener ( ) {
@Override
public void childEvent ( CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
System . out. println ( "儿子们变化了" ) ;
System . out. println ( pathChildrenCacheEvent) ;
PathChildrenCacheEvent. Type type = pathChildrenCacheEvent. getType ( ) ;
if ( type. equals ( PathChildrenCacheEvent. Type . CHILD_UPDATED) ) {
byte [ ] data = pathChildrenCacheEvent. getData ( ) . getData ( ) ;
System . out. println ( new String ( data) ) ;
}
}
} ) ;
pathChildrenCache. start ( ) ;
while ( true ) {
}
}
@Test
void treeCache ( ) throws Exception {
TreeCache treeCache = new TreeCache ( client, "/parent" ) ;
treeCache. getListenable ( ) . addListener ( new TreeCacheListener ( ) {
@Override
public void childEvent ( CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception {
System . out. println ( "发生变化" ) ;
}
} ) ;
treeCache. start ( ) ;
while ( true ) {
}
}
五、分布式锁
概念:跨机器的进程之间的数据同步问题 zk实现:客户端获取锁时创建节点,使用完后删除该节点。
在lock(随便取的)下创建临时顺序节点,临时保证宕机自动删除,顺序保证 获取lock下面的所有子节点,客户端获取到所有的子节点之后,如果发现自己创建的子节点序号最小,那么就认为改客户端获取到了锁,使用完成后,将该节点删除。 如果发现自己创建的节点并非lock下所有子结点中最小的,说明自己还没获取到锁,此时客户端需要找到比自己小的那个节点(注意是靠近当前节点的前面那一个节点),同时对其注册事件监听器,监听删除事件。 如果发现比自己小的那个节点被删除,此时再次判断自己创建的节点是否是lock子节点中序号最小的,如果是则获取到锁,如果不是则重复以上步骤继续获取到比自己小的一个节点并注册监听。
六、Curator实现分布式锁API
支持五种锁方案,需要详细了解可以换个博客看看 分布式锁案例:
package com. test. ticket ;
import org. apache. curator. RetryPolicy ;
import org. apache. curator. framework. CuratorFramework ;
import org. apache. curator. framework. CuratorFrameworkFactory ;
import org. apache. curator. framework. recipes. locks. InterProcessLock ;
import org. apache. curator. framework. recipes. locks. InterProcessMutex ;
import org. apache. curator. retry. ExponentialBackoffRetry ;
import java. util. concurrent. TimeUnit ;
public class SellTicket implements Runnable {
private CuratorFramework client;
private Integer tickets = 10 ;
private InterProcessLock lock;
public SellTicket ( ) {
RetryPolicy retryPolicy = new ExponentialBackoffRetry ( 3000 , 10 ) ;
client = CuratorFrameworkFactory . newClient (
"120.79.157.16:2181" , 60 * 1000 , 15 * 1000 , retryPolicy) ;
client. start ( ) ;
lock = new InterProcessMutex ( client, "/lock" ) ;
}
@Override
public void run ( ) {
try {
while ( true ) {
lock. acquire ( ) ;
if ( tickets > 0 ) {
System . out. println ( Thread . currentThread ( ) . getName ( ) + ":" + tickets) ;
tickets-- ;
}
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
try {
lock. release ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
}
}