1. 搭建開發環境html
(http://zookeeper.apache.org/)首先下載最新版 zookeeper-3.4.5.tar.gz解壓node
2. 而後進入conf目錄修改zoo_sample.cfg爲zoo.cfg,並修改其中的內容apache
1. # The number of milliseconds of each tick ide
2. tickTime=2000 測試
3. # The number of ticks that the initial ui
4. # synchronization phase can take .net
5. initLimit=10 xml
6. # The number of ticks that can pass between htm
7. # sending a request and getting an acknowledgement ip
8. syncLimit=5
9. # the directory where the snapshot is stored.
10. # do not use /tmp for storage, /tmp here is just
11. # example sakes.
12. dataDir=/tmp/zookeeper
13. # the port at which the clients will connect
14. clientPort=2181
15. #
16. # Be sure to read the maintenance section of the
17. # administrator guide before turning on autopurge.
18. #
19. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
20. #
21. # The number of snapshots to retain in dataDir
22. #autopurge.snapRetainCount=3
23. # Purge task interval in hours
24. # Set to "0" to disable auto purge feature
25. #autopurge.purgeInterval=1
3. 編輯保存後進入bin目錄執行,點擊zkServer.cmd
4. 運行結果爲一下表示運行成功.
5. 建立Maven工程,修改pom.xml文件添加如下依賴jar包.
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>com.netflix.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.netflix.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.netflix.curator</groupId>
<artifactId>curator-test</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.netflix.curator</groupId>
<artifactId>curator-x-discovery</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
6. 編寫測試代碼
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
public class ZooKeeperTest {
public static void main(String[] args) throws Exception{
ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", 3000, null);
System.out.println("=========建立節點===========");
if(zk.exists("/test", false) == null)
{
zk.create("/test", "znode1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
System.out.println("=============查看節點是否安裝成功===============");
System.out.println(new String(zk.getData("/test", false, null)));
System.out.println("=========修改節點的數據==========");
zk.setData("/test", "zNode2".getBytes(), -1);
System.out.println("========查看修改的節點是否成功=========");
System.out.println(new String(zk.getData("/test", false, null)));
System.out.println("=======刪除節點==========");
zk.delete("/test", -1);
System.out.println("==========查看節點是否被刪除============");
System.out.println("節點狀態:" + zk.exists("/test", false));
zk.close();
}
}