文章目录

  • 源码环境搭建
    • 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=1


pom文件调整

将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


启动日志

Connecting to 127.0.0.1:2181
2020-11-27 11:05:41,288 [myid:] - INFO  [main:Environment@109] - Client environment:zookeeper.version=1.0.0-1, built on 2020-11-27
2020-11-27 11:05:41,291 [myid:] - INFO  [main:Environment@109] - Client environment:host.name=LAPTOP-JF3RBRRJ
2020-11-27 11:05:41,291 [myid:] - INFO  [main:Environment@109] - Client environment:java.version=1.8.0_261
2020-11-27 11:05:41,292 [myid:] - INFO  [main:Environment@109] - Client environment:java.vendor=Oracle Corporation
2020-11-27 11:05:41,292 [myid:] - INFO  [main:Environment@109] - Client environment:java.home=D:\Program Files (x86)\Java\jdk1.8.0_261\jre
2020-11-27 11:05:41,292 [myid:] - INFO  [main:Environment@109] - Client environment:java.class.path=D:\Program Files
..........
..........
..........
2020-11-27 11:05:41,293 [myid:] - INFO  [main:Environment@109] - Client environment:java.io.tmpdir=C:\Users\artisan\AppData\Local\Temp\
2020-11-27 11:05:41,293 [myid:] - INFO  [main:Environment@109] - Client environment:java.compiler=<NA>
2020-11-27 11:05:41,293 [myid:] - INFO  [main:Environment@109] - Client environment:os.name=Windows 10
2020-11-27 11:05:41,293 [myid:] - INFO  [main:Environment@109] - Client environment:os.arch=amd64
2020-11-27 11:05:41,293 [myid:] - INFO  [main:Environment@109] - Client environment:os.version=10.0
2020-11-27 11:05:41,293 [myid:] - INFO  [main:Environment@109] - Client environment:user.name=artisan
2020-11-27 11:05:41,293 [myid:] - INFO  [main:Environment@109] - Client environment:user.home=C:\Users\artisan
2020-11-27 11:05:41,293 [myid:] - INFO  [main:Environment@109] - Client environment:user.dir=D:\IdeaProjects\zookeeperSource
2020-11-27 11:05:41,293 [myid:] - INFO  [main:Environment@109] - Client environment:os.memory.free=224MB
2020-11-27 11:05:41,294 [myid:] - INFO  [main:Environment@109] - Client environment:os.memory.max=3499MB
2020-11-27 11:05:41,294 [myid:] - INFO  [main:Environment@109] - Client environment:os.memory.total=236MB
2020-11-27 11:05:41,296 [myid:] - INFO  [main:ZooKeeper@868] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@22d8cfe0
2020-11-27 11:05:41,301 [myid:] - INFO  [main:X509Util@79] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2020-11-27 11:05:41,521 [myid:] - INFO  [main:ClientCnxnSocket@237] - jute.maxbuffer value is 4194304 Bytes
2020-11-27 11:05:41,527 [myid:] - INFO  [main:ClientCnxn@1653] - zookeeper.request.timeout value is 0. feature enabled=
Welcome to ZooKeeper!
JLine support is disabled
2020-11-27 11:05:41,539 [myid:127.0.0.1:2181] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1112] - Opening socket connection to server kubernetes.docker.internal/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2020-11-27 11:05:41,540 [myid:127.0.0.1:2181] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@959] - Socket connection established, initiating session, client: /127.0.0.1:50568, server: kubernetes.docker.internal/127.0.0.1:2181
2020-11-27 11:05:41,562 [myid:127.0.0.1:2181] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1394] - Session establishment complete on server kubernetes.docker.internal/127.0.0.1:2181, sessionid = 0x1004701bbe50000, negotiated timeout = 30000WATCHER::WatchedEvent state:SyncConnected type:None path:null

验证

在客户端执行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模式下的服务端和客户端启动相关推荐

  1. windows环境下MySQL服务端和客户端安装,超详细

    windows环境下MySQL服务端和客户端安装,超详细 MySQL简介 MySQL的安装步骤 服务端安装 客户端安装 MySQL简介 MySQL是关系型数据库,由n张互相关联的表组成.一般是c或c+ ...

  2. idea构建spring源码环境

    前言 最近抽空在摸索spring源码,就想在idea中把spring源码环境给跑起来,以便于自己进行断点调试和学习,但在自己构建的时候才发现使用idea构建spring源码的环境的确不是一件简单简单的 ...

  3. Spring 源码学习一: 使用Gradle 构建Spring 源码环境

    Gradle安装 下载Gradle: https://gradle.org/releases/ 选择安装的版本: 6.x 以上 选择版本后,点击下载. 配置环境变量: unzip gradle-6.8 ...

  4. OneFlow源码解析:Eager模式下的SBP Signature推导

    作者|郑建华 更新|赵露阳 OneFlow 的 Global Tensor 有两个必要属性: Placement:决定了 tensor 数据分布在哪些设备上. SBP:决定了 tensor 数据在这些 ...

  5. LINUX环境下VNC服务端与客户端配置

    公司开发原来都是用的Xmanager软件,但近期爆出该软件存在后门漏洞问题,安防部门要求停止使用老版本的Xmanager进行升级或改用开源VNC软件,因此在网上查看了一些配置说明,特整理如下: VNC ...

  6. Kafka源码环境搭建

    欢迎支持笔者新作:<深入理解Kafka:核心设计与实践原理>和<RabbitMQ实战指南>,同时欢迎关注笔者的微信公众号:朱小厮的博客. 欢迎跳转到本文的原文链接:https: ...

  7. ZooKeeper源码阅读心得分享+源码基本结构+源码环境搭建

    首发CSDN:徐同学呀,原创不易,转载请注明源链接.我是徐同学,用心输出高质量文章,希望对你有所帮助. 一.心得分享 如何阅读ZooKeeper源码?从哪里开始阅读?最近把ZooKeeper源码看了个 ...

  8. zookeeper watch java_Apache ZooKeeper Watcher 机制源码解释

    分布式系统从根本上来说就是不同节点上的进程并发执行,并且相互之间对进程的行为进行协调处理的过程.不同节点上的进程互相协调行为的过程叫做分布式同步.许多分布式系统需要一个进程作为任务的协调者,执行一些其 ...

  9. tomcat源码环境构建

    前言 tomcat作为Java项目运行的容器,基本上算是目前使用最多的容器了吧,因此对tomcat的容器的研究很有必要,不仅是学习容器本身的设计思想,更能通过源码的调试和阅读对于其深层的原理进行较好的 ...

最新文章

  1. 笔试题目“翻转字符串”的实现
  2. C# 删除文件错误 access denied
  3. BS-GX-017基于SSM实现的在线考试管理系统
  4. JavaScript编码规范[百度]
  5. challenging and foundational
  6. windows下使用git管理github项目
  7. golang mysql大量写入_Golang 实现分片读取http超大文件流和并发控制
  8. 损失函数(Loss function) 和 代价函数(Cost function)
  9. 把百度网站设为首页_网站百度推广效果好不好?怎样才能把网站推广到百度首页?...
  10. ElasticSearch里面关于日期的存储方式,解决差8个小时
  11. 创建组_在Allegro软件中的Groups组创建之后怎么进行打散呢?
  12. 为facebook添加html/iframe页面 Create A Facebook Landing Page (Static HTML / iFrame)
  13. (1)封装 (2)static关键字 (3)继承
  14. jQuery创建Dom元素
  15. python 倒计时功能怎么用print实现_在python中的print语句中实现60秒倒计时
  16. html5 video play方法,html5 – 如何在现代Chrome上访问video.play()API
  17. Error mounting /dev/sda8 at /media/wxp/
  18. TCP三次握手中SYN,ACK,seq ack的含义
  19. 速度来!《软技能:代码之外的生存指南 (第2版)》送书4本
  20. 时间戳转化为时间小样例

热门文章

  1. OnnxRuntime 性能调优
  2. python将列表横着输出来
  3. python自增_如何在python语言实现自增和自减功能并显示结果
  4. 数据中台建设常见的几大误区,如何规避,你需要知道这几点
  5. 基于MATLAB均值漂移图像分割技术
  6. 深度学习核心技术精讲100篇(四十九)-深度学习之关联规则
  7. 有时间窗车辆路径问题(VRPTW)解决方案合集,[CW节约算法,TS(硬约束版),TS(惩罚函数版),LNS四种方法对比(附MATLAB代码)]
  8. 竞品分析实战系列之如何对电商产品条目进行合理设计?
  9. 深度学习核心技术精讲100篇(二十一)-深入理解Dirichlet分布及过程
  10. MapReduce 模式、算法和用例