使用curator框架简单操作zookeeper 学习笔记
Curator 操作是zookeeper的优秀api(相对于原生api),满足大部分需求.而且是Fluent流式api风格.
参考文献:https://www.jianshu.com/p/70151fc0ef5d 感谢分享,动手敲一遍留个印象
curator-framework:对zookeeper的底层api的一些封装
curator-client:提供一些客户端的操作,例如重试策略等
curator-recipes:封装了一些高级特性,如:Cache事件监听、选举、分布式锁、分布式计数器、分布式Barrier等
环境:JDK1.8 、maven 、启动三台虚拟机做分部署环境 、 curator-recipes 4.0.1 、zookeeper3.4.8
maven 依赖:不依赖zookeeper会报错..
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
</dependency>
测试增删改查:
package com.zookeeper.curator; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode; import java.util.List; /**
* Created by Administrator on 2018/6/25.
* @see org.apache.zookeeper.CreateMode
* PERSISTENT:持久化
PERSISTENT_SEQUENTIAL:持久化并且带序列号
EPHEMERAL:临时
EPHEMERAL_SEQUENTIAL:临时并且带序列号
*/
public class curatorRecipesDemo { final static String zookeeperAddress = "192.168.149.133:2181,192.168.149.135:2181,192.168.149.134:2181"; public static void main(String[] args) throws Exception {
CuratorFramework curatorClint =
CuratorFrameworkFactory.builder().
connectString(zookeeperAddress)//zkClint连接地址
.connectionTimeoutMs(2000)//连接超时时间
.sessionTimeoutMs(10000)//会话超时时间
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
//重试策略
.namespace("myZookeeperTest")
//命名空间,默认节点
.build(); curatorClint.start(); curatorClint.create().forPath("/path");//默认持久化节点,以斜杠开头
System.out.println(curatorClint.getChildren().forPath("/"));
curatorClint.create().withMode(CreateMode.EPHEMERAL)
.forPath("/secondPath","hello,word".getBytes());
System.out.println("节点secondPath的数据"+new String(curatorClint.getData().forPath("/secondPath")));
curatorClint.setData().forPath("/secondPath","hello,myWorld!".getBytes());
System.out.println("节点secondPath的数据"+new String(curatorClint.getData().forPath("/secondPath"))); curatorClint.create()
.creatingParentContainersIfNeeded()
.forPath("/secondPath/second2/second3");//递归创建
List<String> list= curatorClint.getChildren().forPath("/secondPath");//查询节点的所有字节点
System.out.println(list);
curatorClint.delete().deletingChildrenIfNeeded().forPath("/secondPath/second2");//递归删除
System.out.println(curatorClint.checkExists().forPath("/secondPath/second2"));//判断节点是否存在
System.out.println(curatorClint.checkExists().forPath("/secondPath/second2/second3"));//判断节点是否存在
System.out.println(curatorClint.getChildren().forPath("/secondPath"));
curatorClint.delete().deletingChildrenIfNeeded().forPath("/secondPath"); //todo guaranteed()如果删除失败,会记录下来,只要会话有效,就会不断的重试,直到删除成功为止
//todo Stat stat 对象包含版本id,事物id等信息 } }
创建的节点可以通过 zookeeper 安装下的bin目录 连接客户端 sh zkCli.sh ls / 分开斜杠命令进行查看(或./zkCli.sh -timeout 5000 -server 127.0.0.1:2181)
监听watcer api
package com.zookeeper.curator; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.retry.ExponentialBackoffRetry; import java.util.List;
import java.util.Objects; /**
* Created by Administrator on 2018/6/26.
*
* PathChildCache 监听一个节点下子节点的创建、删除、更新
* NodeCache 监听一个节点的更新和创建事件
* TreeCache 综合PatchChildCache和NodeCache的特性
*/
public class WatcherDemo {
final static String zookeeperAddress = "192.168.149.133:2181,192.168.149.135:2181,192.168.149.134:2181"; public static void main(String[] args) throws Exception {
CuratorFramework curatorClint =
CuratorFrameworkFactory.builder().
connectString(zookeeperAddress)//zkClint连接地址
.connectionTimeoutMs(2000)//连接超时时间
.sessionTimeoutMs(10000)//会话超时时间
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
//重试策略
.namespace("myZookeeperTest")
//命名空间,默认节点
.build(); curatorClint.start();
List<String> list= curatorClint.getChildren().forPath("/");
if(Objects.nonNull(list)){
if( !list.contains("myWatch")){
curatorClint.delete().deletingChildrenIfNeeded().forPath("/myWatch");
}
}else {
curatorClint.create().forPath("/myWatch");
} PathChildrenCache pathChildrenCache= pathChildrenCache = new PathChildrenCache(curatorClint,"/myWatch",false);
PathChildrenCacheListener pathChildrenCacheListener=new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
System.out.println("pathChildrenCacheListener::::->"+pathChildrenCacheEvent.getData());
}
};
pathChildrenCache.getListenable().addListener(pathChildrenCacheListener);//注册监听事件
pathChildrenCache.start(); NodeCache nodeCache=new NodeCache(curatorClint,"/myWatch",false);
NodeCacheListener nodeCacheListener=new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
System.out.println("nodeCacheListener::::->:"+nodeCache.getCurrentData().getPath());
}
};
nodeCache.getListenable().addListener(nodeCacheListener);
nodeCache.start(); TreeCache treeCache=new TreeCache(curatorClint,"/myWatch");
TreeCacheListener treeCacheListener=new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception {
System.out.println("treeCacheListener::::->"+treeCacheEvent.getData());
}
};
treeCache.getListenable().addListener(treeCacheListener);
treeCache.start(); curatorClint.create().forPath("/myWatch/child22","生个好孩子".getBytes());
curatorClint.create().creatingParentContainersIfNeeded().forPath("/myWatch/child22/child22","生个好孩子".getBytes());
curatorClint.setData().forPath("/myWatch/child222","生个好孩子aaaa".getBytes());
System.in.read();//阻塞不然启动后clint就关掉了
} }
使用curator框架简单操作zookeeper 学习笔记的更多相关文章
- ZooKeeper学习笔记三:使用ZooKeeper实现一个简单的配置中心
作者:Grey 原文地址:ZooKeeper学习笔记三:使用ZooKeeper实现一个简单的配置中心 前置知识 完成ZooKeeper集群搭建以及熟悉ZooKeeperAPI基本使用 需求 很多程序往 ...
- ZooKeeper学习笔记四:使用ZooKeeper实现一个简单的分布式锁
作者:Grey 原文地址: ZooKeeper学习笔记四:使用ZooKeeper实现一个简单的分布式锁 前置知识 完成ZooKeeper集群搭建以及熟悉ZooKeeperAPI基本使用 需求 当多个进 ...
- ZooKeeper 学习笔记
ZooKeeper学习笔记 1. zookeeper基本概念 zookeeper是一个分布式的,开放源码的分布式应用程序协调服务,是hadoop和Habase的重要组件,是为分布式应用提供一致性服 ...
- delphi操作xml学习笔记 之一 入门必读
Delphi 对XML的支持---TXMLDocument类 Delphi7 支持对XML文档的操作,可以通过TXMLDocument类来实现对XML文档的读写.可以利用TXMLDocum ...
- ZooKeeper学习笔记(二)——内部原理
zookeeper学习笔记(二)--内部原理 1. zookeeper的节点的类型 总的来说可以分为持久型和短暂型,主要区别如下: 持久:客户端与服务器端断开连接的以后,创建的节点不会被删除: 持久化 ...
- Zookeeper学习笔记(中)
Zookeeper学习笔记(中) Zookeeper的基本原理和基本实现 深入了解ZK的基本原理 ZK的一致性: ZAB 协议: Zookeeper 原子消息广播协议 ZK通过选举保证 leader ...
- Zookeeper学习笔记(上)
Zookeeper学习笔记 本篇主要是一些基本的介绍和API的使用介绍, 有些只是记录了知识点,而没有完全在笔记中详细解释, 需要自行查找资料补充相关概念 主要参考了课程中的内容: Zookeeper ...
- PHP操作MongoDB学习笔记
<?php/*** PHP操作MongoDB学习笔记*///*************************//** 连接MongoDB数据库 **////*************** ...
- Flas-SQLAchemy数据库操作使用学习笔记
Flas-SQLAchemy数据库操作使用学习笔记 Flask-SQLALchemy 是一个给你的应用添加 SQLALchemy 支持的 Flask 扩展.SQLALchemy 是Python语言的S ...
随机推荐
- 微信小程序调用微信登陆获取openid及用户信息 java做为服务端
转载的文章,很不错 https://blog.csdn.net/weilai_zhilu/article/details/77932630
- golang中defer的理解
在golang当中,defer代码块会在函数调用链表中增加一个函数调用.这个函数调用不是普通的函数调用,而是会在函数正常返回,也就是return之后添加一个函数调用.因此,defer通常用来释放函数内 ...
- Jmeter(十三)Debug Sampler
上篇记录了关联的一些内容.当然,关联是有“风险”的,经常写的正则表达式,jsonpath,xpath自己认为是没问题的,可是就是跑起来有问题,百思不得其解,究竟是数据问题还是表达式的问题,本篇就来记录 ...
- ha环境下重新格式化hdfs报错
datanode启动不成功,如下所示,我的136,137.138都是datanode,都启动不了. 查看datanode日志文件发现报错: 一个报错Incompatible clusterIDs in ...
- http和https的区别联系
超文本传输协议HTTP协议被用于在Web浏览器和网站服务器之间传递信息,HTTP协议以明文方式发送内容,不提供任何方式的数据加密,如果攻击者截取了Web浏览器和网站服务器之间的传输报文,就可以直接读懂 ...
- crm 2016 tabstatechange event
1 tabstatechange事件在窗体中定义 2 问题是如果选项卡默认为折叠的.且选项卡中包含了iFrame网页. 3 在网页内容加载完成之后,点击选项卡 折叠/展开 按钮, iFrame网页没有 ...
- SSM 项目整合
SSM整合:spring + springmvc + mybatis 1.1 生成Maven项目:ar_ssm 1.2 添加jar包 <dependencies> <!-- 单元测试 ...
- 文本adversarial examples
对文本对抗性样本的研究极少,近期论文归纳如下: 文本对抗三个难点: text data是离散数据,multimedia data是连续数据,样本空间不一样: 对text data的改动可能导致数据不合 ...
- 命令提示符没办法登录MySQL
前几天在CMD命令下输入 MYSQL -UROOT -P 一直显示命令无效. 后来想知道原来是要在MYSQL的BIN(安装)目录下才能访问该命令,如果要在其他路径下使用sql命令,那么要配置MYSQ ...
- 基于pyQt5开发的股价显示器(原创)
#/usr/bin/env python # -*- coding: utf-8 -*- ''' @author="livermorium116" 为了绕开公司内网而开发的 股票实 ...