Zookeeper之javaAPI的使用

桃扇骨 2022-10-02 00:53 71阅读 0赞

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

Java程序操作Zookeeper

1.创建java项目并导入相关jar包

主要jar包在主目录下 在这里插入图片描述 项目需要的相关依赖的jar包在zookeeper的解压文件的lib目录下就有 在这里插入图片描述 将这几个jar包导入项目中 在这里插入图片描述

  1. <dependency>
  2. <groupId>org.apache.zookeeper</groupId>
  3. <artifactId>zookeeper</artifactId>
  4. <version>3.4.6</version>
  5. <type>pom</type>
  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>junit</groupId>
  14. <artifactId>junit</artifactId>
  15. <version>4.12</version>
  16. </dependency>

API简单使用

2.1配置Zookeeper对象

  1. // zookeeper的服务器地址,配置conf是用主机名,这里一样
  2. private String connectString ="zek00:2181,zek01:2181,zek02:2181";
  3. // 连接超时时间
  4. private int sessionTimeout = 2000;
  5. private ZooKeeper zk = null;
  6. /**
  7. * 设置zookeeper对象
  8. * @throws IOException
  9. */
  10. @Before
  11. public void setZookeeper() throws IOException {
  12. zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
  13. /**
  14. * 事件触发的回调方法
  15. */
  16. @Override
  17. public void process(WatchedEvent event) {
  18. }
  19. });
  20. System.out.println("---"+zk);
  21. }

常用API操作

  1. /**
  2. * create 方法参数
  3. * 第一个参数 路径
  4. * 第二个参数 值 bytes
  5. * 第三个参数 对节点的访问控制
  6. * 第四个参数 节点的类型 短暂 永久 序号
  7. * @throws KeeperException
  8. * @throws InterruptedException
  9. */
  10. @Test
  11. public void create() throws KeeperException, InterruptedException {
  12. String path = zk.create("/app3", "123".getBytes(),
  13. Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  14. System.out.println(path);
  15. }
  16. /**
  17. * 判断节点是否存在
  18. * @throws InterruptedException
  19. * @throws KeeperException
  20. */
  21. @Test
  22. public void exist() throws KeeperException, InterruptedException{
  23. // 设置为 true 会调用 zk中的监听器
  24. Stat stat = zk.exists("/app1", true);
  25. if(stat!=null){
  26. System.out.println("存在。。。。"+stat.getDataLength());
  27. }else{
  28. System.out.println("不存在。。。");
  29. }
  30. }
  31. /**
  32. * 获取子节点
  33. * @throws InterruptedException
  34. * @throws KeeperException
  35. */
  36. @Test
  37. public void getChilren() throws KeeperException, InterruptedException{
  38. List<String> list = zk.getChildren("/", true);
  39. for (String s : list) {
  40. System.out.println(s);
  41. }
  42. }
  43. /**
  44. * 获取节点的内容
  45. * @throws InterruptedException
  46. * @throws KeeperException
  47. */
  48. @Test
  49. public void getData() throws KeeperException, InterruptedException{
  50. byte[] b = zk.getData("/app1", false, null );
  51. System.out.println(new String(b));
  52. }
  53. /**
  54. * 修改节点内容
  55. * version -1 自动维护
  56. * @throws InterruptedException
  57. * @throws KeeperException
  58. */
  59. @Test
  60. public void setData() throws KeeperException, InterruptedException{
  61. Stat s = zk.setData("/app1", "test".getBytes(), -1);
  62. }
  63. /**
  64. * 删除节点
  65. * 非空节点删除不掉
  66. * @throws InterruptedException
  67. * @throws KeeperException
  68. */
  69. @Test
  70. public void deleteNode() throws InterruptedException, KeeperException{
  71. zk.delete("/app1", -1);
  72. }

监听器的使用

  1. @Before
  2. public static void setUpBeforeClass() throws Exception {
  3. zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
  4. /**
  5. * 监听器
  6. */
  7. @Override
  8. public void process(WatchedEvent event) {
  9. System.out.println("事件触发...");
  10. }
  11. });
  12. }
  13. /**
  14. * 获取子节点
  15. * getChildren(path,watch?)监听的事件是:节点下的子节点增减变化事件
  16. * @throws InterruptedException
  17. * @throws KeeperException
  18. */
  19. @Test
  20. public void getChilren() throws KeeperException, InterruptedException{
  21. List<String> list = zk.getChildren("/", new Watcher() {
  22. @Override
  23. public void process(WatchedEvent event) {
  24. // TODO Auto-generated method stub
  25. System.out.println("监控节点下的子节点被改变..."+event.getPath());
  26. }
  27. });
  28. for (String s : list) {
  29. System.out.println(s);
  30. }
  31. Thread.sleep(Long.MAX_VALUE);
  32. }
  33. /**
  34. * 获取节点的内容
  35. * getData(path,watch?)监听的事件是:节点数据变化事件
  36. * @throws InterruptedException
  37. * @throws KeeperException
  38. */
  39. @Test
  40. public void getData() throws KeeperException, InterruptedException{
  41. byte[] b = zk.getData("/app1", new Watcher() {
  42. @Override
  43. public void process(WatchedEvent event) {
  44. System.out.println("--数据改变了--");
  45. }
  46. }, null );
  47. System.out.println(new String(b));
  48. Thread.sleep(Long.MAX_VALUE);
  49. }

转载于:https://my.oschina.net/u/4116655/blog/3049614

发表评论

表情:
评论列表 (有 0 条评论,71人围观)

还没有评论,来说两句吧...

相关阅读