1 Maven导入
< dependency>
< groupId> org.apache.curator</ groupId>
< artifactId> curator-framework</ artifactId>
< version> 5.1.0</ version>
</ dependency>
< dependency>
< groupId> org.apache.curator</ groupId>
< artifactId> curator-recipes</ artifactId>
< version> 5.1.0</ version>
</ dependency>
2 基本操作
2.1 连接客户端
RetryPolicy retryPolicy = new ExponentialBackoffRetry ( 3000 , 10 ) ;
CuratorFramework client = CuratorFrameworkFactory . newClient ( "192.168.170.148:2181" ,
60 * 1000 , 15 * 1000 , retryPolicy) ;
CuratorFramework client = CuratorFrameworkFactory . builder ( )
. connectString ( "192.168.170.148:2181" )
. sessionTimeoutMs ( 60 * 1000 )
. connectionTimeoutMs ( 15 * 1000 )
. retryPolicy ( retryPolicy) . build ( ) ;
client. start ( ) ;
2.2 创建节点
String path = curatorFramework. create ( ) . forPath ( "/app3" ) ;
System . out. println ( path) ;
String path2 = curatorFramework. create ( ) . withMode ( CreateMode . EPHEMERAL) . forPath ( "/app4" ) ;
System . out. println ( path2) ;
String path3 = curatorFramework. create ( ) . creatingParentsIfNeeded ( ) . forPath ( "/app5/p1" ) ;
System . out. println ( path3) ;
2.3 查询节点
byte [ ] data = curatorFramework. getData ( ) . forPath ( "/app1" ) ;
System . out. println ( new String ( data) ) ;
List < String > list = curatorFramework. getChildren ( ) . forPath ( "/" ) ;
System . out. println ( list) ;
Stat stat = new Stat ( ) ;
curatorFramework. getData ( ) . storingStatIn ( stat) . forPath ( "/app1" ) ;
System . out. println ( stat) ;
2.4 修改节点
curatorFramework. setData ( ) . forPath ( "/app1" , "333" . getBytes ( StandardCharsets . UTF_8) ) ;
Stat stat1 = new Stat ( ) ;
curatorFramework. getData ( ) . storingStatIn ( stat1) . forPath ( "/app1" ) ;
curatorFramework. setData ( ) . withVersion ( stat1. getVersion ( ) ) . forPath ( "/app1" , "itcast" . getBytes ( StandardCharsets . UTF_8) ) ;
2.5 删除节点
curatorFramework. delete ( ) . forPath ( "/app1" ) ;
curatorFramework. delete ( ) . deletingChildrenIfNeeded ( ) . forPath ( "/app1" ) ;
curatorFramework. delete ( ) . guaranteed ( ) . forPath ( "/app1" ) ;
curatorFramework. delete ( ) . inBackground ( ( curatorFramework1, curatorEvent) -> {
System . out. println ( curatorFramework1) ;
System . out. println ( curatorEvent) ;
} ) . forPath ( "/app1" ) ;
2.6 监听节点
2.6.1 监听单个节点
CuratorCache curatorCache = CuratorCache . build ( client, "/test1" ) ;
curatorCache. listenable ( ) . addListener ( ( type, childData, childData1) -> {
System . out. println ( "--------------------------" ) ;
System . out. println ( type) ;
System . out. println ( "--------------------------" ) ;
System . out. println ( childData) ;
System . out. println ( new String ( childData. getData ( ) ) ) ;
System . out. println ( "--------------------------" ) ;
System . out. println ( childData1) ;
System . out. println ( new String ( childData1. getData ( ) ) ) ;
System . out. println ( "--------------------------" ) ;
} ) ;
curatorCache. start ( ) ;
2.6.2 监听所有子节点
CuratorCache curatorCache = CuratorCache . build ( client, "/test1" ) ;
CuratorCacheListener curatorCacheListener = CuratorCacheListener
. builder ( )
. forPathChildrenCache ( "/test1" , client, ( curatorFramework, pathChildrenCacheEvent) -> {
System . out. println ( "子节点变化了~" ) ;
PathChildrenCacheEvent. Type type = pathChildrenCacheEvent. getType ( ) ;
System . out. println ( type) ;
if ( type. equals ( PathChildrenCacheEvent. Type . CHILD_UPDATED) ) {
System . out. println ( "数据变了!!!" ) ;
byte [ ] data = pathChildrenCacheEvent. getData ( ) . getData ( ) ;
System . out. println ( new String ( data) ) ;
}
} ) . build ( ) ;
curatorCache. listenable ( ) . addListener ( curatorCacheListener) ;
curatorCache. start ( ) ;
2.6.3 监听节点树
CuratorCache curatorCache = CuratorCache . build ( client, "/" ) ;
CuratorCacheListener curatorCacheListener = CuratorCacheListener
. builder ( )
. forTreeCache ( client, ( curatorFramework, treeCacheEvent) -> {
System . out. println ( "节点变化了" ) ;
System . out. println ( treeCacheEvent) ;
System . out. println ( new String ( treeCacheEvent. getData ( ) . getData ( ) ) ) ;
} ) . build ( ) ;
curatorCache. listenable ( ) . addListener ( curatorCacheListener) ;
curatorCache. start ( ) ;
2.6.3 5.0以前版本
final NodeCache nodeCache = new NodeCache ( client, "/app1" ) ;
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 ) ;
PathChildrenCache pathChildrenCache = new PathChildrenCache ( client, "/app2" , true ) ;
pathChildrenCache. getListenable ( ) . addListener ( new PathChildrenCacheListener ( ) {
@Override
public void childEvent ( CuratorFramework client, PathChildrenCacheEvent event) {
System . out. println ( "子节点变化了~" ) ;
System . out. println ( event) ;
PathChildrenCacheEvent. Type type = event. getType ( ) ;
if ( type. equals ( PathChildrenCacheEvent. Type . CHILD_UPDATED) ) {
System . out. println ( "数据变了!!!" ) ;
byte [ ] data = event. getData ( ) . getData ( ) ;
System . out. println ( new String ( data) ) ;
}
}
} ) ;
pathChildrenCache. start ( ) ;
TreeCache treeCache = new TreeCache ( client, "/app2" ) ;
treeCache. getListenable ( ) . addListener ( new TreeCacheListener ( ) {
@Override
public void childEvent ( CuratorFramework client, TreeCacheEvent event) {
System . out. println ( "节点变化了" ) ;
System . out. println ( event) ;
}
} ) ;
treeCache. start ( ) ;