Zookeeper 在Windows下的安裝過程及測試

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/shfei10100/article/details/81028474

  1. 安裝jdk
  2. 安裝Zookeeper. 在https://archive.apache.org/dist/zookeeper/下載對應版本(測試使用的是zookeeper-3.4.6版本
  3. 解壓zookeeper-3.4.6至F:\qsf\zookeeper\zookeeper-3.4.6
  4. F:\qsf\zookeeper\ 新建data及log目錄
  5. ZooKeeper的安裝模式分爲三種,分別爲:單機模式(stand-alone)、集羣模式和集羣僞分佈模式。ZooKeeper 單機模式的安裝相對比較簡單,如果第一次接觸ZooKeeper的話,建議安裝ZooKeeper單機模式或者集羣僞分佈模式。

  6. 安裝單擊模式。 至F:\qsf\zookeeper\zookeeper-3.4.6\conf 複製 zoo_sample.cfg 並粘貼到當前目錄下,命名zoo.cfg.

  7. 編輯zoo.cfg.修改如下配置 

    # The number of milliseconds of each tick
    tickTime=2000
    # The number of ticks that the initial 
    # synchronization phase can take
    initLimit=10
    # The number of ticks that can pass between 
    # sending a request and getting an acknowledgement
    syncLimit=5
    # the directory where the snapshot is stored.
    # do not use /tmp for storage, /tmp here is just 
    # example sakes.
    dataDir=F:\\qsf\\zookeeper\\data
    dataLogDir=F:\\qsf\\zookeeper\\data
    # the port at which the clients will connect
    clientPort=2181
    # the maximum number of client connections.
    # increase this if you need to handle more clients
    #maxClientCnxns=60
    #
    # Be sure to read the maintenance section of the 
    # administrator guide before turning on autopurge.
    #
    # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
    #
    # The number of snapshots to retain in dataDir
    #autopurge.snapRetainCount=3
    # Purge task interval in hours
    # Set to "0" to disable auto purge feature
    #autopurge.purgeInterval=1dataDir=F:\qsf\zookeeper\data
    dataLogDir=F:\qsf\zookeeper\data
  8. cmd命令下進入F:\qsf\zookeeper\zookeeper-3.4.6\\bin目錄下運行zkserver.cmd.如下圖所示:
  9. 運行zkcli.cmd.如下圖所示: 
  10. 之後在搭建集羣模式和集羣僞分佈模式
package com.demo;

import java.io.IOException;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

public class ZooKeeperTest {

	public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
		ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", 30000, new TestWatcher());
		String node = "/node";
		Stat stat = zk.exists(node, false);
		
		if(null==stat) {
			//創建節點
			String createResult = zk.create(node, "test".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
			System.out.println(createResult);
		}
		byte[] data = zk.getData(node, false, stat);
		System.out.println(new String(data));
		zk.close();
		
	}
	
}

class TestWatcher implements Watcher{

	@Override
	public void process(WatchedEvent event) {
		// TODO Auto-generated method stub
		System.out.println("*****************************");
		System.out.println("path:  " + event.getPath());
		System.out.println("type:  " + event.getType());
		System.out.println("state:  " + event.getState());
		System.out.println("*****************************");
	}
	
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章