zookeeper总结之客户端执行核心模块

Posted zhangwanhua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zookeeper总结之客户端执行核心模块相关的知识,希望对你有一定的参考价值。

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总结之客户端执行核心模块的主要内容,如果未能解决你的问题,请参考以下文章

zookeeper源码之客户端网络通信模块

zookeeper源码之服务端启动模块

zookeeper java客户端之curator详解

Zookeeper之-基本命令(CLI)

Spring框架之学习总结

分布式之Zookeeper核心原理详解