Zookeeper之javaAPI的使用

Java程序操做Zookeeper

1.建立java項目並導入相關jar包

主要jar包在主目錄下 在這裏插入圖片描述 項目須要的相關依賴的jar包在zookeeper的解壓文件的lib目錄下就有 在這裏插入圖片描述 將這幾個jar包導入項目中 在這裏插入圖片描述java

<dependency>
	<groupId>org.apache.zookeeper</groupId>
	<artifactId>zookeeper</artifactId>
	<version>3.4.6</version>
	<type>pom</type>
</dependency>
<dependency>
	<groupId>com.github.sgroschupf</groupId>
	<artifactId>zkclient</artifactId>
	<version>0.1</version>
</dependency>
<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.12</version>
</dependency>

API簡單使用

2.1配置Zookeeper對象

// zookeeper的服務器地址,配置conf是用主機名,這裏同樣
	private String connectString ="zek00:2181,zek01:2181,zek02:2181";
	// 鏈接超時時間
	private int sessionTimeout = 2000;
	
	private ZooKeeper zk = null;

	/**
	 * 設置zookeeper對象
	 * @throws IOException 
	 */
	@Before
	public void setZookeeper() throws IOException {
		
		zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
			/**
			 * 事件觸發的回調方法
			 */
			@Override
			public void process(WatchedEvent event) {
				
			}
		});
		System.out.println("---"+zk);
	}

經常使用API操做

/**
 * create 方法參數
 *    第一個參數 路徑
 *    第二個參數  值  bytes
 *    第三個參數  對節點的訪問控制
 *    第四個參數  節點的類型 短暫  永久  序號        
 * @throws KeeperException
 * @throws InterruptedException
 */
@Test
public void create() throws KeeperException, InterruptedException {
	String path = zk.create("/app3", "123".getBytes(), 
			Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	System.out.println(path);
}
/**
 * 判斷節點是否存在
 * @throws InterruptedException 
 * @throws KeeperException 
 */
@Test
public void exist() throws KeeperException, InterruptedException{
	// 設置爲 true 會調用 zk中的監聽器
	Stat stat = zk.exists("/app1", true);
	if(stat!=null){
		
		System.out.println("存在。。。。"+stat.getDataLength());
	}else{
		System.out.println("不存在。。。");
	}
}
/**
 * 獲取子節點
 * @throws InterruptedException 
 * @throws KeeperException 
 */
@Test
public void getChilren() throws KeeperException, InterruptedException{
	List<String> list = zk.getChildren("/", true);
	for (String s : list) {
		System.out.println(s);
	}
}

/**
 * 獲取節點的內容
 * @throws InterruptedException 
 * @throws KeeperException 
 */
@Test
public void getData() throws KeeperException, InterruptedException{
	
	byte[] b = zk.getData("/app1", false, null );
	System.out.println(new String(b));
}

/**
 * 修改節點內容
 * 	  version -1 自動維護
 * @throws InterruptedException 
 * @throws KeeperException 
 */
@Test
public void setData() throws KeeperException, InterruptedException{
	Stat s = zk.setData("/app1", "test".getBytes(), -1);
}
/**
 * 刪除節點
 *    非空節點刪除不掉
 * @throws InterruptedException
 * @throws KeeperException
 */
@Test
public void deleteNode() throws InterruptedException, KeeperException{
	zk.delete("/app1", -1);
}

監聽器的使用

@Before
public static void setUpBeforeClass() throws Exception {
	zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
		/**
		 * 監聽器
		 */
		@Override
		public void process(WatchedEvent event) {
			System.out.println("事件觸發...");
		}
	});
}


/**
 * 獲取子節點
 * getChildren(path,watch?)監聽的事件是:節點下的子節點增減變化事件
 * @throws InterruptedException 
 * @throws KeeperException 
 */
@Test
public void getChilren() throws KeeperException, InterruptedException{
	List<String> list = zk.getChildren("/", new Watcher() {
		
		@Override
		public void process(WatchedEvent event) {
			// TODO Auto-generated method stub
			System.out.println("監控節點下的子節點被改變..."+event.getPath());
		}
	});
	for (String s : list) {
		System.out.println(s);
	}
	Thread.sleep(Long.MAX_VALUE);
}

/**
 * 獲取節點的內容
 * getData(path,watch?)監聽的事件是:節點數據變化事件
 * @throws InterruptedException 
 * @throws KeeperException 
 */
@Test
public void getData() throws KeeperException, InterruptedException{
	
	byte[] b = zk.getData("/app1", new Watcher() {
		
		@Override
		public void process(WatchedEvent event) {
			System.out.println("--數據改變了--");
			
		}
	}, null );
	System.out.println(new String(b));
	Thread.sleep(Long.MAX_VALUE);
}
相關文章
相關標籤/搜索