java zookeeper_Java操作zookeeper

packagezook;importorg.apache.curator.framework.CuratorFramework;importorg.apache.curator.framework.CuratorFrameworkFactory;importorg.apache.curator.framework.recipes.cache.PathChildrenCache;importorg.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;importorg.apache.curator.framework.recipes.cache.PathChildrenCacheListener;importorg.apache.curator.retry.ExponentialBackoffRetry;importorg.apache.zookeeper.CreateMode;importorg.apache.zookeeper.data.Stat;public classCurator {public static void main(String[] args) throwsException {

String connStr= "192.168.23.24:2181";

CuratorFramework cur=CuratorFrameworkFactory.builder()

.connectString(connStr)

.connectionTimeoutMs(5000)

.retryPolicy(new ExponentialBackoffRetry(1000,3))

.build();

cur.start();//连接//创建监听

PathChildrenCache cache=new PathChildrenCache(cur,"/top",true);

cache.start();

cache.rebuild();

cache.getListenable().addListener(newPathChildrenCacheListener() {

@Overridepublic void childEvent(CuratorFramework framwork, PathChildrenCacheEvent event) throwsException {

System.err.println("节点发生变化:"+event.getType());

}

});

Stat stat=cur.checkExists().forPath("/top/zhuzhu");if(stat!=null){

System.out.println("【/top/zhuzhu】节点存在,直接删除");

cur.delete().forPath("/top/zhuzhu");

}

cur.delete().forPath("/top/zhuzhu");

System.in.read();

System.out.println("准备创建【/top/zhuzhu】");

cur.create().withMode(CreateMode.PERSISTENT)

.forPath("/top/zhuzhu", "love forever".getBytes());

System.out.println("节点【/top/zhuzhu】创建成功");

Thread.sleep(1000);byte[] bs=cur.getData().forPath("/top/zhuzhu");

System.out.println("数据:"+newString(bs));

Thread.sleep(1000);

cur.delete().forPath("/top/zhuzhu");

Thread.sleep(1000);

}/*** 三种watcher来做节点的监听

* pathcache 监视一个路径下子节点的创建、删除、节点数据更新

* NodeCache 监视一个节点的创建、更新、删除

* TreeCache pathcaceh+nodecache 的合体(监视路径下的创建、更新、删除事件),

* 缓存路径下的所有子节点的数据*/

public static void main1(String[] args) throwsException {

String connStr= "192.168.23.24:2181";

CuratorFramework curatorFramework=CuratorFrameworkFactory.builder()

.connectString(connStr)

.connectionTimeoutMs(5000)

.retryPolicy(new ExponentialBackoffRetry(1000,3))

.build();

curatorFramework.start();/*** 节点变化NodeCache*/

/*NodeCache cache=new NodeCache(curatorFramework,"/curator",false);

cache.start(true);

cache.getListenable().addListener(()-> System.out.println("节点数据发生变化,变化后的结果" +

":"+new String(cache.getCurrentData().getData())));

curatorFramework.setData().forPath("/curator","菲菲".getBytes());*/

/*** PatchChildrenCache*/PathChildrenCache cache=new PathChildrenCache(curatorFramework,"/event",true);

cache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);

cache.rebuild();//Normal / BUILD_INITIAL_CACHE /POST_INITIALIZED_EVENT

cache.getListenable().addListener((curatorFramework1,pathChildrenCacheEvent)->{switch(pathChildrenCacheEvent.getType()){caseCHILD_ADDED:

System.out.println("增加子节点");break;caseCHILD_REMOVED:

System.out.println("删除子节点");break;caseCHILD_UPDATED:

System.out.println("更新子节点");break;default:break;

}

});//curatorFramework.create().withMode(CreateMode.PERSISTENT).forPath("/event","event".getBytes());//TimeUnit.SECONDS.sleep(1);//System.out.println("1");//curatorFramework.create().withMode(CreateMode.PERSISTENT).forPath("/event/event1","1".getBytes());//TimeUnit.SECONDS.sleep(1);//System.out.println("2");//

//curatorFramework.setData().forPath("/event/event1","222".getBytes());//TimeUnit.SECONDS.sleep(1);//System.out.println("3");

curatorFramework.delete().forPath("/event/event1");

System.out.println("4");

System.in.read();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值