Apache ZooKeeper - 构建ZooKeeper源码环境及StandAlone模式下的服务端和客户端启动
文章目錄
- 源碼環境搭建
- clone source
- 源碼結構
- 啟動
- 查找啟動類
- 配置文件
- pom文件調整
- log4j.properties
- 以源碼的方式啟動Server
- 以源碼的方式啟動Client
- 驗證
- 遇到的問題
- 缺少Info接口
- 缺少org.apache.zookeeper.data.ACL
- Could not find artifact pl.project13.maven
源碼環境搭建
有一天大佬問我看過zk的源碼嗎…?
好的好的 誰說不看呢
說干就干
clone source
https://github.com/apache/zookeeper
當然了,我要看 3.5.8這個版本的
https://github.com/apache/zookeeper/tree/branch-3.5.8
Clone ---- HTTPS
IDEA里
源碼結構
啟動
查找啟動類
根據經驗,從啟動腳本中去找啟動類 ,八成啟動類中有個main方法
找啟動腳本的 start方法
找到 ZOOMAIN="org.apache.zookeeper.server.quorum.QuorumPeerMain"
ZOOCFG="$2" 可知是通過外部傳參進來的,那就按照約定的來吧
配置文件
從 zoo_sample.cfg copy一個出來
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=D:\IdeaProjects\zookeeperSource\data\zk # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1pom文件調整
將zookeeper-server項目里pom.xml文件里依賴的包的scope為provided這一行全部注釋掉 【jline除外】
log4j.properties
將conf文件夾里的log4j.properties文件復制一份到zookeeper-server項目的 \target\classes 目錄下,不然沒有日志
以源碼的方式啟動Server
2020-11-27 11:13:47,849 [myid:] - INFO [main:QuorumPeerConfig@135] - Reading configuration from: D:\IdeaProjects\zookeeperSource\conf\zoo.cfg 2020-11-27 11:13:47,858 [myid:] - WARN [main:VerifyingFileFactory@59] - D:IdeaProjectszookeeperSourcedata is relative. Prepend .\ to indicate that you're sure! 2020-11-27 11:13:47,870 [myid:] - INFO [main:QuorumPeerConfig@387] - clientPortAddress is 0.0.0.0:2181 2020-11-27 11:13:47,872 [myid:] - INFO [main:QuorumPeerConfig@391] - secureClientPort is not set 2020-11-27 11:13:47,876 [myid:] - INFO [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3 2020-11-27 11:13:47,876 [myid:] - INFO [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0 2020-11-27 11:13:47,876 [myid:] - INFO [main:DatadirCleanupManager@101] - Purge task is not scheduled. 2020-11-27 11:13:47,876 [myid:] - WARN [main:QuorumPeerMain@125] - Either no config or no quorum defined in config, running in standalone mode 2020-11-27 11:13:47,882 [myid:] - INFO [main:ManagedUtil@45] - Log4j 1.2 jmx support found and enabled. 2020-11-27 11:13:47,951 [myid:] - INFO [main:QuorumPeerConfig@135] - Reading configuration from: D:\IdeaProjects\zookeeperSource\conf\zoo.cfg 2020-11-27 11:13:47,951 [myid:] - WARN [main:VerifyingFileFactory@59] - D:IdeaProjectszookeeperSourcedata is relative. Prepend .\ to indicate that you're sure! 2020-11-27 11:13:47,952 [myid:] - INFO [main:QuorumPeerConfig@387] - clientPortAddress is 0.0.0.0:2181 2020-11-27 11:13:47,952 [myid:] - INFO [main:QuorumPeerConfig@391] - secureClientPort is not set 2020-11-27 11:13:47,952 [myid:] - INFO [main:ZooKeeperServerMain@117] - Starting server 2020-11-27 11:13:47,955 [myid:] - INFO [main:FileTxnSnapLog@115] - zookeeper.snapshot.trust.empty : false 2020-11-27 11:13:47,985 [myid:] - INFO [main:Environment@109] - Server environment:zookeeper.version=1.0.0-1, built on 2020-11-27 2020-11-27 11:13:47,987 [myid:] - INFO [main:Environment@109] - Server environment:host.name=LAPTOP-JF3RBRRJ 2020-11-27 11:13:47,987 [myid:] - INFO [main:Environment@109] - Server environment:java.version=1.8.0_261 2020-11-27 11:13:47,987 [myid:] - INFO [main:Environment@109] - Server environment:java.vendor=Oracle Corporation 2020-11-27 11:13:47,987 [myid:] - INFO [main:Environment@109] - Server environment:java.home=D:\Program Files (x86)\Java\jdk1.8.0_261\jre 2020-11-27 11:13:47,987 [myid:] - INFO [main:Environment@109] - Server environment:java.class.path=D:\Program Files ...... ...... ...... 2020-11-27 11:13:47,987 [myid:] - INFO [main:Environment@109] - Server environment:java.io.tmpdir=C:\Users\artisan\AppData\Local\Temp\ 2020-11-27 11:13:47,987 [myid:] - INFO [main:Environment@109] - Server environment:java.compiler=<NA> 2020-11-27 11:13:47,987 [myid:] - INFO [main:Environment@109] - Server environment:os.name=Windows 10 2020-11-27 11:13:47,988 [myid:] - INFO [main:Environment@109] - Server environment:os.arch=amd64 2020-11-27 11:13:47,988 [myid:] - INFO [main:Environment@109] - Server environment:os.version=10.0 2020-11-27 11:13:47,988 [myid:] - INFO [main:Environment@109] - Server environment:user.name=artisan 2020-11-27 11:13:47,988 [myid:] - INFO [main:Environment@109] - Server environment:user.home=C:\Users\artisan 2020-11-27 11:13:47,988 [myid:] - INFO [main:Environment@109] - Server environment:user.dir=D:\IdeaProjects\zookeeperSource 2020-11-27 11:13:47,988 [myid:] - INFO [main:Environment@109] - Server environment:os.memory.free=222MB 2020-11-27 11:13:47,988 [myid:] - INFO [main:Environment@109] - Server environment:os.memory.max=3499MB 2020-11-27 11:13:47,988 [myid:] - INFO [main:Environment@109] - Server environment:os.memory.total=236MB 2020-11-27 11:13:47,990 [myid:] - INFO [main:ZooKeeperServer@938] - minSessionTimeout set to 4000 2020-11-27 11:13:47,990 [myid:] - INFO [main:ZooKeeperServer@947] - maxSessionTimeout set to 40000 2020-11-27 11:13:47,991 [myid:] - INFO [main:ZooKeeperServer@166] - Created server with tickTime 2000 minSessionTimeout 4000 maxSessionTimeout 40000 datadir D:IdeaProjectszookeeperSourcedata\version-2 snapdir D:IdeaProjectszookeeperSourcedata\version-2 2020-11-27 11:13:48,014 [myid:] - INFO [main:Log@169] - Logging initialized @433ms to org.eclipse.jetty.util.log.Slf4jLog 2020-11-27 11:13:48,167 [myid:] - WARN [main:ContextHandler@1520] - o.e.j.s.ServletContextHandler@215be6bb{/,null,UNAVAILABLE} contextPath ends with /* 2020-11-27 11:13:48,167 [myid:] - WARN [main:ContextHandler@1531] - Empty contextPath 2020-11-27 11:13:48,182 [myid:] - INFO [main:Server@359] - jetty-9.4.24.v20191120; built: 2019-11-20T21:37:49.771Z; git: 363d5f2df3a8a28de40604320230664b9c793c16; jvm 1.8.0_261-b12 2020-11-27 11:13:48,223 [myid:] - INFO [main:DefaultSessionIdManager@333] - DefaultSessionIdManager workerName=node0 2020-11-27 11:13:48,223 [myid:] - INFO [main:DefaultSessionIdManager@338] - No SessionScavenger set, using defaults 2020-11-27 11:13:48,226 [myid:] - INFO [main:HouseKeeper@140] - node0 Scavenging every 660000ms 2020-11-27 11:13:48,235 [myid:] - INFO [main:ContextHandler@825] - Started o.e.j.s.ServletContextHandler@215be6bb{/,null,AVAILABLE} 2020-11-27 11:13:48,462 [myid:] - INFO [main:AbstractConnector@330] - Started ServerConnector@17f6480{HTTP/1.1,[http/1.1]}{0.0.0.0:8080} 2020-11-27 11:13:48,463 [myid:] - INFO [main:Server@399] - Started @882ms 2020-11-27 11:13:48,463 [myid:] - INFO [main:JettyAdminServer@112] - Started AdminServer on address 0.0.0.0, port 8080 and command URL /commands 2020-11-27 11:13:48,468 [myid:] - INFO [main:ServerCnxnFactory@135] - Using org.apache.zookeeper.server.NIOServerCnxnFactory as server connection factory 2020-11-27 11:13:48,475 [myid:] - INFO [main:NIOServerCnxnFactory@673] - Configuring NIO connection handler with 10s sessionless connection timeout, 2 selector thread(s), 24 worker threads, and 64 kB direct buffers. 2020-11-27 11:13:48,481 [myid:] - INFO [main:NIOServerCnxnFactory@686] - binding to port 0.0.0.0/0.0.0.0:2181 2020-11-27 11:13:48,498 [myid:] - INFO [main:ZKDatabase@117] - zookeeper.snapshotSizeFactor = 0.33 2020-11-27 11:13:48,511 [myid:] - INFO [main:FileSnap@83] - Reading snapshot D:IdeaProjectszookeeperSourcedata\version-2\snapshot.0 2020-11-27 11:13:48,527 [myid:] - INFO [main:FileTxnSnapLog@404] - Snapshotting: 0x4 to D:IdeaProjectszookeeperSourcedata\version-2\snapshot.4 2020-11-27 11:13:48,547 [myid:] - INFO [main:ContainerManager@64] - Using checkIntervalMs=60000 maxPerMinute=10000看重點
2020-11-27 11:13:48,463 [myid:] - INFO [main:Server@399] - Started @882ms
2020-11-27 11:13:48,481 [myid:] - INFO [main:NIOServerCnxnFactory@686] - binding to port 0.0.0.0/0.0.0.0:2181
以源碼的方式啟動Client
啟動日志
驗證
在客戶端執行ZK命令
好了基本完事兒
遇到的問題
缺少Info接口
Info是用來發布的時候生成版本用的,正規的話,需要用 org.apache.zookeeper.version.util.VerGen生成Info , 我們這里隨便搞個就行了
package org.apache.zookeeper.version;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2020/11/27 9:54* @mark: show me the code , change the world*/ public interface Info {int MAJOR = 1;int MINOR = 0;int MICRO = 0 ;String QUALIFIER = null;int REVISION = -1;String REVISION_HASH = "1";String BUILD_DATE = "2020-11-27";}缺少org.apache.zookeeper.data.ACL
重新編譯 Jute序列化模塊
Could not find artifact pl.project13.maven
Could not find artifact pl.project13.maven:git-commit-id-plugin:jar: in aliyunmaven (https://maven.aliyun.com/repository/central)忽略即可
總結
以上是生活随笔為你收集整理的Apache ZooKeeper - 构建ZooKeeper源码环境及StandAlone模式下的服务端和客户端启动的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Apache ZooKeeper - 使
- 下一篇: Apache ZooKeeper - 使