标签:
上一篇介绍了,Zookeeper的基本概念,怎么启动,怎么解决可能遇到的几个问题。package cn.fansunion.zookeeper; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooKeeper; public class ZooKeeperTest { private static final int TIME_OUT = 3000; private static final String HOST = "localhost:2181"; public static void main(String[] args) throws Exception{ ZooKeeper zookeeper = new ZooKeeper(HOST, TIME_OUT, null); System.out.println("=========创建节点==========="); if(zookeeper.exists("/test", false) == null) { zookeeper.create("/test", "znode1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } System.out.println("=============查看节点是否安装成功==============="); System.out.println(new String(zookeeper.getData("/test", false, null))); System.out.println("=========修改节点的数据=========="); String data = "zNode2"; zookeeper.setData("/test", data.getBytes(), -1); System.out.println("========查看修改的节点是否成功========="); System.out.println(new String(zookeeper.getData("/test", false, null))); System.out.println("=======删除节点=========="); zookeeper.delete("/test", -1); System.out.println("==========查看节点是否被删除============"); System.out.println("节点状态:" + zookeeper.exists("/test", false)); zookeeper.close(); } }
<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>cn.fansunion</groupId> <artifactId>ZookeeperDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source /> <target /> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> </project>
Zookeeper入门-Java版本HelloWorld例子
标签:
原文地址:http://blog.csdn.net/fansunion/article/details/50127011