Zookeeper学习(二)——Zookeeper的安装和使用

单机安装和使用就不讲了,这里主要说一下集群的方式。

由于手头机器资源有限,所以本例采用windows下伪集群的方式(即在同一台电脑上安装多个节点)来进行说明。

 

参考:http://blackproof.iteye.com/blog/2039040

 

准备工作:

windows系统

Zookeeper的安装包:3.3.6版本

 

安装步骤:

以windows为例:

第一步:在F盘下新建3个目录:server1   server2   server3

第二步:解压Zookeeper的安装包到每一个目录下,并额外创建几个文件夹:data dataLog logs

最终目录如下:data dataLog logs zookeeper-3.3.6

第三步:进入每个server的data目录,创建一个myid的文件,里面写入一个数字,比如server1就写一个1,server2对应myid文件就写入2,server3对应myid文件就写个3

第四步:进入zookeeper-3.3.6/conf目录,那么如果是刚下过来,会有3个文件,configuration.xml, log4j.properties,zoo_sample.cfg,这3个文件我们首先要做的就是在这个目录创建一个zoo.cfg的配置文件,当然你可以把zoo_sample.cfg文件改成zoo.cfg,

例如server1里的配置内容如下所示:  

# 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.
dataDir=F:/server1/data
dataLogDir=F:/server1/dataLog
# the port at which the clients will connect
clientPort=2181
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
 server2里的配置内容如下所示:   
# 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.
dataDir=F:/server2/data
dataLogDir=F:/server2/dataLog
# the port at which the clients will connect
clientPort=2182
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
  server3里的配置内容如下所示:  
# 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.
dataDir=F:/server3/data
dataLogDir=F:/server3/dataLog
# the port at which the clients will connect
clientPort=2183
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
      需要注意的是clientPort这个端口如果你是在1台机器上部署多个server,那么每台机器都要不同的clientPort,比如我server1是2181,server2是2182,server3是2183,dataDir和dataLogDir也需要区分下。

  

      配置好之后,就可以启动了,linux环境下直接执行命令:./zkServer.sh start即可启动一个server。

(此时报错是因为其他server还没起来,连不上导致的,待其他server启动完成之后就不会报错了)

windows下的启动脚本自身没有带,需要我们手动写一个,例如server3的启动脚本:

@echo off
REM Licensed to the Apache Software Foundation (ASF) under one or more
REM contributor license agreements.  See the NOTICE file distributed with
REM this work for additional information regarding copyright ownership.
REM The ASF licenses this file to You under the Apache License, Version 2.0
REM (the "License"); you may not use this file except in compliance with
REM the License.  You may obtain a copy of the License at
REM
REM     http://www.apache.org/licenses/LICENSE-2.0
REM
REM Unless required by applicable law or agreed to in writing, software
REM distributed under the License is distributed on an "AS IS" BASIS,
REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
REM See the License for the specific language governing permissions and
REM limitations under the License.

setlocal
call "%~dp0zkEnv.cmd"

set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
set ZOOCFG=F:/server3/zookeeper-3.3.6/conf/zoo.cfg
echo on
java "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*

endlocal

 其他server改一下相应的路径即可。

 

Zookeeper的使用:

1)在自带cli客户端上使用命令:

        所有server都启动正常之后,就可以启动cli客户端进行操作了,执行zkCli.cmd脚本即可启动一个client(linux环境下是zkCli.sh)。常用基本命令如下:

ls:查看当前节点数据,例如: ls /   会列出当前目录列表

ls2:查看当前节点数据并能看到更新次数等数据

create:创建一个节点,例如:create /zk  会创建一个zk节点

get:得到一个节点,包含数据和更新次数等数据,例如:get /zk

set:修改节点,例如:  set /zk mydata

delete:删除一个节点,例如:delete /zk

help命令可以查看帮助。

2)在Java里使用API操作Zookeeper:

先引入zk的依赖:

<dependency>
	<groupId>org.apache.zookeeper</groupId>
	<artifactId>zookeeper</artifactId>
	<version>3.3.6</version>
</dependency>

 测试类:

public class TestZookeeper {
	
	public static final String ZK_SERVER_IP = "127.0.0.1";
	public static final String ZK_SERVER_PORT = "2181";
	public static final int SESSION_TIMEOUT = 500000;
	
	public static void main(String[] args) {
		try {
			//创建一个Zookeeper实例,第一个参数为目标服务器地址和端口,第二个参数为Session超时时间,第三个为节点变化时的回调方法
			ZooKeeper zk = new ZooKeeper(ZK_SERVER_IP + ":" + ZK_SERVER_PORT, SESSION_TIMEOUT, new Watcher(){

				// 监控所有被触发的事件
				@Override
				public void process(WatchedEvent event) {
					System.out.println("watcher doing something");
				}
			});
			
			// 创建一个节点,不进行ACL权限控制,节点为永久性的(即使客户端shutdown了,节点也不会消失)
			String createResult = zk.create("/root", "myData".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
			System.out.println(createResult);
			
			// 再在上述节点下创建一个子节点,也不进行ACL权限控制,节点为永久性的
			createResult = zk.create("/root/childone", "childoneData".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
			System.out.println(createResult);
			
			// 取得/root节点下的子节点名称,返回List<String>
			List<String> list = zk.getChildren("/root", true);
			
			// 取得/root/childone节点下的数据,返回byte[]
			byte[] byteArr = zk.getData("/root/childone", true, null);
			
			// 修改/root/childone节点下的数据,第三个参数为版本,如果为-1表示无视版本,直接修改
			zk.setData("/root/childone", "childoneData2".getBytes(), -1);
			
			// 删除/root/childone节点,第二个参数为版本,如果为-1表示无视版本,直接删除
			zk.delete("/root/childone", -1);
			
			// 关闭连接
			zk.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}

猜你喜欢

转载自guwq2014.iteye.com/blog/2365706