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 ...
随机推荐
- ARTIFICIAL INTELLIGENCE FOR GAMES (Ian Millington / John Funge 著)
相关网站:http://www.ai4g.com PART I AI AND GAMESCHAPTER1 INTRODUCTIONCHAPTER2 GAME AIPART II TECHNIQUESC ...
- MongoDB基础知识(二)
一.基本概念 1:文档(document)是MongoDB中数据的基本单元,非常类似于关系型数据库管理系统中的行 2:集合(collection)可以看做是一个拥有动态模式(dynamic schem ...
- confluence6.3.1升级最新版本(6.15.1)
参考自官方文档:https://www.cwiki.us/display/CONFLUENCEWIKI/Upgrading+Confluence 1,confluence6.3.1安装部署 https ...
- Maven+Spirng+Mybatis+CXF搭建WebService服务
https://jingyan.baidu.com/article/39810a23b1de2fb637fda66c.html
- 淘宝客知道这几个ID,收入将会提高50%
基础问题天天说,天天有人问.这篇文章写点基础的.特别对新手的帮助会很大哦. 1,PID,做淘宝客不知道PID,赚到钱也会被冻结. 如何手动获取PID 2,单品ID,淘宝商品的唯一识别编号,和身份证一样 ...
- CentOS 7安装WordPress
在开始本文前,我假定你已经安装好了nginx.php-fpm和mariaDB(或mysql).它们的安装过程可参考我以前的文章. 1. 安装EPEL(Extra Packages for Enterp ...
- 【idea】之使用SVN一些技巧
@Copy https://www.cnblogs.com/whc321/p/5669804.html
- Program type already present: android.support.v4.widget.EdgeEffectCompat
1.确保所有依赖包的 implementation 'com.android.support:appcompat-v7:25.4.0'是一样的 2.确保最外层的build.gradle中增加如下代码: ...
- docker network基础
前面介绍了nginx与php两个容器间是如何进行通信的: [root@docker ~]# docker run -d --name=php -v /www:/usr/local/nginx/html ...
- 杂谈2.cpp
Ostream类定义的插入运算符(<<)使数据插入到输出流,istream定义的抽取运算符(>>)能够从输入流中抽取信息 Cin和cout都是智能对象,能够根据程序上下文将信息 ...