1. 下载源码
本文基于zookeeper源码3.6.2版本。
zookeeper的源码在github上(https://github.com/apache/zookeeper)
2. 找入口类
zookeeper是一个java项目,启动时有启动脚本,执行命令./zkServer conf/zoo.cfg即可。那么我们可以从这个脚本中找到最终执行的java启动类,为QuarumPeerMain这个主类。
3. 启动报错
知道主类后,我们在idea中配置zookeeper的启动参数。zookeeper启动时需要一个配置文件,我们可以从源码的根目录下的conf文件中zoo_sample.cfg复制一份改名为zoo.cfg。然后启动main方法。
发现报如下错误:
![](https://img2020.cnblogs.com/blog/461410/202009/461410-20200914151719356-1764680292.png)
冒失缺失了一个类,导入对应的maven坐标还是无法解决。最后找到一种方法,将该main方法放到test类下面即可。如QuarumPeerMainTest。将QuarumPeerMain的main方法复制到QuarumPeerMainTest类中。再次启动即可。
public static void main(String[] args) { args = new String[1]; args[0] = "zoo.cfg"; QuorumPeerMain main = new QuorumPeerMain(); try { main.initializeAndRun(args); } catch (IllegalArgumentException e) { LOG.error("Invalid arguments, exiting abnormally", e); //LOG.info(USAGE); //System.err.println(USAGE); ZKAuditProvider.addServerStartFailureAuditLog(); ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue()); } catch (QuorumPeerConfig.ConfigException e) { LOG.error("Invalid config, exiting abnormally", e); System.err.println("Invalid config, exiting abnormally"); ZKAuditProvider.addServerStartFailureAuditLog(); ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue()); } catch (FileTxnSnapLog.DatadirException e) { LOG.error("Unable to access datadir, exiting abnormally", e); System.err.println("Unable to access datadir, exiting abnormally"); ZKAuditProvider.addServerStartFailureAuditLog(); ServiceUtils.requestSystemExit(ExitCode.UNABLE_TO_ACCESS_DATADIR.getValue()); } catch (AdminServer.AdminServerException e) { LOG.error("Unable to start AdminServer, exiting abnormally", e); System.err.println("Unable to start AdminServer, exiting abnormally"); ZKAuditProvider.addServerStartFailureAuditLog(); ServiceUtils.requestSystemExit(ExitCode.ERROR_STARTING_ADMIN_SERVER.getValue()); } catch (Exception e) { LOG.error("Unexpected exception, exiting abnormally", e); ZKAuditProvider.addServerStartFailureAuditLog(); ServiceUtils.requestSystemExit(ExitCode.UNEXPECTED_ERROR.getValue()); } LOG.info("Exiting normally"); ServiceUtils.requestSystemExit(ExitCode.EXECUTION_FINISHED.getValue()); }