Zookeeper入门(七)之Java连接Zookeeper Linux环境下Zookeeper安装

前端之家收集整理的这篇文章主要介绍了Zookeeper入门(七)之Java连接Zookeeper Linux环境下Zookeeper安装前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

 

Java操作Zookeeper很简单,但是前提要把包导对。

关于Zookeeper的Linux环境搭建可以参考我的这篇博客:Linux环境下Zookeeper安装

下面进入正题:

一、导入依赖

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2. modelVersion>4.0.0</groupId>cn.zookeeperartifactId>zookeeper_demoversion>0.0.1-SNAPSHOT>
  3. <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
  4.  
  5. dependencies>
  6. dependency>org.apache.zookeeper>zookeeper>3.4.6>
  7. project>

 

二、编写工具类代码和测试代码

  1. package zookeeper_demo;
  2. import java.util.List;
  3. java.util.concurrent.CountDownLatch;
  4. org.apache.zookeeper.CreateMode;
  5. org.apache.zookeeper.KeeperException;
  6. org.apache.zookeeper.WatchedEvent;
  7. org.apache.zookeeper.Watcher;
  8. org.apache.zookeeper.Watcher.Event.KeeperState;
  9. org.apache.zookeeper.ZooDefs.Ids;
  10. org.apache.zookeeper.ZooKeeper;
  11. org.apache.zookeeper.data.Stat;
  12. public class BaseZookeeper implements Watcher{
  13. private ZooKeeper zookeeper;
  14. /**
  15. * 超时时间
  16. */
  17. private static final int SESSION_TIME_OUT = 2000;
  18. private CountDownLatch countDownLatch = new CountDownLatch(1);
  19. void process(WatchedEvent event) {
  20. if (event.getState() == KeeperState.SyncConnected) {
  21. System.out.println("Watch received event");
  22. countDownLatch.countDown();
  23. }
  24. }
  25. 连接zookeeper
  26. * @param host
  27. * @throws Exception
  28. void connectZookeeper(String host) throws Exception{
  29. zookeeper = new ZooKeeper(host,SESSION_TIME_OUT,this);
  30. countDownLatch.await();
  31. System.out.println("zookeeper connection success");
  32. }
  33. * 创建节点
  34. * path
  35. * data
  36. * public String createNode(String path,String data) Exception{
  37. return .zookeeper.create(path,data.getBytes(),Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
  38. }
  39. * 获取路径下所有子节点
  40. * @return
  41. * KeeperException
  42. * InterruptedException
  43. public List<String> getChildren(String path) KeeperException,InterruptedException{
  44. List<String> children = zookeeper.getChildren(path,1)">false);
  45. return children;
  46. }
  47. * 获取节点上面的数据
  48. * path 路径
  49. * public String getData(String path) byte[] data = zookeeper.getData(path,1)">false,1)">nullif (data == ) {
  50. return "";
  51. }
  52. new String(data);
  53. }
  54. * 设置节点信息
  55. * data 数据
  56. * public Stat setData(String path,InterruptedException{
  57. Stat stat = zookeeper.setData(path,-1 stat;
  58. }
  59. * 删除节点
  60. * InterruptedException
  61. * KeeperException
  62. void deleteNode(String path) InterruptedException,KeeperException{
  63. zookeeper.delete(path,-1
  64. * 获取创建时间
  65. * public String getCTime(String path) String.valueOf(stat.getCtime());
  66. }
  67. * 获取某个路径下孩子的数量
  68. * public Integer getChildrenNum(String path) int childenNum = zookeeper.getChildren(path,1)">).size();
  69. childenNum;
  70. }
  71. * 关闭连接
  72. * void closeConnection() InterruptedException{
  73. if (zookeeper != ) {
  74. zookeeper.close();
  75. }
  76. }
  77. void main(String[] args) Exception {
  78. BaseZookeeper zookeeper = BaseZookeeper();
  79. zookeeper.connectZookeeper("192.168.126.128:2181");
  80. List<String> children = zookeeper.getChildren("/");
  81. System.out.println(children);
  82. }
  83. }

 

完成以上两步,即可完成Java连接并对Zookeeper的简单操作。