Zookeeper 在Windows下的安装过程及测试

  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("*****************************");
	}
	
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值