Zookeeper的paxos,ZAB,watch和API
Paxos
参考文章:https://www.douban.com/note/208430424/
参考文章:https://zh.wikipedia.org/zh-cn/Paxos%E7%AE%97%E6%B3%95
参考文章:https://www.cnblogs.com/BigBender/p/14470535.html
Paxos算法是莱斯利·兰伯特,于1990年提出的一种基于消息传递且具有高度容错特性的共识(consensus)算法。
需要注意的是,Paxos常被误称为“一致性算法”。但是“一致性(consistency)”和“共识(consensus)”并不是同一个概念
Paxos是一个共识(consensus)算法
ZAB
参考文章:https://www.cnblogs.com/BigBender/p/14470604.html
参考文章:https://www.cnblogs.com/frankltf/p/10392151.html
其中一个节点挂掉之后,3个节点会不断通过3888端口通信
然后投票,版本号低的票会被淘汰,最终推选出一个合适的leader
watch
官方文档:https://zookeeper.apache.org/doc/r3.6.2/zookeeperProgrammers.html#ch_zkWatches
API示例
官方地址:https://zookeeper.apache.org/doc/r3.6.2/apidocs/zookeeper-server/index.html
package com.test.zookeeper; import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import java.util.concurrent.CountDownLatch; /** * Hello world! * */ public class App { public static void main( String[] args ) throws Exception { System.out.println( "Hello World!" ); //zk是有session概念的,没有连接池的概念 //watch:观察,回调 //watch的注册值发生在 读类型调用,get,exites。。。 //第一类:new zk 时候,传入的watch,这个watch,session级别的,跟path 、node没有关系。 final CountDownLatch cd = new CountDownLatch(1); final ZooKeeper zk = new ZooKeeper("192.168.150.11:2181,192.168.150.12:2181,192.168.150.13:2181,192.168.150.14:2181", 3000, new Watcher() { //Watch 的回调方法! @Override public void process(WatchedEvent event) { Event.KeeperState state = event.getState(); Event.EventType type = event.getType(); String path = event.getPath(); System.out.println("new zk watch: "+ event.toString()); switch (state) { case Unknown: break; case Disconnected: break; case NoSyncConnected: break; case SyncConnected: System.out.println("connected"); cd.countDown(); break; case AuthFailed: break; case ConnectedReadOnly: break; case SaslAuthenticated: break; case Expired: break; } switch (type) { case None: break; case NodeCreated: break; case NodeDeleted: break; case NodeDataChanged: break; case NodeChildrenChanged: break; } } }); cd.await(); ZooKeeper.States state = zk.getState(); switch (state) { case CONNECTING: System.out.println("ing......"); break; case ASSOCIATING: break; case CONNECTED: System.out.println("ed........"); break; case CONNECTEDREADONLY: break; case CLOSED: break; case AUTH_FAILED: break; case NOT_CONNECTED: break; } String pathName = zk.create("/test", "olddata".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); final Stat stat = new Stat(); byte[] node = zk.getData("/test", new Watcher() { @Override public void process(WatchedEvent event) { System.out.println("getData watch: " + event.toString()); try { //true default Watch 被重新注册 new zk的那个watch zk.getData("/test", this, stat); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }, stat); System.out.println(new String(node)); //触发回调 Stat stat1 = zk.setData("/test", "newdata".getBytes(), 0); //还会触发吗? Stat stat2 = zk.setData("/test", "newdata01".getBytes(), stat1.getVersion()); System.out.println("-------async start----------"); zk.getData("/test", false, new AsyncCallback.DataCallback() { @Override public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) { System.out.println("-------async call back----------"); System.out.println(ctx.toString()); System.out.println(new String(data)); } },"abc"); System.out.println("-------async over----------"); Thread.sleep(2222222); } }
ACL Permission
官方文档:https://zookeeper.apache.org/doc/r3.6.2/apidocs/zookeeper-server/index.html
- ANYONE_ID_UNSAFE,This Id represents anyone
- AUTH_IDS,This Id is only usable to set ACLs,It will get substituted with the Id's the client authenticated wit
- OPEN_ACL_UNSAFE,This is a completely open ACL
- CREATOR_ALL_ACL,This ACL gives the creators authentication id's all permissions
- READ_ACL_UNSAFE,This ACL gives the world the ability to read
CoundDownLatch
论读书
睁开眼,书在面前 闭上眼,书在心里
睁开眼,书在面前 闭上眼,书在心里