当前位置: 首页 > news >正文

湘潭网站公司搜索引擎排名大全

湘潭网站公司,搜索引擎排名大全,在线做爰a视频网站,四川病毒最新消息今天API ZooKeeper官方提供了Java API,可以通过Java代码来连接zookeeper服务进行操作。可以连接、创建节点、获取节点数据、监听节点变化等操作,具体有以下几个重要的类: ZooKeeper:ZooKeeper类是Java API的核心类,用于与…
API

ZooKeeper官方提供了Java API,可以通过Java代码来连接zookeeper服务进行操作。可以连接、创建节点、获取节点数据、监听节点变化等操作,具体有以下几个重要的类:

  1. ZooKeeper:ZooKeeper类是Java API的核心类,用于与ZooKeeper服务器建立连接,并提供了一系列方法来操作ZooKeeper的节点。
  2. Watcher:Watcher是ZooKeeper的一个回调接口,当节点发生变化时会调用相应的方法进行通知。
  3. CreateMode:CreateMode枚举类定义了节点的类型,包括永久节点、临时节点、顺序节点和临时顺序节点。
  4. Stat:Stat类表示节点的元数据信息,比如修改版本、数据长度、子节点数量等。

添加依赖

<dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.7.2</version>
</dependency>

操作例子

String host = "localhost:2181";
//建立连接
zooKeeper = new ZooKeeper(host, 2000, null);
String path = "/test";
Watcher watcher = new Watcher() {@Overridepublic void process(WatchedEvent watchedEvent) {System.out.println("Node changed: " + watchedEvent.getPath());System.out.println(watchedEvent);}
};
//获取节点状态 如果不存在返回null
Stat stat = zooKeeper.exists(path, false);
if(null != stat){System.out.println(stat.getCzxid()+"-"+stat.getAversion());
}//创建节点 包含版本、时间、数据长度等信息
zooKeeper.create(path,"123".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
//添加watcher
zooKeeper.addWatch(path, watcher, AddWatchMode.PERSISTENT);//获取节点数据
byte[] data = zooKeeper.getData(path, false, null);
System.out.println(new String(data));zooKeeper.create(path+"/1","child1".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
//获取子节点
List<String> children = zooKeeper.getChildren(path, false);
System.out.println("childs size:"+children.size());
//删除子节点
zooKeeper.delete(path+"/1",-1);
zooKeeper.close();
zkClient

zkClient封装了zookeeper的官方api,简化了一些繁琐的操作,并提供了一些额外的功能,提高了开发效.

添加依赖

<dependency><groupId>com.101tec</groupId><artifactId>zkclient</artifactId><version>0.11</version>
</dependency>

zkclient对节点数据的操作进行了序列化, 这里先准备一个string类型的序列化类。需要实现ZkSerializer接口

public class ZkStringSerializer implements ZkSerializer {@Overridepublic byte[] serialize(Object o) throws ZkMarshallingError {return String.valueOf(o).getBytes();}@Overridepublic Object deserialize(byte[] bytes) throws ZkMarshallingError {return new String(bytes);}
}

基本操作

ZkClient zkClient = new ZkClient("localhost:2181");
//自定义序列化 否则报错
zkClient.setZkSerializer(new ZkStringSerializer());
String path = "/test";
//判断节点是否存在
boolean exist = zkClient.exists(path);
System.out.println(exist);
if(!exist){//创建节点zkClient.create(path,"123", CreateMode.PERSISTENT);
}
//读取节点数据
System.out.println((String) zkClient.readData(path));
zkClient.writeData(path,"456");//设置节点数据
System.out.println((String) zkClient.readData(path));
zkClient.delete(path);//删除节点zkClient.close();

节点变化事件

String path = "/test";
/*** 节点变化事件* 只监听节点增减,不监听数据变化事件*/
zkClient.subscribeChildChanges(path, new IZkChildListener() {@Overridepublic void handleChildChange(String parentPath, List<String> children) throws Exception {System.out.println("节点"+parentPath+"发生变化");System.out.println(children);}
});
//节点操作,观察handleChildChange接收到对应事件
Thread.sleep(2000);
zkClient.createPersistent(path);
Thread.sleep(2000);
zkClient.createPersistent(path+"/child1");
Thread.sleep(2000);
zkClient.writeData(path+"/child1","123");
Thread.sleep(2000);
zkClient.delete(path+"/child1");
Thread.sleep(2000);
zkClient.delete(path);
Thread.sleep(100000);

节点数据变化事件

    String path = "/test";/*** 节点变化事件,只检测当前节点,感知不到其子节点* 节点被删除或节点数据变化*/zkClient.subscribeDataChanges(path, new IZkDataListener() {@Overridepublic void handleDataChange(String s, Object o) throws Exception {System.out.println("节点:"+s+"数据变为:"+o);}@Overridepublic void handleDataDeleted(String s) throws Exception {System.out.println("节点:"+s+"删除");}});Thread.sleep(2000);zkClient.createPersistent(path);Thread.sleep(2000);zkClient.createPersistent(path+"/child1");Thread.sleep(2000);zkClient.delete(path+"/child1");Thread.sleep(2000);zkClient.writeData(path,"123");Thread.sleep(2000);zkClient.delete(path);Thread.sleep(100000);
}

Curator

curator是另一个java连接zookeeper类库。功能更加强大。提供了连接重试、分布式锁、选举、队列等多种实际场景的用例。这里先简单搞个使用例子。

添加依赖

<dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>5.1.0</version>
</dependency>

curator-framework是基础的依赖,一些特定的使用方式需要添加不同的依赖,有curator-recipes、curator-x-discovery、curator-x-async等。

基本操作

//创建连接
CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(1000, 3));
client.start();
String path = "/test";
client.checkExists().forPath(path);//判断是否存在
client.create().forPath(path, "123".getBytes());//创建节点
byte[] data = client.getData().forPath(path);//获取数据
System.out.println(new String(data));
client.setData().forPath(path, "456".getBytes());//设置数据
client.delete().forPath(path);//删除节点client.close();

节点监听

CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(1000, 3));
client.start();
String path = "/test";
NodeCache  nodeCache = new NodeCache(client,path);
//添加监听
nodeCache.getListenable().addListener(new NodeCacheListener() {@Overridepublic void nodeChanged() throws Exception {ChildData data = nodeCache.getCurrentData();if (data != null) {System.out.println("Node changed: " + data.getPath() + ", value: " + new String(data.getData()));} else {System.out.println("Node deleted: " + nodeCache.getPath());}}
});
nodeCache.start();
client.create().forPath(path);
client.setData().forPath(path, "123".getBytes());
client.delete().forPath(path);
client.close();

这里NodeCache被标识@Deprecated,也不知道被什么方式代替了,后面再研究。先简单使用。

http://www.ds6.com.cn/news/22373.html

相关文章:

  • 如何找外贸网站建设公司代做seo排名
  • 做网站或者app网店营销策划方案
  • 如何给公司做网站什么关键词能搜到资源
  • 黄山网站建设方案中国今天刚刚发生的新闻
  • 小说网站编辑怎么做链接买卖
  • 电子商务网站开发设计网站编辑
  • 做企业网站进行推广要多少钱互联网平台推广是什么意思
  • 上海建筑建材业网官网爱站seo工具包官网
  • 男女直接做视频网站seopeixun com cn
  • 做网站开发用sublime好吗平面设计主要做什么
  • 深圳创意网站设计青岛做网站推广公司
  • ps个人网站怎么做怎么做神马搜索排名seo
  • b2c的网站名称有哪些百度做广告费用
  • 网站建设得要素seo公司北京
  • 上海网站建设 建站猫重庆电子商务seo
  • 关于网站备案的公告除了百度指数还有哪些指数
  • 关于做网站的文献综述国际婚恋网站排名
  • 有没有代做毕业设计的网站win7最好的优化软件
  • 商城网站 不易优化seo是什么的简称
  • 电商网站安全解决方案上海专业seo公司
  • 做外贸soho 需要有网站吗google seo实战教程
  • 做网站不挣钱百度竞价排名软件
  • 首都在线官网网站seo排名优化培训价格
  • 网站建设代理政策市场营销产品推广策划方案
  • 阿里企业邮箱个人登录网站seo优化技能
  • 合肥网站建设网站推广google推广专员招聘
  • 亚购物车功能网站怎么做的全网推广系统
  • 便宜网站制作公司互联网推广项目
  • 苏州网站建设2万起2023第二波疫情已经到来了
  • 电商网站开发框架女装标题优化关键词