zookeeper-3.4.14 Installation Document

zookeeper installation

1. Upload installation package to / opt directory

2. Decompression:

tar -zxf /opt/zookeeper-3.4.14.tar.gz -C ./

3. Configure environment variables:

vim  /etc/profile

export ZK_HOME=/opt/zookeeper-3.4.14
export PATH=$ZK_HOME/bin:

4. Modifying configuration files

(1)Copy the sample file configuration file
	cp zoo_sample.cfg zoo.cfg
(2)Modify the configuration file:
	tickTime=2000
	initLimit=10
	syncLimit=5
	dataDir=/home/hadoop/zoodata
	clientPort=2181
	# IP: Port Number for Communication with leader: Port Number for Communication with Slave Node
	server.0=master:2888:3888
	server.1=slave1:2888:3888
	server.2=slave2:2888:3888

5. Enter the dataDir path configured in the zoo.cfg file

cd  /home/hadoop/zoodata
 Execute in master: echo 0 > myid
 Execute in slave1: echo 1 > myid
 Execute in slave 2: echo 2 > myid

6. Copy zookeeper configuration file remotely:

scp  -r  /opt/zookeeper-3.4.14   slave1:/opt/
scp  -r  /opt/zookeeper-3.4.14   slave2:/opt/

zookeeper server-side instructions:

zkServer.sh start
 zkServer.sh stop stop
 zkServer.sh restart restart
 zkServer.sh status view status

After zkServer starts successfully, view the process through JPS:

QuorumPeerMain

zookeeper client instruction:

zkCli.sh Connect to Local zk Server
 zkClie.sh-server host:port client connects to remote server

After entering the client:

Ls path [watch] view child node information for the specified node (add listeners)
Create path data to create specified nodes and corresponding data
 Get path [watch] view data under the specified node (add listeners)
Set path data modifies data under a specified node
 RMR path deletes the specified node (it connects the child node to delete one piece)

Node type:
Persistent nodes, temporary nodes, sequential nodes, non-sequential nodes

Create-s path data to create persistent sequential nodes (default non-sequential nodes)
Create-e Ptah data creates temporary out-of-order nodes (which are automatically deleted when the client creating the node goes offline)

zookeeper Java API:

public class Demo2 {
	ZooKeeper zk = null;

public void init() throws Exception {
	String connStr = "192.168.56.101:2181";
	// Parameters: IP: port connected to zk server, timeout, listening
	zk = new ZooKeeper(connStr, 2000000, new Watcher() {

		@Override
		public void process(WatchedEvent event) {
			if(event.getState() == KeeperState.SyncConnected  // Judging whether monitoring is synchronized
					&&
				event.getType() == EventType.NodeChildrenChanged) { // Judging the type of listening: child node changes
				
				System.out.println("Changes in child nodes");
				// Re-monitor
				try {
					getChildren();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else if(event.getState() == KeeperState.SyncConnected
					&&
				event.getType() == EventType.NodeDataChanged) { // Listening Type: Node Data Change
				
				System.out.println("Node data changes");
				// Re-monitor
				try {
					getData();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	});
}

/**
 * Create Nodes
 * @throws Exception
 */
public void createNode() throws Exception {
	// Parameters: node path, data stored by nodes, permissions, node type
	zk.create("/aa", "hello".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}

/**
 * Get the data for the specified node
 * @throws Exception
 */
public void getData() throws Exception {
	// Parameters: node path, whether to add listeners, data version
	byte[] data = zk.getData("/aa", true, null);
	System.out.println(new String(data));
}

/**
 *  Gets the child node of the specified node
 * @throws Exception
 */
public void getChildren() throws Exception {
	// Parameter: Node path, whether to add listeners
	List<String> children = zk.getChildren("/aa", true);
	for(String str : children) {
		// The returned node name is not included/
		System.out.println(str);
	}
}

public static void main(String[] args) throws Exception {
	Demo2 demo2 = new Demo2();
	demo2.init();
	demo2.getChildren();
	demo2.getData();
	
	Thread.sleep(Integer.MAX_VALUE);
}
	
}

zookeeper application scenario: server dynamic awareness

Server side:

public class QueryTimeServer {
	ZooKeeper zk = null;

public void init() throws IOException {
	String connStr = "192.168.56.101:2181";
	zk = new ZooKeeper(connStr, 2000000, null);
}

// Register IP, port
public void registSever(String ip, String port) throws Exception {
	// Determine whether the / Servers node is stored
	Stat exists = zk.exists("/Servers", false);
	if(exists == null) {
		// If the / Servers node does not exist, you need to create: persistent out-of-order nodes
		zk.create("/Servers", 
				null, 
				Ids.OPEN_ACL_UNSAFE, 
				CreateMode.PERSISTENT);
	}
	
	// Register IP, PORT to / Servers/server00000X nodes
	// This node is a temporary sequential node
	zk.create("/Servers/server", 
			(ip+":"+port).getBytes(), 
			Ids.OPEN_ACL_UNSAFE, 
			CreateMode.EPHEMERAL_SEQUENTIAL);
	
	System.out.println(ip+":"+port+"Server to zk Successful registration!");
}

// TODO Business Processing -- Response Time Request

public static void main(String[] args) throws Exception {
	QueryTimeServer server = new QueryTimeServer();
	server.init();
	server.registSever(args[0], args[1]);

	// TODO Business Processing -- Response Time Request
	
	Thread.sleep(Integer.MAX_VALUE);
	}
}

Client:

public class Client {
	ZooKeeper zk = null;
	// Server online list
	List<String> online = new ArrayList<String>();
	
public void init() throws IOException {
	String connStr = "192.168.56.101:2181";
	zk = new ZooKeeper(connStr, 2000000, new Watcher() {

		@Override
		public void process(WatchedEvent event) {
			if(event.getState() == KeeperState.SyncConnected
					&&
				event.getType() == EventType.NodeChildrenChanged) {
				
				// Get the latest online list
				try {
					getOnline();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	});
}

// Get a list of servers
public void getOnline() throws Exception {
	List<String> servers = new ArrayList<String>();
	// Get the child nodes under the / Servers node
	List<String> children = zk.getChildren("/Servers", true);
	for(String str : children) {
		byte[] data = zk.getData("/Servers/"+str, false, null);
		String msg = new String(data);
		
		servers.add(msg);
	}
	
	online = servers;
	
	System.out.println("Current online server:"+Arrays.toString(online.toArray()));
}

// TODO Business Processing--Time Query

public static void main(String[] args) throws Exception {
	Client client = new Client();
	client.init();
	client.getOnline();

	// TODO Business Processing--Time Query

	Thread.sleep(Integer.MAX_VALUE);
	}
}

Use the installed zookeeper cluster in HBase:
1. Modify the hbase-env.sh file on the master and modify the following:

vim  /opt/hbase-1.2.6/conf/hbase-env.sh

export HBASE_MANAGES_ZK=false

2. Copy hbase-env.sh to slave1 and slave2 virtual machines

scp  /opt/hbase-1.2.6/conf/hbase-env.sh  slave1:/opt/hbase-1.2.6/conf/
scp  /opt/hbase-1.2.6/conf/hbase-env.sh  slave2:/opt/hbase-1.2.6/conf/

3. Startup sequence: HDFS, ZooKeeper, Hbase

start-dfs.sh (Start on master)
ZkServer.sh start
 start-hbase.sh (Start on master)

Keywords: Zookeeper HBase vim Hadoop

Added by cage on Wed, 21 Aug 2019 09:48:25 +0300