Zookeeper_ read the source code _ the first step to start zkServer (stand-alone) in the IDE

Zookeeper is open source, if you want to know more about Zookeeper or look at its source code, it is best to find the source code and start in the IDE, you can debug to see how it performed, can help you understand the principle.

Ready source

So it is easy to get to its source, such as from us get on GitHub source code or from Apache official website to get run versions compressed (containing source). Here I downloaded the 3.4.13 version of the source code from GitHub pull the idea runtime, not compile, lack of class, because some classes require data package in the class, but do not have this data package, so the compiler could not pass, it can not be tested.

 
So choose the second, then downloaded to the zookeeper-3.4.13.tar.gz decompression, and downloaded from GitHub is quite different.
 

Introduced into the IDE

Then download the source code into good idea. We need to create resources directory copy log4j.properties conf directory to the directory, otherwise the wrong start of abnormal packets can not find log4j.properties.

Ready to start

Then we find org.apache.zookeeper.server.quorum.QuorumPeerMain this class, this is the main entrance zookeeper when you start with a script is actually running this class.
 
Off-topic, start to see the script.
 
zkCli.sh script file
 

 

zkCli.cmd

 

Come back, we open this class will see there is a main method, see the comments, we need to specify the configuration file at startup.
 
/**
     * To start the replicated server specify the configuration file name on
     * the command line.要启动复制的服务器,请在命令行上指定配置文件名。
     * @param args path to the configfile 配置文件的路径
     */
    public static void main(String[] args) {
        QuorumPeerMain main = new QuorumPeerMain();
        try {
            main.initializeAndRun(args);
        } //some catchs... 
         //some codes ...
     } LOG.info(
"Exiting normally"); System.exit(0); }

配置启动参数

首先运行这个类的main方法,会报错,说非法参数哦,如下

 

配置一下,在 Program arguments里添加配置文件的路径。

正式启动

再启动就成功了

测试是否启动成功

我们开启客户端连一下

 

连上了,并且有三个节点。

这段代码是一些初始化,并判断是单机启动还是集群启动

protected void initializeAndRun(String[] args)
throws ConfigException, IOException
{
QuorumPeerConfig config = new QuorumPeerConfig();
if (args.length == 1) {
config.parse(args[0]);
}

// Start and schedule the the purge task
  DatadirCleanupManager purgeMgr = new DatadirCleanupManager(config
    .getDataDir(), config.getDataLogDir(), config
    .getSnapRetainCount(), config.getPurgeInterval());
purgeMgr.start();

if (args.length == 1 && config.servers.size() > 0) {
System.out.println("=======集群模式=======");
runFromConfig(config);
} else {
System.out.println("=======单机模式=======");
LOG.warn("Either no config or no quorum defined in config, running "
          + " in standalone mode");
ZooKeeperServerMain.main(args);
}
}

 

 

























单机版启动,到这里就结束了。篇幅原因,集群版下一篇再见。


转载请注明出处

 

Guess you like

Origin www.cnblogs.com/ibigboy/p/11353031.html