ZooKeeper-3.4.10 Java API的使用样例
来源:互联网 发布:网络攻防大赛比什么 编辑:程序博客网 时间:2024/05/21 14:58
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务框架,包含一组简单的原语集合。通过这些原语言的组合使用,能够帮助我们解决更高层次的分布式问题。
package demo.jdbc.zookeeper.main;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.ZooDefs;import org.apache.zookeeper.ZooKeeper;import org.apache.zookeeper.data.Stat;import java.io.IOException;import java.util.List;public class SimpleZKClient { private static final String connectString = "Master:2181,Slave1:2181,Slave2:2181"; private static final int sessionTimeout = 2000; private static ZooKeeper zooKeeper = null; public static void main(String[] args) throws IOException, KeeperException, InterruptedException { getConnection(); if (testExist() == null) { testCreate(); } getChildren(); getData(); setData(); getData(); deleteZNode(); testExist(); } /** * 初始化zooKeeper * * @throws IOException */ public static void getConnection() throws IOException { zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() { @Override public void process(WatchedEvent event) { } }); } /** * @return * @throws KeeperException * @throws InterruptedException */ public static Stat testExist() throws KeeperException, InterruptedException { Stat stat = zooKeeper.exists("/eclipse", false); if (stat == null) { System.out.println("NOT EXIST"); } else { System.out.println("EXIST"); } return stat; } /** * 在zk中创建数据节点 * * @throws KeeperException * @throws InterruptedException */ public static void testCreate() throws KeeperException, InterruptedException { // 参数1:创建的节点的路径 参数2:节点的数据 参数3:节点的权限 参数4:节点的类型 zooKeeper.create("/eclipse", "eclipse".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); // 上传的数据可以是任何类型,但是要转换成byte[] } /** * 获取子节点 * * @throws KeeperException * @throws InterruptedException */ public static void getChildren() throws KeeperException, InterruptedException { List<String> children = zooKeeper.getChildren("/", true); System.out.println("ZNode List:{"); for (String child : children) { System.out.println(child); } System.out.println("}"); } /** * 获取ZNode中的数据 * * @throws KeeperException * @throws InterruptedException */ public static void getData() throws KeeperException, InterruptedException { byte[] data = zooKeeper.getData("/eclipse", false, null); System.out.println("data:" + new String(data)); } /** * 修改ZNode中的数据 * * @throws KeeperException * @throws InterruptedException */ public static void setData() throws KeeperException, InterruptedException { zooKeeper.setData("/eclipse", "eclipse.new".getBytes(), -1); } /** * 删除ZNode * * @throws KeeperException * @throws InterruptedException */ public static void deleteZNode() throws KeeperException, InterruptedException { // 参数2:指定要删除的版本 -1表示删除所有的版本 zooKeeper.delete("/eclipse", -1); }}
输出结果:
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.NOT EXISTZNode List:{zookeepereclipse}data:eclipsedata:eclipse.newNOT EXISTProcess finished with exit code 0
依赖的Jar 包
jline-0.9.94.jarlog4j-1.2.16.jarnetty-3.10.5.Final.jarslf4j-api-1.6.1.jarslf4j-log4j12-1.6.1.jarzookeeper-3.4.10.jar
注:依赖的Jar包均可以在zookeeper-3.4.10.tar.gz安装包里面找到。
Over
阅读全文
0 0
- ZooKeeper-3.4.10 Java API的使用样例
- Zookeeper Java API的使用
- Zookeeper Java Api的使用
- 【ZooKeeper Notes 3】ZooKeeper Java API 使用样例
- 【ZooKeeper Notes 3】ZooKeeper Java API 使用样例
- Zookeeper Java API使用
- Zookeeper使用--Java API
- ZooKeeper Java API使用
- Zookeeper Java客户端API的使用
- zookeeper(二) java客户端api的使用
- ZooKeeper API 的使用
- 【Zookeeper系列三】ZooKeeper Java API使用
- 使用java api操作zookeeper
- Zookeeper服务框架之Java API的使用(三)
- Zookeeper原始API的使用
- zookeeper的Java客户端API
- ZooKeeper的Java Api初探
- zookeeper的java客户端api
- Linux网络命令
- flow
- 以太坊钱包Ethereum Wallet C盘数据转移
- nginx(四) nginx+keepalived 实现主备+双主热备模型的高可用负载均衡代理服务
- Ionic运行第一个项目
- ZooKeeper-3.4.10 Java API的使用样例
- maven创建非web项目
- 清除DB缓存,获得SQL问运行的真实效率.
- JavaScript基础语句
- no suggestions
- 1008 N的阶乘 mod P【基础题or 水题?】
- 大四报了个天价培训班,变成量产型炮灰工程师
- MSP430中断相关
- 分享一些Swift的干货(续)