zookeeper 源碼地址:https://github.com/apache/zookeeper.git
clone 到本地后
通過 zkServer.cmd 的
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" "-Dzookeeper.log.file=%ZOO_LOG_FILE%" "-XX:+HeapDumpOnOutOfMemoryError" "-XX:OnOutOfMemoryError=cmd /c taskkill /pid %%%%p /t /f" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*
找到 入口:QuorumPeerMain
1. 修改配置文件
(1)zoo_sample.cfg 改名為 zoo.cfg
(2)修改 dataDir 為 dataDir=/Users/kevin/Documents/workspace/ideaworkspace/zookeeper-release-3.7.0/data
2. 引入以下依賴 (如果 zookeeper-server 的 pom.xml 中沒有的話),但要將 metrics-core 的 <scope>provided</scope> 注釋掉
<dependency> <groupId>commons-cli</groupId> <artifactId>commons-cli</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>io.dropwizard.metrics</groupId> <artifactId>metrics-core</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.xerial.snappy</groupId> <artifactId>snappy-java</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-client</artifactId> <scope>provided</scope> </dependency>
3. 編譯 zookeeper-jute 模塊 並標記 target 中 generated-sources 中的 java 為 Generated Sources Boot
防止出現 ACL、Id 缺失的錯誤
4. 編譯 zookeeper-server 並標記 target 中 generated-sources 中的 java 為 Generated Sources Boot
5. debug 方式運行 QuorumPeerMain 中的 main 方法,在執行 ZooKeeperServerMain 類中 的 runFromConfig 方法的 ServerMetrics.metricsProviderInitialized(metricsProvider); 一行時,報 Exception in thread "main" java.lang.NoClassDefFoundError: com/codahale/metrics/Reservoir
原因:(1)2 中 metrics-core 的 <scope>provided</scope> 未注釋掉
(2)缺少 metrics-graphite 依賴
解決方法:
參考 stackoverflow 上
Exception in thread "main" java.lang.NoClassDefFoundError: com/codahale/metrics/Reservoir
這個問題的回答
查看 zookeeper-server 的 pom.xml 中 metrics-core 的版本:
<dependency> <groupId>io.dropwizard.metrics</groupId> <artifactId>metrics-core</artifactId> <!-- <scope>provided</scope>--> </dependency>
版本為 :4.1.12.1
因此 引入 4.1.12.1 版本的 metrics-graphite 依賴:
<dependency> <groupId>io.dropwizard.metrics</groupId> <artifactId>metrics-graphite</artifactId> <version>4.1.12.1</version> </dependency>
再次 compile zookeeper-server 模塊
6. 編輯啟動配置,配置啟動參數即 配置文件位置
填入 zoo.cfg 位置:
即可運行成功