Zookeeper原始碼除錯環境踩坑記錄
阿新 • • 發佈:2020-09-14
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方法。
發現報如下錯誤:
冒失缺失了一個類,匯入對應的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()); }