1. java API的使用
- 導入jar包
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
</dependency>
權限控制模式(ZooDefs.Ids.==ANYONE_ID_UNSAFE/AUTH_IDS/OPEN_ACL_UNSAFE/CREATOR_ALL_ACL/READ_ACL_UNSAFE==)
- scheme 授權對象
- ip :192.168.1.1
- Digest :username:password
- world :開放式的權限控制模式,數(shù)據(jù)節(jié)點的訪問權限隊所有用戶開放哎垦。world:anyone
- super :超級用戶漏设,可以對zookeeper上的數(shù)據(jù)節(jié)點進行操作
package com.frame.test.gp.zookeeperAPI;
import org.apache.zookeeper.Watcher;
import com.frame.test.gp.zookeeperAPI.ZookeeperAPIDemo;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
public class AuthControlDemo implements Watcher {
private final static String CONNECTSTRING="192.168.202.133:2181,192.168.202.134:2181,192.168.202.135:2181";
private static CountDownLatch countDownLatch=new CountDownLatch(1);
private static CountDownLatch countDownLatch2=new CountDownLatch(1);
private static ZooKeeper zookeeper;
private static Stat stat=new Stat();
public static void main(String[] args) throws IOException, InterruptedException, KeeperException, NoSuchAlgorithmException {
zookeeper=new ZooKeeper(CONNECTSTRING, 5000, new AuthControlDemo());
countDownLatch.await();
ACL acl=new ACL(ZooDefs.Perms.CREATE, new Id("digest","root:root"));
ACL acl2=new ACL(ZooDefs.Perms.CREATE, new Id("ip","192.168.1.1"));
List<ACL> acls=new ArrayList<>();
acls.add(acl);
//acls.add(acl2);
zookeeper.create("/auth1","123".getBytes(),acls,CreateMode.PERSISTENT);
zookeeper.addAuthInfo("digest","root:root".getBytes());
zookeeper.create("/auth1","123".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL,CreateMode.PERSISTENT);
zookeeper.create("/auth1/auth1-1","123".getBytes(),ZooDefs.Ids.CREATOR_ALL_ACL,CreateMode.PERSISTENT);
ZooKeeper zooKeeper1= new ZooKeeper(CONNECTSTRING, 5000, new AuthControlDemo());
countDownLatch.wait();;
zooKeeper1.delete("/auth1",-1);
// acl (create /delete /admin /read/write)
//權限模式: ip/Digest(username:password)/world/super
}
public void process(WatchedEvent watchedEvent) {
//如果當前的連接狀態(tài)是連接成功的鸳碧,那么通過計數(shù)器去控制
if(watchedEvent.getState()==Event.KeeperState.SyncConnected){
if(Event.EventType.None==watchedEvent.getType()&&null==watchedEvent.getPath()){
countDownLatch.countDown();
System.out.println(watchedEvent.getState()+"-->"+watchedEvent.getType());
}
}
}
}
- 節(jié)點的增刪改查瞻离,以及事件監(jiān)聽
package com.frame.test.gp.zookeeperAPI;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* @author Administrator
* @CREATE 2017/8/5 21:21
*/
public class ZookeeperAPIDemo implements Watcher {
private static final String CONNECTSTRING = "192.168.202.133:2181,192.168.202.134:2181,192.168.202.135:2181";
private static ZooKeeper zookeeper;
//concurrent 原子并發(fā)包的工具套利,需要查看
private static CountDownLatch countDownLatch = new CountDownLatch(1);
private static Stat stat = new Stat();
public static ZooKeeper getConnection() throws IOException, InterruptedException {
zookeeper = new ZooKeeper(ZookeeperAPIDemo.CONNECTSTRING, 5000, new ZookeeperAPIDemo());
countDownLatch.await();
return zookeeper;
}
/**
*代碼不復雜日裙,每個都試驗一下昂拂,配合到客戶端操作驗證結(jié)果
*/
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
zookeeper = getConnection();
//創(chuàng)建節(jié)點
String result =zookeeper.create("/node1","123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL);
zookeeper.getData("/node1",new ZookeeperAPIDemo(),stat);
System.out.println("創(chuàng)建成功:"+result);
//修改節(jié)點
zookeeper.setData("/node1","666".getBytes(),-1);
System.out.println(zookeeper.getData("/node1",new ZookeeperAPIDemo(),stat));
//刪除節(jié)點格侯,先從子節(jié)點刪除联四,不然會報錯朝墩,
zookeeper.delete("/node1",-1);
//創(chuàng)建持久化節(jié)點和子節(jié)點
String path="/node11";
zookeeper.create(path,"123".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
TimeUnit.SECONDS.sleep(1);
Stat stat=zookeeper.exists(path+"/node1",true);
if (stat == null){
zookeeper.create(path+"/node1","123".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
TimeUnit.SECONDS.sleep(1);
}
//修改子路徑
zookeeper.setData(path+"/node1","mic123".getBytes(),-1);
TimeUnit.SECONDS.sleep(1);
}
@Override
public void process(WatchedEvent watchedEvent) {
//如果當前的連接狀態(tài)時成功的收苏,那么通過計數(shù)器去控制
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
if (Event.EventType.None == watchedEvent.getType() && null == watchedEvent.getPath()) {
countDownLatch.countDown();
System.out.println("process:"+watchedEvent.getState() + "-->" + watchedEvent.getType());
} else if (watchedEvent.getType() == Event.EventType.NodeDataChanged) {
try {
System.out.println("數(shù)據(jù)變更觸發(fā)路徑:" + watchedEvent.getPath() + "->改變后的值" + zookeeper.getData(watchedEvent.getPath(), true, stat));
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else if (watchedEvent.getType() == Event.EventType.NodeChildrenChanged){ //子節(jié)點數(shù)據(jù)變化會觸發(fā)
try {
System.out.println("子節(jié)點數(shù)據(jù)變更路徑:"+watchedEvent.getPath()+"->節(jié)點的值:"+ zookeeper.getData(watchedEvent.getPath(),true,stat));
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else if (watchedEvent.getType()==Event.EventType.NodeCreated){ //創(chuàng)建子節(jié)點的時候回觸發(fā)
try {
System.out.println("節(jié)點創(chuàng)建路徑:"+watchedEvent.getPath()+"->節(jié)點的值:"+ zookeeper.getData(watchedEvent.getPath(),true,stat));
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else if (watchedEvent.getType()== Event.EventType.NodeDeleted){ //子節(jié)點刪除會觸發(fā)
System.out.println("節(jié)點刪除路徑:"+watchedEvent.getPath());
}
System.out.println("process:"+watchedEvent.getType());
}
}
}
連接狀態(tài)
- KeeperStat.Expired 在一定時間內(nèi)客戶端沒有收到服務器的通知,則認為當前的會話已經(jīng)過期
- KeeperStat.Disconnected 斷開連接的狀態(tài)
- KeeperStat.SyncConnected 客戶端和服務器端在某一個節(jié)點上建立連接懦鼠,并且完成一次version屹堰、直到同步扯键。
- KeeperStat.authFailed 授權失敗
事件類型
- NodeCreated 節(jié)點被創(chuàng)建的時候忧陪,觸發(fā)
- NodeChildrenChanged 表示子節(jié)點被創(chuàng)建、刪除延蟹、子節(jié)點的數(shù)據(jù)發(fā)生變化
- NodeDataChanged 節(jié)點數(shù)據(jù)發(fā)生變化
- NodeDeleted 節(jié)點被刪除
- Node 客戶端和服務器端連接狀態(tài)發(fā)生變化的時候阱飘,事件類型就是None
2. zkclient
package com.frame.test.gp.zookeeperAPI;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* @author Administrator
* @CREATE 2017/8/8 20:51
*/
public class zkClientApiOperatorDemo {
private final static String CONNECTSTRING="192.168.202.133:2181,192.168.202.134:2181,192.168.202.135:2181";
private static ZkClient getInstance(){
return new ZkClient(CONNECTSTRING);
}
public static void main(String[] args) throws InterruptedException {
ZkClient zkClient=getInstance();
//zkclient 提供遞歸創(chuàng)建父節(jié)點功能
zkClient.createPersistent("/zkclient/zkclient1",true);
System.out.println("success");
//刪除節(jié)點
//zkClient.delete("/auth1");
//遞歸刪除節(jié)點
//zkClient.deleteRecursive("/zkclient");
//獲取父節(jié)點下的子節(jié)點
List<String> list =zkClient.getChildren("/zkclient");
System.out.println(list);
//訂閱數(shù)據(jù)修改事件
zkClient.subscribeDataChanges("/zkclient", new IZkDataListener() {
@Override
public void handleDataChange(String dataPath, Object data) throws Exception {
System.out.println(dataPath+"->"+data);
}
@Override
public void handleDataDeleted(String dataPath) throws Exception {
System.out.println("delete path"+dataPath);
}
});
//修改節(jié)點數(shù)據(jù)
zkClient.writeData("/zkclient","zkclient-data");
TimeUnit.SECONDS.sleep(2);
zkClient.deleteRecursive("/zkclient");
TimeUnit.SECONDS.sleep(2);
}
}
3. curator
- Curator是Netflix公司開源的zookeeper客戶端;
- curator-frameword 提供了fluent風格的api
- curator-replice 提供了實現(xiàn)封裝
- curator鏈接的重試策略
- ExponentialBackOffRetry() 衰減重試
- RetryNtimes 指定最大重試次數(shù)
- RetryOneTime 僅重試一次
- RetryunitilElapsed 一直重試直到規(guī)定的時間
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.9.0</version>
</dependency>
==pom同時存在zookeeper/zkclient/guava斥杜,就會報錯,啟動不了==
package com.frame.test.gp.zookeeperAPI.curator;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
/**
* @author Administrator
* @CREATE 2017/8/8 22:17
*/
public class CuratorClientUtils {
private static CuratorFramework curatorFramework;
private final static String CONNECTSTRING = "192.168.202.133:2181,192.168.202.134:2181,192.168.202.135:2181";
public static CuratorFramework getInstance() {
//fluent 風格
curatorFramework=CuratorFrameworkFactory.builder().connectString(CONNECTSTRING).sessionTimeoutMs(5000).retryPolicy(
new ExponentialBackoffRetry(1000,3)).build();
//normal
//curatorFramework = CuratorFrameworkFactory.newClient(CONNECTSTRING, 5000, 5000, new ExponentialBackoffRetry(1000, 3));
curatorFramework.start();
return curatorFramework;
}
}
package com.frame.test.gp.zookeeperAPI.curator;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.BackgroundCallback;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.framework.api.transaction.CuratorTransactionResult;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author Administrator
* @CREATE 2017/8/8 21:25
*/
public class CuratorCreateSessionDemo {
public static void main(String[] args) throws Exception {
CuratorFramework curatorFramework = CuratorClientUtils.getInstance();
System.out.println("連接成功.......");
/**
*【curator使用異沉ば伲】KeeperErrorCode = Unimplemented for /***
*Curator的版本過高造成蔗喂,換成低一點版本即可 (version從3.0.0到2.9.1)
*/
//新增節(jié)點,所有操作都可以在zkCli.sh 中驗證高帖。
String result = curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/curator/curator1/curator11", "123".getBytes());
System.out.println(result);
//刪除子節(jié)點
curatorFramework.delete().forPath("/curator/curator1/curator11");
//遞歸刪除節(jié)點
//curatorFramework.delete().deletingChildrenIfNeeded().forPath("/curator");
//查詢數(shù)據(jù)缰儿,以及狀態(tài)
Stat stat = new Stat();
byte[] bytes = curatorFramework.getData().storingStatIn(stat).forPath("/curator");
System.out.println(new String(bytes) + "-->state:" + stat); //值 前面章節(jié)講過散址;
//更新
Stat stat1 = curatorFramework.setData().forPath("/curator", "123".getBytes());
System.out.println(stat1);
/**
* 特性:
* 異步操作
*/
ExecutorService executorService = Executors.newFixedThreadPool(1);
CountDownLatch countDownLatch=new CountDownLatch(1);
curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).inBackground(new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
//創(chuàng)建節(jié)點是有線程池處理的乖阵,異步操作
System.out.println(Thread.currentThread().getName() + "->resultCode:" + event.getResultCode() + "->" + event.getType());
countDownLatch.countDown();
}
}, executorService).forPath("/sync", "123".getBytes());
countDownLatch.await();
executorService.shutdown();
/**
* 事務操作(curator獨有的)
*/
//在同一個事務,先創(chuàng)建一個transaction節(jié)點预麸,然后又修改trans這個節(jié)點的值瞪浸。 這樣就會報錯,修改的不是同一個節(jié)點吏祸。然后ls / 对蒲,發(fā)現(xiàn)zookeeper客戶端并沒有創(chuàng)建這個節(jié)點
/*Collection<CuratorTransactionResult> resultCollection=curatorFramework.inTransaction().create().forPath("/transaction","transaction".getBytes()).and().setData().forPath("/trans","transaction roll back".getBytes()).and().commit();
for (CuratorTransactionResult curatorTransactionResult:resultCollection){
System.out.println(curatorTransactionResult.getForPath()+"->"+curatorTransactionResult.getType());
}*/
//在同一個事務,先創(chuàng)建一個transaction節(jié)點贡翘,然后又修改transaction這個節(jié)點的值蹈矮。這樣查看數(shù)據(jù)的值發(fā)現(xiàn)更新了
Collection<CuratorTransactionResult> resultCollection1=curatorFramework.inTransaction().create().forPath("/transaction","transaction".getBytes()).and().setData().forPath("/transaction","transaction update ".getBytes()).and().commit();
for (CuratorTransactionResult curatorTransactionResult:resultCollection1){
System.out.println(curatorTransactionResult.getForPath()+"->"+curatorTransactionResult.getType());
}
}
}
package com.frame.test.gp.zookeeperAPI.curator;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.zookeeper.CreateMode;
import java.util.concurrent.TimeUnit;
/**
* @author Administrator
* @CREATE 2017/8/8 23:31
*/
public class CuratorEventDemo {
/**
* 三種watcher來做節(jié)點的監(jiān)聽
* pathCache 監(jiān)視一個路徑下子節(jié)點的創(chuàng)建、刪除床估、節(jié)點數(shù)據(jù)更新
* NodeCache 監(jiān)視一個節(jié)點的創(chuàng)建、更新诱渤、刪除
* TreeCache pathCache + NOdeCache(監(jiān)視路徑下的創(chuàng)建丐巫、更新、刪除事件)勺美,緩存路徑下所有子節(jié)點的數(shù)據(jù)
*/
public static void main(String[] args) throws Exception {
CuratorFramework curatorFramework = CuratorClientUtils.getInstance();
/**
* NodeCache
*/
/*
NodeCache cache=new NodeCache(curatorFramework,"/curator",false);
cache.start();
cache.getListenable().addListener(()-> System.out.println("節(jié)點數(shù)據(jù)發(fā)生變化递胧,變化后的結(jié)果為:"+new String(cache.getCurrentData().getData())));
curatorFramework.setData().forPath("/curator","update".getBytes());*/
/**
* PathChildrenCache 監(jiān)聽/event 下的子節(jié)點
*/
PathChildrenCache childrenCache = new PathChildrenCache(curatorFramework, "/event", true);
//NORMAL: 初始時為空。
//BUILD_INITIAL_CACHE: 在這個方法返回之前調(diào)用rebuild()赡茸。
//POST_INITIALIZED_EVENT: 當Cache初始化數(shù)據(jù)后發(fā)送一個PathChildrenCacheEvent.Type #INITIALIZED事件
childrenCache.start(PathChildrenCache.StartMode.NORMAL);
childrenCache.getListenable().addListener((curatorFramework1, pathChildrenCacheEvent) -> {
switch (pathChildrenCacheEvent.getType()) {
case CHILD_ADDED:
System.out.println("增加子節(jié)點");
break;
case CHILD_REMOVED:
System.out.println("刪除子節(jié)點");
break;
case CHILD_UPDATED:
System.out.println("更新子節(jié)點");
break;
default:
break;
}
});
curatorFramework.create().withMode(CreateMode.PERSISTENT).forPath("/event","events".getBytes());
TimeUnit.SECONDS.sleep(1);
System.out.println(1);
//pathChildrenCache 會遞歸監(jiān)聽子節(jié)點時間,所以子節(jié)點創(chuàng)建打印出來了
curatorFramework.create().withMode(CreateMode.EPHEMERAL).forPath("/event/event1","1".getBytes());
TimeUnit.SECONDS.sleep(1);
curatorFramework.setData().forPath("/event/event1","666".getBytes());
TimeUnit.SECONDS.sleep(1);
curatorFramework.delete().deletingChildrenIfNeeded().forPath("/event");
System.in.read();
}
}
主要是動手缎脾,大家多動手多學習;--黑白 2017/8/9 0:21