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()); } }