乐趣区

编译运行Zookeeper源码

GitHub 地址:

https://github.com/apache/zookeeper

最新版本的 zookeeper 已经使用了 maven 进行管理了、不再需要安装 Ant

下载完成之后、使用 Idea 作为一个 maven 项目打开

拷贝配置文件

  • 拷贝 zoo_sample.cfg 文件至相同文件夹下,名为:zoo.cfg,配置全部使用默认;
  • 创建 /tmp/zookeeper 目录、用于存放 zk 数据.
  • 拷贝 log4j.properties 文件至:zookeeper-server/src/main/resources,文件名还是 log4j.properties 不变.
  • resource 标记为 Resources Root

增加启动项

找到类 org.apache.zookeeper.server.quorum.QuorumPeerMain

在参数中加上 conf/zoo.cfg

修改 pom 文件中 scope

scope 注释掉

<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>

执行 server

执行 main 方法前建议执行命令 mvn clean install -DskipTests

2020-05-11 23:51:27,731 [myid:] - INFO  [main:NIOServerCnxnFactory@664] - binding to port 0.0.0.0/0.0.0.0:2181
2020-05-11 23:51:27,751 [myid:] - INFO  [main:WatchManagerFactory@42] - Using org.apache.zookeeper.server.watch.WatchManager as watchmanager
2020-05-11 23:51:27,751 [myid:] - INFO  [main:WatchManagerFactory@42] - Using org.apache.zookeeper.server.watch.WatchManager as watchmanager
2020-05-11 23:51:27,751 [myid:] - INFO  [main:ZKDatabase@132] - zookeeper.snapshotSizeFactor = 0.33
2020-05-11 23:51:27,751 [myid:] - INFO  [main:ZKDatabase@152] - zookeeper.commitLogCount=500
2020-05-11 23:51:27,757 [myid:] - INFO  [main:SnapStream@61] - zookeeper.snapshot.compression.method = CHECKED
2020-05-11 23:51:27,759 [myid:] - INFO  [main:FileSnap@85] - Reading snapshot /tmp/zookeeper/version-2/snapshot.3
2020-05-11 23:51:27,763 [myid:] - INFO  [main:DataTree@1730] - The digest in the snapshot has digest version of 2, , with zxid as 0x3, and digest value as 1371985504
2020-05-11 23:51:27,774 [myid:] - INFO  [main:FileTxnSnapLog@363] - 1 txns loaded in 5 ms
2020-05-11 23:51:27,774 [myid:] - INFO  [main:ZKDatabase@289] - Snapshot loaded in 23 ms, highest zxid is 0x4, digest is 1371985504
2020-05-11 23:51:27,774 [myid:] - INFO  [main:FileTxnSnapLog@470] - Snapshotting: 0x4 to /tmp/zookeeper/version-2/snapshot.4
2020-05-11 23:51:27,775 [myid:] - INFO  [main:ZooKeeperServer@522] - Snapshot taken in 1 ms
2020-05-11 23:51:27,788 [myid:] - INFO  [main:RequestThrottler@75] - zookeeper.request_throttler.shutdownTimeout = 10000
2020-05-11 23:51:27,802 [myid:] - INFO  [main:ContainerManager@83] - Using checkIntervalMs=60000 maxPerMinute=10000 maxNeverUsedIntervalMs=0
2020-05-11 23:51:27,803 [myid:] - INFO  [main:ZKAuditProvider@42] - ZooKeeper audit is disabled.
2020-05-11 23:51:58,403 [myid:] - INFO  [SessionTracker:ZooKeeperServer@603] - Expiring session 0x100016819530000, timeout of 30000ms exceeded
2020-05-11 23:51:58,404 [myid:] - INFO  [SessionTracker:ZooKeeperServer@603] - Expiring session 0x10001651fa70000, timeout of 30000ms exceeded
2020-05-11 23:51:58,407 [myid:] - INFO  [SyncThread:0:FileTxnLog@284] - Creating new log file: log.5

启动成功

执行 client

如 server 一般、配置入口类 org.apache.zookeeper.ZooKeeperMain

将下面的依赖的 scope 注释掉

<dependency>  
<groupId>commons-cli</groupId>  
<artifactId>commons-cli</artifactId>
</dependency>

启动

Welcome to ZooKeeper!JLine support is disabled
2020-05-11 23:54:50,732 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1169] - Opening socket connection to server localhost/127.0.0.1:2181.
2020-05-11 23:54:50,732 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1171] - SASL config status: Will not attempt to authenticate using SASL (unknown error)
2020-05-11 23:54:50,748 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1001] - Socket connection established, initiating session, client: /127.0.0.1:59060, server: localhost/127.0.0.1:2181
2020-05-11 23:54:50,769 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1436] - Session establishment complete on server localhost/127.0.0.1:2181, session id = 0x10001a019010000, negotiated timeout = 30000WATCHER::WatchedEvent state:SyncConnected type:None path:nullls /[zookeeper]ls /zookeeper[config, quota]

至此,已成功编译运行 zookeeper 的源码

退出移动版