Zookeeper源码阅读--环境搭建、启动服务demo

下载Zookeeper源码

下载地址https://github.com/apache/zookeeper

安装ANT

变量备注
ANT_HOMED:\apache-ant-1.10.2新建变量
Path;%ANT_HOME%\bin没有就新建变量,有则在内容后追加
CLASSPATH;%ANT_HOME%\lib没有就新建变量,有则在内容后追加
  • 验证ant是否安装成功
$ ant -version
Apache Ant(TM) version 1.10.2 compiled on February 3 2018

使用ant将Zookeeper源码编译成Eclipse工程

  • 使用:ant eclipse命令
$ ant eclipse
Buildfile: E:\workspace\git\zookeeper\build.xml

ant-eclipse-download:
      [get] Getting: http://downloads.sourceforge.net/project/ant-eclipse/ant-eclipse/1.0/ant-eclipse-1.0.bin.tar.bz2
      [get] To: E:\workspace\git\zookeeper\src\java\ant-eclipse-1.0.bin.tar.bz2
      [get] http://downloads.sourceforge.net/project/ant-eclipse/ant-eclipse/1.0/ant-eclipse-1.0.bin.tar.bz2 moved to https://nchc.dl.sourceforge.net/project/ant-eclipse/ant-eclipse/1.0/ant-eclipse-1.0.bin.tar.bz2

BUILD FAILED
E:\workspace\git\zookeeper\build.xml:1730: Redirection detected from http to https. Protocol switch unsafe, not allowed.

1.下载ant-eclipse-1.0.bin.tar.bz2失败,将源码build.xml中的
get src=”http://downloads.sourceforge.net/project/ant-eclipse/ant-eclipse/1.0/ant-eclipse-1.0.bin.tar.bz2” 替换成如下地址
get src=”http://ufpr.dl.sourceforge.net/project/ant-eclipse/ant-eclipse/1.0/ant-eclipse-1.0.bin.tar.bz2
2.再次执行ant eclipse命令,等待时间有点长,等编译结束后即可导入eclipse中

  • 编译总耗时5分49秒
BUILD SUCCESSFUL
Total time: 5 minutes 49 seconds

导入项目

  1. 导入项目

    《Zookeeper源码阅读--环境搭建、启动服务demo》 daoru.png

  2. 设置jdk版本为1.7

    《Zookeeper源码阅读--环境搭建、启动服务demo》 1.7.png

  3. 导入成功

    《Zookeeper源码阅读--环境搭建、启动服务demo》 import.png

运行Zookeeper

  1. main方法:org.apache.zookeeper.server.ZooKeeperServerMain.main(String[])
  2. main方法设置传参

拷贝config下的zoo_sample.cfg 文件,设置dataDir为本地目录,其他使用默认配置
设置程序参数为当前拷贝的zoo.cfg文件路径

《Zookeeper源码阅读--环境搭建、启动服务demo》 config.png

  1. 启动server

《Zookeeper源码阅读--环境搭建、启动服务demo》 start.png

启动客户端

  1. 通过客户端main函数传入参数和命令:org.apache.zookeeper.ZooKeeperMain.main(String[])

    《Zookeeper源码阅读--环境搭建、启动服务demo》 org.apache.zookeeper.ZooKeeperMain.main(String[]).png

传入get /root命令获取/root下的数据

  1. 在main方法中创建客户端,会通过parseOptions方法解析参数
  public ZooKeeperMain(String args[]) throws IOException, InterruptedException {
        cl.parseOptions(args); // 解析参数
        System.out.println("Connecting to " + cl.getOption("server"));
        connectToZK(cl.getOption("server"));
    }

  public boolean parseOptions(String[] args) {
            List<String> argList = Arrays.asList(args);
            Iterator<String> it = argList.iterator();

            while (it.hasNext()) {
                String opt = it.next();
                try {
                    if (opt.equals("-server")) { // - 开头的为参数
                        options.put("server", it.next()); // 参数后一个内容是参数值
                    } else if (opt.equals("-timeout")) {
                        options.put("timeout", it.next());
                    } else if (opt.equals("-r")) {
                        options.put("readonly", "true");
                    }
                } catch (NoSuchElementException e){
                    System.err.println("Error: no argument found for option "
                            + opt);
                    return false;
                }

                if (!opt.startsWith("-")) { // 非-开头的是传入执行命令
                    command = opt;
                    cmdArgs = new ArrayList<String>( );
                    cmdArgs.add( command );
                    while (it.hasNext()) {
                        cmdArgs.add(it.next());
                    }
                    return true;
                }
            }
            return true;
        }
  1. 命令处理

断点可以看到命令最后被处理成字符数组传入执行

《Zookeeper源码阅读--环境搭建、启动服务demo》 get_command.png

    原文作者:maxbin
    原文地址: https://www.jianshu.com/p/7ee757d6a08a
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞