Zookeeper的安装和使用

1、  下载zookeeper安装文件,解压缩,放入固定目录中

下载地址:http://hadoop.apache.org/zookeeper/

下载文件:zookeeper-3.4.6.tar.gz,解压缩后,放入目录D:\ll\program\zookeeper-3.4.6

2、  修改zookeeper配置文件zoo-example.cfg改为zoo.cfg,zookeeper默认寻找zoo.cfg配置文件

Zoo.cfg在zookeeper安装文件的conf文件夹下

修改内容为:

# 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=/tmp/zookeeper
dataDir=D:\\ll\\program\\zookeeper-3.4.6\\data
dataLogDir=D:\\ll\\program\\zookeeper-3.4.6\\log
# 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=1

说明:

tickTime:这个时间是作为Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。

dataDir:顾名思义就是Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。

dataLogDir:顾名思义就是Zookeeper 保存日志文件的目录

clientPort:这个端口就是客户端连接Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。

3、  启动zookeeper

Window下命令:进入bin目录 ->zkServer.cmd

Linux下命令:进入bin目录 -> zkServer.sh

示例:在windows的命令行下执行D:\ll\program\zookeeper-3.4.6\bin>zkServer.cmd
说明:头两次执行失败,修改路径后仍不成功,第三次手动添加data和log目录,运行,执行成功

4、检查zookeeper是否已经在服务,通过netstat – ano 命令查看是否有你配置的 clientPort 端口号在监听服务

示例:D:\ll\program\zookeeper-3.4.6\bin>netstat-ano

4、  Zookeeper启动成功后,编写java代码

需要两个jar包:zookeeper-3.4.6.jarslf4j-api-1.6.1.jar,在zookeeper-3.4.6.tar.gz中都可找到

package test;

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.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;

public class ZookeeperTest {
	// 会话超时时间,设置为与系统默认时间一致
	private final static int SESSION_TIMEOUT = 30000;
	
	//创建zookeeper实例
	ZooKeeper zk;
	
	//创建watch实例
	Watcher wa = new Watcher(){
		public void process(WatchedEvent event){
			System.out.print(event.toString());
		}
	};
	
	//初始化zookeeper实例
	private void createZkInstance() throws IOException{
		zk = new ZooKeeper("localhost:2181",ZookeeperTest.SESSION_TIMEOUT , this.wa);
	}
	
	private void ZKOptions() throws KeeperException, InterruptedException{
		System.out.println("/n1. 创建 ZooKeeper 节点 (znode : zoo2, 数据: myData2 ,权限:OPEN_ACL_UNSAFE ,节点类型: Persistent");
		zk.create("/zoo2","myData2".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		System.out.println("/n2. 查看是否创建成功: ");
		System.out.println(new String(zk.getData("/zoo2",false,null)));
		System.out.println("/n3. 修改节点数据 ");
		zk.setData("/zoo2", "shenlan211314".getBytes(), -1);
		System.out.println("/n4. 查看是否修改成功: ");
		System.out.println(new String(zk.getData("/zoo2", false, null)));
		System.out.println("/n5. 删除节点 ");
		zk.delete("/zoo2", -1);
		System.out.println("/n6. 查看节点是否被删除: ");
		System.out.println(" 节点状态: ["+zk.exists("/zoo2", false)+"]");
	}
	
	private void ZKClose() throws InterruptedException{
		zk.close();
	}
	
	/**
	 * @param args
	 * @author ll
	 * @throws IOException 
	 * @throws InterruptedException 
	 * @throws KeeperException 
	 * @category zookeeper
	 */
	public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
		ZookeeperTest z = new ZookeeperTest();
		z.createZkInstance();
		z.ZKOptions();
		z.ZKClose();
	}
}
启动java程序后,运行成功(前提需要zookeeper服务器启动),控制台打印如下信息:



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值