- 首先产徊,從github上面克隆zookeeper的源代碼律想。
地址: https://github.com/apache/zookeeper.git
可以看到zookeeper服務(wù)端的主要代碼結(jié)構(gòu)焊唬,其中最主要的就是zookeeper-server和zookeeper-client模塊蒋纬。
zookeeper-server是zookeeper的服務(wù)端實(shí)現(xiàn)源梭。
zookeeper-client是官網(wǎng)提供的zookeeper客戶端铺呵。
zookeeper-jute是zookeeper的序列化與反序列化吉挣。
zookeeper-recipes是zookeeper的菜譜派撕,也就是常見的一些應(yīng)用場(chǎng)景。
- 導(dǎo)入項(xiàng)目之后睬魂,需要為序列化與反序列化生成一些自動(dòng)化的代碼和版本信息终吼。
點(diǎn)擊右側(cè)Maven工具欄紅色圈出的按鈕。
-
需要把zookeeper-server模塊的resources目錄變成只資源模塊氯哮,并且把conf目錄下的log4j.properties文件拷貝過去际跪。
- 設(shè)置啟動(dòng)類和啟動(dòng)參數(shù)。啟動(dòng)類喉钢,org.apache.zookeeper.server.quorum.QuorumPeerMain
- 有一些jar因?yàn)榘姹镜膯栴}姆打,啟動(dòng)的時(shí)候會(huì)出現(xiàn)ClassNotFoundError,需要修改一下pom文件肠虽。在zookeeper-server模塊下的pom文件最下面增加以下依賴幔戏。
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.xerial.snappy</groupId>
<artifactId>snappy-java</artifactId>
<version>1.1.7.3</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
</dependency>
</dependencies>
- 重新clean一下項(xiàng)目,并且執(zhí)行第2步的自動(dòng)生成代碼税课,然后運(yùn)行項(xiàng)目, 可以看到控制臺(tái)有輸出日志闲延。
2021-05-04 11:19:16,352 [myid:] - INFO [main:QuorumPeerConfig@181] - Reading configuration from: conf/zoo.cfg
2021-05-04 11:19:16,358 [myid:] - WARN [main:VerifyingFileFactory@65] - conf/zoo.cfg is relative. Prepend ./ to indicate that you're sure!
2021-05-04 11:19:16,386 [myid:] - INFO [main:QuorumPeerConfig@444] - clientPortAddress is 0.0.0.0:2181
2021-05-04 11:19:16,388 [myid:] - INFO [main:QuorumPeerConfig@448] - secureClientPort is not set
2021-05-04 11:19:16,388 [myid:] - INFO [main:QuorumPeerConfig@464] - observerMasterPort is not set
2021-05-04 11:19:16,388 [myid:] - INFO [main:QuorumPeerConfig@481] - metricsProvider.className is org.apache.zookeeper.metrics.impl.DefaultMetricsProvider
2021-05-04 11:19:16,456 [myid:1] - INFO [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3
2021-05-04 11:19:16,456 [myid:1] - INFO [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0
2021-05-04 11:19:16,456 [myid:1] - INFO [main:DatadirCleanupManager@101] - Purge task is not scheduled.
2021-05-04 11:19:16,463 [myid:1] - INFO [main:ManagedUtil@44] - Log4j 1.2 jmx support found and enabled.
2021-05-04 11:19:16,548 [myid:1] - INFO [main:QuorumPeerMain@155] - Starting quorum peer, myid=1
2021-05-04 11:19:16,571 [myid:1] - INFO [main:ServerMetrics@62] - ServerMetrics initialized with provider org.apache.zookeeper.metrics.impl.DefaultMetricsProvider@58134517
2021-05-04 11:19:16,588 [myid:1] - INFO [main:DigestAuthenticationProvider@47] - ACL digest algorithm is: SHA1
2021-05-04 11:19:16,588 [myid:1] - INFO [main:DigestAuthenticationProvider@61] - zookeeper.DigestAuthenticationProvider.enabled = true
2021-05-04 11:19:16,596 [myid:1] - INFO [main:ServerCnxnFactory@169] - Using org.apache.zookeeper.server.NIOServerCnxnFactory as server connection factory
2021-05-04 11:19:16,599 [myid:1] - WARN [main:ServerCnxnFactory@309] - maxCnxns is not configured, using default value 0.
2021-05-04 11:19:16,603 [myid:1] - INFO [main:NIOServerCnxnFactory@652] - Configuring NIO connection handler with 10s sessionless connection timeout, 2 selector thread(s), 16 worker threads, and 64 kB direct buffers.
2021-05-04 11:19:16,621 [myid:1] - INFO [main:NIOServerCnxnFactory@660] - binding to port 0.0.0.0/0.0.0.0:2181
2021-05-04 11:19:16,645 [myid:1] - INFO [main:QuorumPeer@808] - zookeeper.quorumCnxnTimeoutMs=-1
2021-05-04 11:19:16,684 [myid:1] - INFO [main:Log@169] - Logging initialized @683ms to org.eclipse.jetty.util.log.Slf4jLog
2021-05-04 11:19:16,857 [myid:1] - WARN [main:ContextHandler@1660] - o.e.j.s.ServletContextHandler@55b53d44{/,null,STOPPED} contextPath ends with /*
2021-05-04 11:19:16,857 [myid:1] - WARN [main:ContextHandler@1671] - Empty contextPath
2021-05-04 11:19:16,895 [myid:1] - INFO [main:X509Util@77] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2021-05-04 11:19:16,899 [myid:1] - INFO [main:FileTxnSnapLog@126] - zookeeper.snapshot.trust.empty : false
2021-05-04 11:19:16,904 [myid:1] - INFO [main:QuorumPeer@1787] - Local sessions disabled
2021-05-04 11:19:16,904 [myid:1] - INFO [main:QuorumPeer@1798] - Local session upgrading disabled
2021-05-04 11:19:16,905 [myid:1] - INFO [main:QuorumPeer@1765] - tickTime set to 2000
2021-05-04 11:19:16,905 [myid:1] - INFO [main:QuorumPeer@1809] - minSessionTimeout set to 4000
2021-05-04 11:19:16,905 [myid:1] - INFO [main:QuorumPeer@1820] - maxSessionTimeout set to 40000
2021-05-04 11:19:16,905 [myid:1] - INFO [main:QuorumPeer@1845] - initLimit set to 10
2021-05-04 11:19:16,905 [myid:1] - INFO [main:QuorumPeer@2032] - syncLimit set to 5
2021-05-04 11:19:16,905 [myid:1] - INFO [main:QuorumPeer@2047] - connectToLearnerMasterLimit set to 0
2021-05-04 11:19:16,943 [myid:1] - INFO [main:ZookeeperBanner@42] -
2021-05-04 11:19:16,943 [myid:1] - INFO [main:ZookeeperBanner@42] - ______ _
2021-05-04 11:19:16,943 [myid:1] - INFO [main:ZookeeperBanner@42] - |___ / | |
2021-05-04 11:19:16,943 [myid:1] - INFO [main:ZookeeperBanner@42] - / / ___ ___ | | __ ___ ___ _ __ ___ _ __
2021-05-04 11:19:16,943 [myid:1] - INFO [main:ZookeeperBanner@42] - / / / _ \ / _ \ | |/ / / _ \ / _ \ | '_ \ / _ \ | '__|
2021-05-04 11:19:16,943 [myid:1] - INFO [main:ZookeeperBanner@42] - / /__ | (_) | | (_) | | < | __/ | __/ | |_) | | __/ | |
2021-05-04 11:19:16,943 [myid:1] - INFO [main:ZookeeperBanner@42] - /_____| \___/ \___/ |_|\_\ \___| \___| | .__/ \___| |_|
2021-05-04 11:19:16,943 [myid:1] - INFO [main:ZookeeperBanner@42] - | |
2021-05-04 11:19:16,943 [myid:1] - INFO [main:ZookeeperBanner@42] - |_|
2021-05-04 11:19:16,943 [myid:1] - INFO [main:ZookeeperBanner@42] -
2021-05-04 11:19:16,953 [myid:1] - INFO [main:Environment@98] - Server environment:zookeeper.version=3.8.0-SNAPSHOT-f8ebf1a25281b2c7f48e08011918c72643313209-dirty, built on 2021-04-26 09:07 UTC
2021-05-04 11:19:16,954 [myid:1] - INFO [main:Environment@98] - Server environment:host.name=localhost
2021-05-04 11:19:16,954 [myid:1] - INFO [main:Environment@98] - Server environment:java.version=1.8.0_221
2021-05-04 11:19:16,954 [myid:1] - INFO [main:Environment@98] - Server environment:java.vendor=Oracle Corporation
2021-05-04 11:19:16,954 [myid:1] - INFO [main:Environment@98] - Server environment:java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre
2021-05-04 11:19:16,954 [myid:1] - INFO [main:Environment@98] - Server environment:java.class.path=/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/lib/tools.jar:/Users/zengzehao/IdeaProjects/zookeeper/zookeeper-server/target/classes:/Users/zengzehao/IdeaProjects/zookeeper/zookeeper-jute/target/classes:/Users/zengzehao/.m2/repository/org/apache/yetus/audience-annotations/0.12.0/audience-annotations-0.12.0.jar:/Users/zengzehao/.m2/repository/io/netty/netty-handler/4.1.63.Final/netty-handler-4.1.63.Final.jar:/Users/zengzehao/.m2/repository/io/netty/netty-common/4.1.63.Final/netty-common-4.1.63.Final.jar:/Users/zengzehao/.m2/repository/io/netty/netty-resolver/4.1.63.Final/netty-resolver-4.1.63.Final.jar:/Users/zengzehao/.m2/repository/io/netty/netty-buffer/4.1.63.Final/netty-buffer-4.1.63.Final.jar:/Users/zengzehao/.m2/repository/io/netty/netty-transport/4.1.63.Final/netty-transport-4.1.63.Final.jar:/Users/zengzehao/.m2/repository/io/netty/netty-codec/4.1.63.Final/netty-codec-4.1.63.Final.jar:/Users/zengzehao/.m2/repository/io/netty/netty-transport-native-epoll/4.1.63.Final/netty-transport-native-epoll-4.1.63.Final.jar:/Users/zengzehao/.m2/repository/io/netty/netty-transport-native-unix-common/4.1.63.Final/netty-transport-native-unix-common-4.1.63.Final.jar:/Users/zengzehao/.m2/repository/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar:/Users/zengzehao/.m2/repository/org/slf4j/slf4j-log4j12/1.7.30/slf4j-log4j12-1.7.30.jar:/Users/zengzehao/.m2/repository/org/eclipse/jetty/jetty-server/9.4.39.v20210325/jetty-server-9.4.39.v20210325.jar:/Users/zengzehao/.m2/repository/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar:/Users/zengzehao/.m2/repository/org/eclipse/jetty/jetty-http/9.4.39.v20210325/jetty-http-9.4.39.v20210325.jar:/Users/zengzehao/.m2/repository/org/eclipse/jetty/jetty-util/9.4.39.v20210325/jetty-util-9.4.39.v20210325.jar:/Users/zengzehao/.m2/repository/org/eclipse/jetty/jetty-io/9.4.39.v20210325/jetty-io-9.4.39.v20210325.jar:/Users/zengzehao/.m2/repository/org/eclipse/jetty/jetty-servlet/9.4.39.v20210325/jetty-servlet-9.4.39.v20210325.jar:/Users/zengzehao/.m2/repository/org/eclipse/jetty/jetty-security/9.4.39.v20210325/jetty-security-9.4.39.v20210325.jar:/Users/zengzehao/.m2/repository/org/eclipse/jetty/jetty-util-ajax/9.4.39.v20210325/jetty-util-ajax-9.4.39.v20210325.jar:/Users/zengzehao/.m2/repository/io/dropwizard/metrics/metrics-core/3.1.0/metrics-core-3.1.0.jar:/Users/zengzehao/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar:/Users/zengzehao/.m2/repository/org/xerial/snappy/snappy-java/1.1.7.3/snappy-java-1.1.7.3.jar:/Users/zengzehao/.m2/repository/commons-io/commons-io/2.6/commons-io-2.6.jar:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar:/Users/zengzehao/Library/Caches/IntelliJIdea2019.1/captureAgent/debugger-agent.jar
2021-05-04 11:19:16,954 [myid:1] - INFO [main:Environment@98] - Server environment:java.library.path=/Users/zengzehao/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
2021-05-04 11:19:16,954 [myid:1] - INFO [main:Environment@98] - Server environment:java.io.tmpdir=/var/folders/xt/p72g3jsd0zncc1yv8vnzs3l00000gn/T/
2021-05-04 11:19:16,954 [myid:1] - INFO [main:Environment@98] - Server environment:java.compiler=<NA>
2021-05-04 11:19:16,954 [myid:1] - INFO [main:Environment@98] - Server environment:os.name=Mac OS X
2021-05-04 11:19:16,955 [myid:1] - INFO [main:Environment@98] - Server environment:os.arch=x86_64
2021-05-04 11:19:16,955 [myid:1] - INFO [main:Environment@98] - Server environment:os.version=10.15.6
2021-05-04 11:19:16,955 [myid:1] - INFO [main:Environment@98] - Server environment:user.name=zengzehao
2021-05-04 11:19:16,955 [myid:1] - INFO [main:Environment@98] - Server environment:user.home=/Users/zengzehao
2021-05-04 11:19:16,955 [myid:1] - INFO [main:Environment@98] - Server environment:user.dir=/Users/zengzehao/IdeaProjects/zookeeper
2021-05-04 11:19:16,955 [myid:1] - INFO [main:Environment@98] - Server environment:os.memory.free=110MB
2021-05-04 11:19:16,955 [myid:1] - INFO [main:Environment@98] - Server environment:os.memory.max=1820MB
2021-05-04 11:19:16,955 [myid:1] - INFO [main:Environment@98] - Server environment:os.memory.total=123MB
2021-05-04 11:19:16,955 [myid:1] - INFO [main:ZooKeeperServer@138] - zookeeper.enableEagerACLCheck = false
2021-05-04 11:19:16,955 [myid:1] - INFO [main:ZooKeeperServer@151] - zookeeper.digest.enabled = true
2021-05-04 11:19:16,956 [myid:1] - INFO [main:ZooKeeperServer@155] - zookeeper.closeSessionTxn.enabled = true
2021-05-04 11:19:16,956 [myid:1] - INFO [main:ZooKeeperServer@1504] - zookeeper.flushDelay = 0 ms
2021-05-04 11:19:16,956 [myid:1] - INFO [main:ZooKeeperServer@1513] - zookeeper.maxWriteQueuePollTime = 0 ms
2021-05-04 11:19:16,956 [myid:1] - INFO [main:ZooKeeperServer@1522] - zookeeper.maxBatchSize=1000
2021-05-04 11:19:16,956 [myid:1] - INFO [main:ZooKeeperServer@265] - zookeeper.intBufferStartingSizeBytes = 1024
2021-05-04 11:19:16,966 [myid:1] - INFO [main:WatchManagerFactory@42] - Using org.apache.zookeeper.server.watch.WatchManager as watch manager
2021-05-04 11:19:16,966 [myid:1] - INFO [main:WatchManagerFactory@42] - Using org.apache.zookeeper.server.watch.WatchManager as watch manager
2021-05-04 11:19:16,967 [myid:1] - INFO [main:ZKDatabase@138] - zookeeper.snapshotSizeFactor = 0.33
2021-05-04 11:19:16,967 [myid:1] - INFO [main:ZKDatabase@158] - zookeeper.commitLogCount=500
2021-05-04 11:19:16,985 [myid:1] - INFO [main:QuorumPeer@2111] - Using insecure (non-TLS) quorum communication
2021-05-04 11:19:16,985 [myid:1] - INFO [main:QuorumPeer@2117] - Port unification disabled
2021-05-04 11:19:16,985 [myid:1] - INFO [main:QuorumPeer@183] - multiAddress.enabled set to false
2021-05-04 11:19:16,985 [myid:1] - INFO [main:QuorumPeer@208] - multiAddress.reachabilityCheckEnabled set to true
2021-05-04 11:19:16,985 [myid:1] - INFO [main:QuorumPeer@195] - multiAddress.reachabilityCheckTimeoutMs set to 1000
2021-05-04 11:19:16,986 [myid:1] - INFO [main:QuorumPeer@2572] - QuorumPeer communication is not secured! (SASL auth disabled)
2021-05-04 11:19:16,986 [myid:1] - INFO [main:QuorumPeer@2597] - quorum.cnxn.threads.size set to 20
2021-05-04 11:19:16,992 [myid:1] - INFO [main:SnapStream@61] - zookeeper.snapshot.compression.method = CHECKED
2021-05-04 11:19:16,997 [myid:1] - INFO [main:FileSnap@88] - Reading snapshot /tmp/zookeeper/version-2/snapshot.900000000
2021-05-04 11:19:17,003 [myid:1] - INFO [main:DataTree@1742] - The digest value is empty in snapshot
2021-05-04 11:19:17,008 [myid:1] - INFO [main:ZKDatabase@295] - Snapshot loaded in 23 ms, highest zxid is 0x900000000, digest is 1371985504
2021-05-04 11:19:17,020 [myid:1] - INFO [main:Server@375] - jetty-9.4.39.v20210325; built: 2021-03-25T14:42:11.471Z; git: 9fc7ca5a922f2a37b84ec9dbc26a5168cee7e667; jvm 1.8.0_221-b11
2021-05-04 11:19:17,075 [myid:1] - INFO [main:DefaultSessionIdManager@334] - DefaultSessionIdManager workerName=node0
2021-05-04 11:19:17,075 [myid:1] - INFO [main:DefaultSessionIdManager@339] - No SessionScavenger set, using defaults
2021-05-04 11:19:17,079 [myid:1] - INFO [main:HouseKeeper@132] - node0 Scavenging every 600000ms
2021-05-04 11:19:17,114 [myid:1] - WARN [main:ConstraintSecurityHandler@759] - ServletContext@o.e.j.s.ServletContextHandler@55b53d44{/,null,STARTING} has uncovered http methods for path: /*
2021-05-04 11:19:17,134 [myid:1] - INFO [main:ContextHandler@916] - Started o.e.j.s.ServletContextHandler@55b53d44{/,null,AVAILABLE}
2021-05-04 11:19:17,180 [myid:1] - INFO [main:AbstractConnector@331] - Started ServerConnector@59309333{HTTP/1.1, (http/1.1)}{0.0.0.0:9001}
2021-05-04 11:19:17,180 [myid:1] - INFO [main:Server@415] - Started @1179ms
2021-05-04 11:19:17,180 [myid:1] - INFO [main:JettyAdminServer@190] - Started AdminServer on address 0.0.0.0, port 9001 and command URL /commands
如果希望是以集群的方式運(yùn)行源代碼痊剖,跟之前搭建集群一樣,復(fù)制多份zoo.cfg文件垒玲,新建data目錄和myid文件陆馁,然后用不同的啟動(dòng)參數(shù)指定配置文件就可以了。