Curator的cluster,实现多节点数据共享
模拟两个客户端,实现多节点数据共享
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.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode; public class CuratorWatcher { /** 父节点path */
static final String PARENT_PATH = "/super"; /** zookeeper服务器地址 */
public static final String CONNECT_ADDR = "192.168.2.2: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();
System.out.println("c1 start...");
Thread.sleep(100000000);
}
}
package bjsxt.curator.cluster;
public class Client2 {
public static void main(String[] args) throws Exception {
CuratorWatcher watcher = new CuratorWatcher();
System.out.println("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.2.2: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"); }
}
运行代码后,可以看到Client1和Client2,监听,获取到节点的数据变化。
Curator的cluster,实现多节点数据共享的更多相关文章
- zookeeper客户端使用第三方(Curator)封装的Api操作节点
1.为什么使用Curator? Curator本身是Netflix公司开源的zookeeper客户端: Curator 提供了各种应用场景的实现封装: curator-framework 提供了f ...
- Redis Cluster 集群节点维护 (三)
Redis Cluster 集群节点维护: 集群运行很久之后,难免由于硬件故障,网络规划,业务增长,等原因对已有集群进行相应的调整,比如增加redis nodes 节点,减少节点,节点迁移,更换服务器 ...
- 【RAC】安装cluster软件 在节点2执行root.sh脚本
安装cluster软件 在节点2执行root.sh脚本 报错如下: Running vipca(silent) for configuring nodeapps /db/oracle/product ...
- Windows Cluster 添加新节点--验证报错
今天给既有Windows Cluster 添加节点时,验证总是不通过.报错信息为 防火墙未正确配置为故障转移群集.现将处理步骤汇总如下. 1.错误具体信息 报错的位置 --[验证警告] 的步骤中发现错 ...
- 获取redis cluster中所有节点的内存使用情况
需求:获取redis cluster集群中所有节点的内存使用情况. ip_port=`redis-cli -h $ -p $ -a abc123 -c cluster nodes | awk '{pr ...
- redis cluster 添加/删除节点操作
RedisCluster 添加/删除节点 添加节点新配置两个测试节点8008和9009 [root@--- ~]# /usr/local/redis-/bin/redis-server /u02/re ...
- Redis Cluster 集群节点信息 维护篇(二)
集群信息文件: # cluster 集群内部信息对应文件,由集群自动维护. /data/soft/redis/6379data/nodes-6379.conf 集群信息查看: ./redis-trib ...
- PXC(percona xtradb cluster)新加节点避免SST的方法
环境: node1:192.168.0.100 pxc节点 node2:192.168.0.101 新节点 把新加入的节点先建立为node1的从库,可以使用mysqldump或innobackup ...
- MySQL Cluster配置文件-SQL节点4G内存
# Example MySQL config file for large systems. # # This is for a large system with memory = 512M whe ...
随机推荐
- freebsd安装python2
第一步 cd /usr/ports/lang/python27 第二步 输入以下命令(需要联网) make make install 到此如果输入python无效 继续 第三步 cd /usr/ ...
- JS 验证字符串是否为空
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- PythonStudy——字典的定义 Dictionary definition
# 空字典 d1 = {} d2 = dict() # 用map映射创建字典 d3 = dict({'a': 1, 'b': 1}) print(d3) # 用关键字赋值方式 d4 = dict(na ...
- 将文件夹下的所有csv文件存入数据库
# 股票的多因子分层回测代码实现 import os import pymysql # import datetime, time # from config import * database_ta ...
- Python调用ffpmeg和ffprobe处理视频文件
需求: 运营有若干批次的视频.有上千个,视频文件,有mp4格式的,有ts格式的 现在有需要去掉视频文件片头和片尾的批量操作需求. 比如 文件夹A下面的视频去掉片尾10秒 文件夹B下面的视频去掉片头6秒 ...
- Fabric的@runs_once的理解
1:runs_once的用法,一直没理解,我看网上都是说:“函数修饰符,标识的函数只会执行一次,不受多台主机影响” 实在没理解,然后看了一下官方文档,这样解释 举个例子: #!/usr ...
- Linux内核分析第三次作业
实验:mykernel时间片轮转多道程序内核 进入实验楼实验,在终端中分别输入以下命令 cd LinuxKernel/linux-3.9.4 rm -rf mykernel patch -p1 < ...
- WebApi 序列化 循环引用问题
public static void Register(HttpConfiguration config) { // Web API 配置和服务 config.Formatters.Remove(co ...
- PHP chdir函数:改变当前的目录
PHP chdir函数的作用是改变当前的目录,这里主机吧详细介绍下chdir函数的用法,并列举使用chdir函数的例子. chdir定义和用法: chdir() 函数改变当前的目录. chdir实例: ...
- activate-power-mode效果实验(未完全成功)
原产地: Atom 支持版本一览 https://github.com/codeinthedark/awesome-power-mode 1.安装Atom:[ AtomSetup-x64.exe] 百 ...