Zookeeper:JavaApi创建节点
一.pom文件和log4j.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <?xml version= "1.0" encoding= "UTF-8" ?> <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" > <modelVersion> 4.0 . 0 </modelVersion> <groupId>com.wj</groupId> <artifactId>zookeeperApi</artifactId> <version> 1.0 -SNAPSHOT</version> <dependencies> <!-- https: //mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version> 3.4 . 10 </version> </dependency> <!-- https: //mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version> 1.2 . 16 </version> </dependency> <!-- https: //mvnrepository.com/artifact/jline/jline --> <dependency> <groupId>jline</groupId> <artifactId>jline</artifactId> <version> 0.9 . 94 </version> </dependency> <!-- https: //mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version> 4.7 </version> </dependency> <!-- https: //mvnrepository.com/artifact/org.slf4j/slf4j-api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version> 1.6 . 1 </version> </dependency> <!-- https: //mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version> 1.6 . 1 </version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version> 1.6 . 1 </version> </dependency> </dependencies> </project> |
1 2 3 4 5 6 7 | # Define some default values that can be overridden by system properties log4j.rootLogger=INFO, CONSOLE log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Threshold=INFO log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p - %m%n |
二.Java连接Zookeeper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public static void main(String[] args) throws Exception { //计数器对象 final CountDownLatch latch = new CountDownLatch( 1 ); // * @param sessionTimeout // * session timeout in milliseconds // * @param watcher // * a watcher object which will be notified of state changes, may // * also be notified for node events ZooKeeper zooKeeper = new ZooKeeper( "192.168.10.132:2181" , 500 , new Watcher() { public void process(WatchedEvent event) { if (event.getState() ==Watcher.Event.KeeperState.SyncConnected){ System.out.println( "连接创建成功" ); latch.countDown(); } } }); //主线程阻塞等待连接对象的创建成功 latch.await(); System.out.println( "----" +zooKeeper.getSessionId()+ "----" ); zooKeeper.close(); } |
控制台打印情况:
三.创建节点
1 2 3 4 | //同步方式 create( final String path, byte data[], List<ACL> acl,CreateMode createMode) //异步方式 create( final String path, byte data[], List<ACL> acl,CreateMode createMode, StringCallback cb, Object ctx) |
参数说明:
path:-znode路径
data:节点数据内容
acl:访问控制列表
createMode:节点的类型,枚举类
cb:异步回调接口
ctx:传递上下文参数
1 2 3 4 5 | //节点路径 /create //节点数据 create //权限列表: world:anyone:adrwa //节点类型:持久化节点 zooKeeper.create( "/create" , "create" .getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); |
world授权:
1 2 3 4 5 | List<ACL> acls = new ArrayList<ACL>(); Id id = new Id( "world" , "anyone" ); acls.add( new ACL(ZooDefs.Perms.READ,id)); acls.add( new ACL(ZooDefs.Perms.WRITE,id)); zooKeeper.create( "/create/node2" , "node2" .getBytes(), acls, CreateMode.PERSISTENT); |
ip授权:
1 2 3 4 | List<ACL> acls = new ArrayList<ACL>(); Id id = new Id( "ip" , "192.168.10.132" ); acls.add( new ACL(ZooDefs.Perms.ALL,id)); zooKeeper.create( "/create/node3" , "node3" .getBytes(), acls, CreateMode.PERSISTENT); |
auth授权:
1 2 3 | //添加授权用户 zooKeeper.addAuthInfo( "digest" , "admin:admin" .getBytes());<br> //给予所有权限 zooKeeper.create( "/create/node4" , "node4" .getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); |
digest授权:
1 2 3 4 5 | //授权模式和授权对象 Id id = new Id( "digest" , "wj:64ibjjwm94195LPhuzhUdkIjOl0=" ); //所有权限 acls.add( new ACL(ZooDefs.Perms.ALL,id)); zooKeeper.create( "/create/node5" , "node5" .getBytes(), acls, CreateMode.PERSISTENT); |
创建持久化有序节点:
输出结果为节点路径
1 | String s = zooKeeper.create( "/create/node6" , "node6" .getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);<br>System.out.println(s); |
CreateMode枚举类介绍:
异步方式创建节点:
1 2 3 4 5 6 7 8 | //异步方式创建节点 zooKeeper.create( "/create/node6" , "node6" .getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new AsyncCallback.StringCallback() { public void processResult( int rc, String path, Object ctx, String name) { System.out.println(rc+ "==" +path+ "==" +ctx+ "==" +name); } }, "context...." ); Thread.sleep( 5000 ); System.out.println( "end..." ); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix