09.Curator临时节点
1.PersistentEphemeralNode类
/*** @param client client instance* @param mode creation/protection mode* @param basePath the base path for the node* @param data data for the node*/public PersistentEphemeralNode(CuratorFramework client, Mode mode, String basePath, byte[] initData)
- EPHEMERAL: 以ZooKeeper的 CreateMode.EPHEMERAL方式创建节点。
- EPHEMERAL_SEQUENTIAL: 如果path已经存在,以CreateMode.EPHEMERAL创建节点,否则以CreateMode.EPHEMERAL_SEQUENTIAL方式创建节点。
- PROTECTED_EPHEMERAL: 以CreateMode.EPHEMERAL创建,提供保护方式。
- PROTECTED_EPHEMERAL_SEQUENTIAL: 类似EPHEMERAL_SEQUENTIAL,提供保护方式。
2.编写示例程序
public class PersistentEphemeralNodeExample{private static final String PATH = "/example/ephemeralNode";private static final String PATH2 = "/example/node";public static void main(String[] args) throws Exception{CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));client.getConnectionStateListenable().addListener(new ConnectionStateListener(){@Overridepublic void stateChanged(CuratorFramework client, ConnectionState newState){System.out.println("连接状态:" + newState.name());}});client.start();PersistentEphemeralNode node = new PersistentEphemeralNode(client, Mode.EPHEMERAL, PATH, "临时节点".getBytes());node.start();node.waitForInitialCreate(3, TimeUnit.SECONDS);String actualPath = node.getActualPath();System.out.println("临时节点路径:" + actualPath + " | 值: " + new String(client.getData().forPath(actualPath)));client.create().forPath(PATH2, "持久化节点".getBytes());System.out.println("持久化节点路径: " + PATH2 + " | 值: " + new String(client.getData().forPath(PATH2)));KillSession.kill(client.getZookeeperClient().getZooKeeper(), "127.0.0.1:2181");System.out.println("临时节点路径:" + actualPath + " | 是否存在: " + (client.checkExists().forPath(actualPath) != null));System.out.println("持久化节点路径: " + PATH2 + " | 值: " + new String(client.getData().forPath(PATH2)));CloseableUtils.closeQuietly(node);CloseableUtils.closeQuietly(client);}}
3.示例程序运行结果
连接状态:CONNECTED临时节点路径:/example/ephemeralNode | 值: 临时节点持久化节点路径: /example/node | 值: 持久化节点连接状态:SUSPENDED连接状态:LOST连接状态:RECONNECTED临时节点路径:/example/ephemeralNode | 是否存在: true持久化节点路径: /example/node | 值: 持久化节点
-------------------------------------------------------------------------------------------------------------------------------
09.Curator临时节点的更多相关文章
- Zookeeper--0300--java操作Zookeeper,临时节点实现分布式锁原理
删除Zookeeper的java客户端有 : 1,Zookeeper官方提供的原生API, 2,zkClient,在原生api上进行扩展的开源java客户端 3, 一.Zookeeper原生API ...
- Zookeeper的临时节点和永久节点
Zookeeper中节点分为两种:临时节点和永久节点. 临时节点有一个节点: 当创建临时节点的程序停掉之后,这个临时节点就会消失. 更直观的,如下 Persistent是临时节点. Persist ...
- 实验:zk master宕机后,临时节点在新的master上是否存在,结果出人意料
一.实验 实验说明:3台zk集群,主要验证:master上的客户端,在master上建立临时节点,当master宕机时,其他follower选为主后,临时节点是否存在. 主要是通过此来验证,基于zk的 ...
- mysql 创建临时节点
use ZooKeeper; use AnyEvent; use AE; use Data::Dumper; use IO::Socket; my $zk = ZooKeeper->new(ho ...
- 临时节点不能有child 子节点
[zk: 127.0.0.1:2181(CONNECTED) 67] create '/zk01/0001/aaaa' dada Ephemerals cannot have children: /z ...
- zookeeper客户端使用第三方(Curator)封装的Api操作节点
1.为什么使用Curator? Curator本身是Netflix公司开源的zookeeper客户端: Curator 提供了各种应用场景的实现封装: curator-framework 提供了f ...
- 基于zk“临时顺序节点“的分布式锁
import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import java.io.IOException; im ...
- Zookeeper注册节点的掉线自动重新注册及测试方法
转载:http://www.codelast.com/ 在一套分布式的online services系统中,各service通常不会放在一台服务器上,而是通过Zookeeper这样的东西,将自己的se ...
- Curator实现分布式锁
分布式锁的应用 分布式锁服务宕机, ZooKeeper 一般是以集群部署, 如果出现 ZooKeeper 宕机, 那么只要当前正常的服务器超过集群的半数, 依然可以正常提供服务 持有锁资源服务器宕机, ...
随机推荐
- e591. Drawing Simple Text
See also e575 The Quintessential Drawing Program. public void paint(Graphics g) { // Set the desired ...
- e1086. if/else语句
The if statement encloses some code which is executed only if a condition is true. The general synta ...
- 倾斜摄影 实景三维建模软件photoscan教程
PhotoScan是一款基于影响自动生成高质量实景三维模型的优秀软件,这对于3D建模需求来说实在是一把利器. PhotoScan无需设置初始值,无须相机检校,它根据最新的多视图三维重建技术,可对任意照 ...
- Cocos2d-x3.0触摸
cocos2d-x 3.0開始採用C++11,并開始脱离OC风格,在触摸处理上也作出了改变 C++11带来了Lambda表达式(匿名函数),它将简化程序编写,可使代码更清晰易懂 在旧2.x版本号的触摸 ...
- 漫谈.Net关键字系列之一Sealed与Final(转)
转自:http://www.cnblogs.com/isline/archive/2010/08/31/1813396.html Sealed与Final修饰符其实并不是一个语言平台的产物,他们有着各 ...
- 处理URL为模块/控制器/方法的格式
一个路由在不确定长度的情况下,控制器和方法不一定传参进来,可能只有模块,甚至模块都没有,这个时候需要特殊处理一下,默认模块用 index,默认控制器用index,默认方法用 index来处理URL. ...
- jquery 添加可操作,编辑不可操作
--jsp <td class="queryTitle" width="100">优惠券批次号</td> <td class=&q ...
- 说说SPI协议
SPI,是英语Serial Peripheral Interface 的缩写,顾名思义就是串行外围设备接口.SPI,是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线,节约了芯片的管 ...
- 使用Jquery做分页效果
之前写过一个PHP 的分页效果,但是今天小伙伴和我说了一个不适用后台单纯用前段的JS来写分页,整理了一下,代码如下: html: <div id="containet"> ...
- 点击一个textView里的link导航至程序内可返回的自定义webView
1,在AppDelegate.h里定义一个 id currentViewController; 在AppDelegate.m里 @implementation UIApplication (Priva ...