模拟两个客户端,实现多节点数据共享

 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,实现多节点数据共享的更多相关文章

  1. zookeeper客户端使用第三方(Curator)封装的Api操作节点

    1.为什么使用Curator? Curator本身是Netflix公司开源的zookeeper客户端: Curator  提供了各种应用场景的实现封装: curator-framework  提供了f ...

  2. Redis Cluster 集群节点维护 (三)

    Redis Cluster 集群节点维护: 集群运行很久之后,难免由于硬件故障,网络规划,业务增长,等原因对已有集群进行相应的调整,比如增加redis nodes 节点,减少节点,节点迁移,更换服务器 ...

  3. 【RAC】安装cluster软件 在节点2执行root.sh脚本

    安装cluster软件  在节点2执行root.sh脚本 报错如下: Running vipca(silent) for configuring nodeapps /db/oracle/product ...

  4. Windows Cluster 添加新节点--验证报错

    今天给既有Windows Cluster 添加节点时,验证总是不通过.报错信息为 防火墙未正确配置为故障转移群集.现将处理步骤汇总如下. 1.错误具体信息 报错的位置 --[验证警告] 的步骤中发现错 ...

  5. 获取redis cluster中所有节点的内存使用情况

    需求:获取redis cluster集群中所有节点的内存使用情况. ip_port=`redis-cli -h $ -p $ -a abc123 -c cluster nodes | awk '{pr ...

  6. redis cluster 添加/删除节点操作

    RedisCluster 添加/删除节点 添加节点新配置两个测试节点8008和9009 [root@--- ~]# /usr/local/redis-/bin/redis-server /u02/re ...

  7. Redis Cluster 集群节点信息 维护篇(二)

    集群信息文件: # cluster 集群内部信息对应文件,由集群自动维护. /data/soft/redis/6379data/nodes-6379.conf 集群信息查看: ./redis-trib ...

  8. PXC(percona xtradb cluster)新加节点避免SST的方法

    环境: node1:192.168.0.100  pxc节点 node2:192.168.0.101  新节点 把新加入的节点先建立为node1的从库,可以使用mysqldump或innobackup ...

  9. MySQL Cluster配置文件-SQL节点4G内存

    # Example MySQL config file for large systems. # # This is for a large system with memory = 512M whe ...

随机推荐

  1. @Transactional 可以写在 Controller 方法上面了

    上图  t1  掉用的service 没定义事物环境,但是 在 t1 上面定义了. 依旧可以 在 参数是5 的 时候 ,让 前面的操作级联回滚.  但是 我不建议这么用,除非特殊需求,正常来说事物根据 ...

  2. IIS 负载均衡

    在大型Web应用系统中,由于请求的数据量过大以及并发的因素,导致Web系统会出现宕机的现象,解决这一类问题的方法我个人觉得主要在以下几个方面: 1.IIS 负载均衡. 2.数据库 负载均衡. 3.系统 ...

  3. c++11 tuple实现

    实现一个简易版的c++11 tuple. 我使用的编译器是gcc,codeblocks13.12自带的,哪个版本我不熟gcc也没去查. 大致看了下他家的tuple实现,多继承,tuple之上还有2个辅 ...

  4. Git-撤销(回退)已经add,commit或push的提交

    本文只阐述如何解决问题,不会对git的各种概念多做介绍,如果有兴趣可以点击下面的链接,进行详细的学习:Pro Git本文适用的环境 现在先假设几个环境,本文将会给出相应的解决方法:1. 本地代码(或文 ...

  5. 猴子选大王的c#实现

    原文地址:猴子选大王的c#实现作者:余文 今天被问到了猴子选大王的意思,题目大意就是说有n只猴子围坐成一个圈,按顺时针方向从1到n编号.然后从1号猴子开始沿顺时针方向从1开始报数,报到m的猴子出局,再 ...

  6. Kong管理UI -kong-dashboard

    本文仍然是在ubuntu18的环境下进行 https://github.com/PGBI/kong-dashboard kong dashboart如果要正常使用管理UI,前提为kong已经正常run ...

  7. 关于Django部分

    1  安装 执行pip安装即可 pip install Django 2 开启项目 3 项目新建后所在位置: 4 开启端口命令 python manage.py runserver 0.0.0.0:9 ...

  8. ss客户端以及tcp,udp,dns代理ss-tproxy在线安装版--centos7.3 x64以上(7.3-7.6x64测试通过)

    #!/bin/sh # # Script for automatic setup of an SS-TPROXY server on CentOS 7.3 Minimal. # export PATH ...

  9. lecune入门示例

    注意:本示例中的lucene版本需在jdk7以上使用. 一.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" ...

  10. day38数据库MySQL基础

    数据库相关基础1 数据库介绍 1.数据库相关概念  数据库服务器(本质就是一个台计算机,该计算机之上安装有数据库管理软件的服务端)  数据库管理管理系统RDBMS(本质就是一个C/S架构的套接字软件) ...