Curator 实现多节点数据共享demo

package bjsxt.curator.cluster;

import java.util.List;

import java.util.Map;

import java.util.concurrent.ConcurrentHashMap;

import java.util.concurrent.CopyOnWriteArrayList;

import java.util.concurrent.CountDownLatch;

import org.apache.curator.RetryPolicy;

import org.apache.curator.framework.CuratorFramework;

import org.apache.curator.framework.CuratorFrameworkFactory;

import org.apache.curator.framework.recipes.cache.PathChildrenCache;

import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;

import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;

import org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode;

import org.apache.curator.retry.ExponentialBackoffRetry;

import org.apache.zookeeper.CreateMode;

import org.apache.zookeeper.KeeperException;

import org.apache.zookeeper.WatchedEvent;

import org.apache.zookeeper.Watcher;

import org.apache.zookeeper.Watcher.Event.EventType;

import org.apache.zookeeper.Watcher.Event.KeeperState;

import org.apache.zookeeper.ZooDefs.Ids;

import org.apache.zookeeper.ZooKeeper;

public class CuratorWatcher {

/** 父节点path */

static final String PARENT_PATH = "/super";

/** zookeeper服务器地址 */

public static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181"; /** 定义session失效时间 */

public static final int SESSION_TIMEOUT = 30000;

public CuratorWatcher() throws Exception{

//1 重试策略:初试时间为1s 重试10次

RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10);

//2 通过工厂创建连接

CuratorFramework cf = CuratorFrameworkFactory.builder()

.connectString(CONNECT_ADDR)

.sessionTimeoutMs(SESSION_TIMEOUT)

.retryPolicy(retryPolicy)

.build();

//3 建立连接

cf.start();

//4 创建跟节点  如果根节点不存在就创建一个

if(cf.checkExists().forPath(PARENT_PATH) == null){

cf.create().withMode(CreateMode.PERSISTENT).forPath(PARENT_PATH,"super init".getBytes());

}

//4 建立一个PathChildrenCache缓存,第三个参数为是否接受节点数据内容 如果为false则不接受

PathChildrenCache cache = new PathChildrenCache(cf, PARENT_PATH, true);

//5 在初始化的时候就进行缓存监听

cache.start(StartMode.POST_INITIALIZED_EVENT);

cache.getListenable().addListener(new PathChildrenCacheListener() {

/**

 * <B>方法名称:</B>监听子节点变更<BR>

 * <B>概要说明:</B>新建、修改、删除<BR>

 * @see org.apache.curator.framework.recipes.cache.PathChildrenCacheListener#childEvent(org.apache.curator.framework.CuratorFramework, org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)

 */

@Override

public void childEvent(CuratorFramework cf, PathChildrenCacheEvent event) throws Exception {

switch (event.getType()) {

case CHILD_ADDED:

System.out.println("CHILD_ADDED :" + event.getData().getPath());

System.out.println("CHILD_ADDED :" + new String(event.getData().getData()));

break;

case CHILD_UPDATED:

System.out.println("CHILD_UPDATED :" + event.getData().getPath());

System.out.println("CHILD_UPDATED :" + new String(event.getData().getData()));

break;

case CHILD_REMOVED:

System.out.println("CHILD_REMOVED :" + event.getData().getPath());

System.out.println("CHILD_REMOVED :" + new String(event.getData().getData()));

break;

default:

break;

}

}

});

}

}

package bjsxt.curator.cluster;

public class Client1 {

public static void main(String[] args) throws Exception{

CuratorWatcher watcher = new CuratorWatcher();

Sysyem.out.priintln(“c1  start..”);

Thread.sleep(100000000);

}

}

运行结果 :

package bjsxt.curator.cluster;

public class Client2 {

public static void main(String[] args) throws Exception{

CuratorWatcher watcher = new CuratorWatcher();

Sysyem.out.priintln(“c2  start..”);

Thread.sleep(100000000);

}

}

运行结果 :

package bjsxt.curator.cluster;

import org.apache.curator.RetryPolicy;

import org.apache.curator.framework.CuratorFramework;

import org.apache.curator.framework.CuratorFrameworkFactory;

import org.apache.curator.retry.ExponentialBackoffRetry;

import org.apache.zookeeper.CreateMode;

public class Test {

/** zookeeper地址 */

static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181";

/** session超时时间 */

static final int SESSION_OUTTIME = 5000;//ms

public static void main(String[] args) throws Exception {

//1 重试策略:初试时间为1s 重试10次

RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10);

//2 通过工厂创建连接

CuratorFramework cf = CuratorFrameworkFactory.builder()

.connectString(CONNECT_ADDR)

.sessionTimeoutMs(SESSION_OUTTIME)

.retryPolicy(retryPolicy)

.build();

//3 开启连接

cf.start();

Thread.sleep(3000);

System.out.println(cf.getChildren().forPath("/super").get(0));

//4 创建节点

// Thread.sleep(1000);

cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/c1","c1内容".getBytes());

Thread.sleep(1000);

// cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/c2","c2内容".getBytes());

// Thread.sleep(1000);

//

//

//

// //5 读取节点

// Thread.sleep(1000);

// String ret1 = new String(cf.getData().forPath("/super/c1"));

// System.out.println(ret1);

//

//

// //6 修改节点

Thread.sleep(1000);

cf.setData().forPath("/super/c2", "修改的新c2内容".getBytes());

String ret2 = new String(cf.getData().forPath("/super/c2"));

System.out.println(ret2);

//

//

//

// //7 删除节点

// Thread.sleep(1000);

// cf.delete().forPath("/super/c1");

}

}

运行结果

将上面三个class全部停掉后,再次启动c1、c2 控制台打印

curator实现了重复注册重复读取的功能: 可以将c1、c2 看做是两个服务器,在添加数据之后,  服务器停掉之后,再次启动,还可以获取到之前的数据,包括线下的节点添加。

  • 24
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JAVA代码搬运工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值