集群模式下启动所有的
ZK
节点启动⼊⼝都是
QuorumPeerMain
类的
main
⽅法。
main
⽅法加载配置⽂件以后,最终会调⽤到
QuorumPeer
的
start
⽅法,来看下:
public void runFromConfig(QuorumPeerConfig config)
throws IOException, AdminServerException
{
try {
ManagedUtil.registerLog4jMBeans();
} catch (JMException e) {
LOG.warn("Unable to register log4j JMX control", e);
}
LOG.info("Starting quorum peer");
try {
ServerCnxnFactory cnxnFactory = null;
ServerCnxnFactory secureCnxnFactory = null;
if (config.getClientPortAddress() != null) {
//创建ServerCnxnFactory
cnxnFactory = ServerCnxnFactory.createFactory();
//初始化ServerCnxnFactory,配置客户端连接端口
cnxnFactory.configure(config.getClientPortAddress(),
config.getMaxClientCnxns(),
false);
}
if (config.getSecureClientPortAddress() != null) {
//配置安全连接端口
secureCnxnFactory = ServerCnxnFactory.createFactory();
secureCnxnFactory.configure(config.getSecureClientPortAddress(),
config.getMaxClientCnxns(),
true);
}
//--------------------初始化当前ZK服务节点的配置----------------
// 设置数据和快照操作 数据管理器
quorumPeer = getQuorumPeer();
//创建FileTxnSnapLog
quorumPeer.setTxnFactory(new FileTxnSnapLog(
config.getDataLogDir(),
config.getDataDir()));
quorumPeer.enableLocalSessions(config.areLocalSessionsEnabled());
quorumPeer.enableLocalSessionsUpgrading(
config.isLocalSessionsUpgradingEnabled());
//quorumPeer.setQuorumPeers(config.getAllMembers());
//选举类型
quorumPeer.setElectionType(config.getElectionAlg());
//server ID
quorumPeer.setMyid(config.getServerId());
quorumPeer.setTickTime(config.getTickTime());
quorumPeer.setMinSessionTimeout(config.getMinSessionTimeout());
quorumPeer.setMaxSessionTimeout(config.getMaxSessionTimeout());
quorumPeer.setInitLimit(config.getInitLimit());
quorumPeer.setSyncLimit(config.getSyncLimit());
quorumPeer.setConfigFileName(config.getConfigFilename());
// 设置ZK的节点数据库:管理zookeeper的所有会话记录以及DataTree和事务日志的存储