1. 程式人生 > >zookeeper總結之客戶端執行核心模塊

zookeeper總結之客戶端執行核心模塊

except pre 執行 strong main amp per bst ply

ZooKeeper

  ZooKeeper是客戶端操作ZooKeeper服務端的核心類。當用戶向ZooKeeperMain執行相關命令時,最終會交給ZooKeeper執行,其會將用戶請求封裝成對象,然後發送到服務端。內部使用ClientCnxn來提供與服務端的通信。 請求數據會被封裝成RequestHeader、Request對象,相應的返回結果會存儲在Response,ReplyHeader對象。

public String create(final String path, byte data[], List<ACL> acl,
            CreateMode createMode)
        
throws KeeperException, InterruptedException { final String clientPath = path; PathUtils.validatePath(clientPath, createMode.isSequential()); final String serverPath = prependChroot(clientPath); RequestHeader h = new RequestHeader(); h.setType(ZooDefs.OpCode.create); CreateRequest request
= new CreateRequest(); CreateResponse response = new CreateResponse(); request.setData(data); request.setFlags(createMode.toFlag()); request.setPath(serverPath); if (acl != null && acl.size() == 0) { throw new KeeperException.InvalidACLException(); } request.setAcl(acl); ReplyHeader r
= cnxn.submitRequest(h, request, response, null); if (r.getErr() != 0) { throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath); } if (cnxn.chrootPath == null) { return response.getPath(); } else { return response.getPath().substring(cnxn.chrootPath.length()); } }

zookeeper總結之客戶端執行核心模塊