1. 程式人生 > >zookeeper的API應用

zookeeper的API應用

eclipse環境搭建

拷貝zookeeper-3.4.10.jar、jline-0.9.94.jar、log4j-1.2.16.jar、netty-3.10.5.Final.jar、slf4j-api-1.6.1.jar、slf4j-log4j12-1.6.1.jar到工程的lib目錄。並build一下,匯入工程。
拷貝log4j.properties檔案到專案根目錄

log4j.rootLogger=INFO, stdout  
log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n  
log4j.appender.logfile=org.apache.log4j.FileAppender  
log4j.appender.logfile.File=target/spring.log  
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout  
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n  

客戶端程式碼

建立ZooKeeper客戶端
package com.zyd.zook;
import java.io.IOException;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Test;

public class TestZookeeper {
	//連線zkServer
	private String connectString = "testnote01,testnote02,testnote03";
	//超時間設定
	private int sessionTimeout = 2000;
	ZooKeeper zkClient;
	
	//初始化客戶端
	@Test
	public void initClient() throws IOException {
		zkClient =new ZooKeeper(connectString, sessionTimeout, new Watcher(){

			@Override
			public void process(WatchedEvent event) {
			System.out.println(event.getType()+"--------"+event.getPath());
			}
			
		});
	}
}

結果

2018-11-17 11:01:32,638 INFO [org.apache.zookeeper.ZooKeeper] - Initiating client connection, connectString=testnote01,testnote02,testnote03 sessionTimeout=2000 [email protected]

建立子節點

/**
	 * 建立子節點
	 * @throws InterruptedException 
	 * @throws KeeperException 
	 */
	@Test
	public void create() throws KeeperException, InterruptedException{
		//路徑 資料 許可權(通過介面Ids分別所有)節點型別(臨時的 臨時帶序列號的 永久的 永久帶序列號的,具體看原始碼)
	String path =	zkClient.create("/zyd", "shuai".getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
		
		System.out.println(path);
	}

修改建立客戶端為@Test
結果

 2018-11-17 11:15:41,588 INFO [org.apache.zookeeper.ClientCnxn] - Session establishment complete on server testnote01/192.168.18.50:2181, sessionid = 0x1672083bdd80004, negotiated timeout = 4000
  None--------null
/zyd

獲取節點資訊

監聽模式為false
//獲取子節點
	@Test
	public void getChild() throws KeeperException, InterruptedException {
		List<String> children = zkClient.getChildren("/", false);
		for (String child : children) {
			System.out.println(child);
		}
	}

結果

  2018-11-17 11:37:44,417 INFO [org.apache.zookeeper.ClientCnxn] - Session establishment complete on server testnote03/192.168.18.52:2181, sessionid = 0x3671e81bdeb0000, negotiated timeout = 4000
  None--------null
zookeeper
zyd
app1
開啟監聽模式
//獲取子節點
	@Test
	public void getChild() throws KeeperException, InterruptedException {
		List<String> children = zkClient.getChildren("/", true);
		for (String child : children) {
			System.out.println(child);
		}
		Thread.sleep(Long.MAX_VALUE);
	}

呼叫的是客戶端的監聽方法

@Override
			public void process(WatchedEvent event) {
//			System.out.println(event.getType()+"--------"+event.getPath());
			
			System.out.println("------------start--------------");
			List<String> children;
			try {
				children = zkClient.getChildren("/", true);
				for (String child : children) {
					System.out.println(child);
				}
				System.out.println("------------end--------------");
			} catch (KeeperException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}
			
		});
	}

判斷子節點是否存在

//判斷節點是否存在
	
	@Test
	public void exist() throws KeeperException, InterruptedException{
		Stat stat = zkClient.exists("/zyd", false);
		System.out.println(stat == null ? "not exist":"exist");
	}
}