当前位置:   article > 正文

基于ZkClient往zookeeper分布式集群节点写入数据_zk客户端手动增加数据

zk客户端手动增加数据

zookeeper自身的ZooKeeper构建一个连接,然后往zookeeper节点写入数据,但是代码写法略显复杂。现在使用github上的一个开源项目ZkClient 

https://github.com/sgroschupf/zkclient

 

可以大大简化往zookeeper分布式集群中的节点写入数据的复杂度。

首先需要在pom.xml添加引用:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.apache.zookeeper</groupId>
  4. <artifactId>zookeeper</artifactId>
  5. <version>3.5.5</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.github.sgroschupf</groupId>
  9. <artifactId>zkclient</artifactId>
  10. <version>0.1</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.apache.logging.log4j</groupId>
  14. <artifactId>log4j-core</artifactId>
  15. <version>2.12.1</version>
  16. </dependency>
  17. </dependencies>

 

Java代码程序:

  1. import org.I0Itec.zkclient.IZkChildListener;
  2. import org.I0Itec.zkclient.IZkDataListener;
  3. import org.I0Itec.zkclient.ZkClient;
  4. import org.apache.zookeeper.*;
  5. import java.util.List;
  6. public class Main {
  7. public static void main(String[] args) {
  8. //初始化log4j,zookeeper否则报错。
  9. org.apache.log4j.BasicConfigurator.configure();
  10. try {
  11. Main m = new Main();
  12. } catch (Exception e) {
  13. e.printStackTrace();
  14. }
  15. }
  16. public Main() throws Exception {
  17. String ip = "localhost";
  18. String addrs = ip + ":2181," + ip + ":2182," + ip + ":2183";
  19. //连接zookeeper服务器。
  20. //addrs是一批地址,如果其中某一个服务器挂掉,其他仍可用。
  21. ZkClient zkClient = new ZkClient(addrs);
  22. //zkClient.connect(300 * 1000, new MyWatcher());
  23. System.out.println("连接建立");
  24. String node = "/zhang_phil_node_2";
  25. //数据变化监听。
  26. zkClient.subscribeDataChanges(node, new IZkDataListener() {
  27. @Override
  28. public void handleDataChange(String dataPath, Object data) throws Exception {
  29. System.out.println("handleDataChange:" + dataPath);
  30. System.out.println("handleDataChange:" + data.toString());
  31. }
  32. @Override
  33. public void handleDataDeleted(String dataPath) throws Exception {
  34. }
  35. });
  36. zkClient.subscribeChildChanges(node, new IZkChildListener() {
  37. @Override
  38. public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
  39. System.out.println("handleChildChange:" + parentPath);
  40. for (String c : currentChilds) {
  41. System.out.println("handleChildChange:" + c);
  42. }
  43. }
  44. });
  45. if (!zkClient.exists(node)) {
  46. zkClient.create(node, "hello,world! 2", CreateMode.PERSISTENT);
  47. }
  48. String s = zkClient.readData(node);
  49. System.out.println(s);
  50. }
  51. /*
  52. private class MyWatcher implements Watcher {
  53. @Override
  54. public void process(WatchedEvent event) {
  55. if (event.getState() == Event.KeeperState.SyncConnected) {
  56. System.out.println("状态:" + Event.KeeperState.SyncConnected);
  57. }
  58. }
  59. }
  60. */
  61. }

 

程序运行后,用zookeeper提供的命令行进入节点查看数据情况:

表明写入成功。

乱码是序列号导致。用ZkClient读取出来的数据仍然是:

hello,world! 2

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/957238
推荐阅读
相关标签
  

闽ICP备14008679号