Zookeeper Learning (2) - Installation and Use of Zookeeper

The installation and use of a single machine will not be discussed. Here we will mainly talk about the clustering method.

Due to the limited resources of the machine at hand, this example uses a pseudo-cluster method under Windows (that is, installing multiple nodes on the same computer) to illustrate.

 

Reference: http://blackproof.iteye.com/blog/2039040

 

Ready to work:

windows system

Zookeeper installation package: version 3.3.6

 

installation steps:

Take windows as an example:

Step 1: Create three new directories under the F drive: server1 server2 server3

Step 2: Unzip the Zookeeper installation package to each directory, and create several additional folders: data dataLog logs

The final directory is as follows: data dataLog logs zookeeper-3.3.6

Step 3: Enter the data directory of each server, create a myid file, and write a number in it, such as server1, write a 1, server2 corresponds to myid file, write 2, server3 corresponds to myid file, write 3

Step 4: Enter the zookeeper-3.3.6/conf directory, then if you just downloaded it, there will be 3 files, configuration.xml, log4j.properties, zoo_sample.cfg, the first thing we need to do for these 3 files is to This directory creates a zoo.cfg configuration file, of course, you can change the zoo_sample.cfg file to zoo.cfg,

For example, the configuration content in server1 is as follows:  

# 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
 The configuration content in server2 is as follows:   
# 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
  The configuration content in server3 is as follows:  
# 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
      It should be noted that if you deploy multiple servers on one machine, each machine needs a different clientPort. For example, my server1 is 2181, server2 is 2182, server3 is 2183, and dataDir and dataLogDir also need to be distinguished. Down.

  

      After the configuration is complete, it can be started. In the Linux environment, execute the command directly: ./zkServer.sh start to start a server.

(The error is reported at this time because the other servers have not yet started and cannot be connected. After the other servers are started, the error will not be reported.)

The startup script under windows itself does not have it, we need to write one manually, such as the startup script of 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

 Other servers can change the corresponding path.

 

Use of Zookeeper:

1) Use the command on the built-in cli client:

        After all servers are started normally, you can start the cli client to operate, and execute the zkCli.cmd script to start a client (zkCli.sh in the Linux environment). Common basic commands are as follows:

ls: View the current node data, for example: ls / will list the current directory list

ls2: View the current node data and see data such as the number of updates

create: create a node, for example: create /zk will create a zk node

get: get a node containing data such as data and update times, for example: get /zk

set: modify the node, for example: set /zk mydata

delete: delete a node, for example: 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();
		}
		
	}

}

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326576956&siteId=291194637