• Zookeeper源码解读


    1.1. 客户端源码

    1.1.1. 总体流程

     

     

    启动客户端 zkCli.sh文件里面的配置

    实际运行

     

      public static void main(String args[])
    
            throws KeeperException, IOException, InterruptedException
    
        {
    
            ZooKeeperMain main = new ZooKeeperMain(args);  
    
            main.run();
    
        }

    Main方法流程:

    1. new ZooKeeperMain 对象
    2. 调用run()方法

     

    ZookeeperMain的构造方法里面,重点是

      

     public ZooKeeperMain(String args[]) throws IOException, InterruptedException {
    
            cl.parseOptions(args);
    
            System.out.println("Connecting to " + cl.getOption("server"));
    
         //连接上ZK
    
            connectToZK(cl.getOption("server"));
    
        }
      protected void connectToZK(String newHost) throws InterruptedException, IOException {
    
            if (zk != null && zk.getState().isAlive()) {
    
                zk.close();
    
            }
    
            host = newHost;
    
            boolean readOnly = cl.getOption("readonly") != null;
    
            zk = new ZooKeeper(host,
    
                     Integer.parseInt(cl.getOption("timeout")),
    
                     new MyWatcher(), readOnly);
    
        }
    
     

    最终在connectToZK方法里面也就是使用原生的Zk客户端进行连接的。

       public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher,
    
                boolean canBeReadOnly)
    
            throws IOException
    
        {
    
            LOG.info("Initiating client connection, connectString=" + connectString
    
                    + " sessionTimeout=" + sessionTimeout + " watcher=" + watcher);
    
     
    
            watchManager.defaultWatcher = watcher;
    
     
    
            ConnectStringParser connectStringParser = new ConnectStringParser(
    
                    connectString);
    
            HostProvider hostProvider = new StaticHostProvider(
    
                    connectStringParser.getServerAddresses());
    
            cnxn = new ClientCnxn(connectStringParser.getChrootPath(),
    
                    hostProvider, sessionTimeout, this, watchManager,
    
    //获得和服务端连接的对象
    
                    getClientCnxnSocket(), canBeReadOnly);
    
             //调用start()
    
            cnxn.start();
    
        }

    public void start() {

        sendThread.start();

        eventThread.start();

    }

     

    1.1.2. 开启SendThread线程

    org.apache.zookeeper.ClientCnxn.SendThread#run

     

    1.1.3. 开启EventThread

    org.apache.zookeeper.ClientCnxn.EventThread.run

     

    1.1.4. 总结:

     

    1.2. 服务端源码(单机)

    1.2.1. 总体流程

     

    1.2.2. 具体处理流程

  • 相关阅读:
    借阅的列表
    列表中的方法
    对编程本质的认识
    列表技能
    链表策略
    在pycharm里添加解释器路径
    数据类型转化
    罗列内存中的数字
    【python3的学习之路十】模块
    【python3的学习之路九】函数式编程
  • 原文地址:https://www.cnblogs.com/Soy-technology/p/11391746.html
Copyright © 2020-2023  润新知