1. 通过zkServer.sh启动zookeeper时,正常指定的QuorumPeerMain作为启动主类。

2.QuorumPeerMain执行main方法,里面主要逻辑是执行initializeAndRun方法。

 

 public static void main(String[] args) {
        QuorumPeerMain main = new QuorumPeerMain();
        try {
            main.initializeAndRun(args);
        ....
    }


3.initializeAndRun方法

 

  (1)通过QuorumPeerConfig.parse方法,解析配置文件

  (2)通过DatadirCleanupManager.start方法,启动清除调度任务

  (3)runFromConfig或ZooKeeperServerMain.main来启动zookkeeper

 

protected void initializeAndRun(String[] args)
        throws ConfigException, IOException, AdminServerException
    {
        QuorumPeerConfig config = new QuorumPeerConfig();
        if (args.length == 1) {//如果指定的配置文件
            config.parse(args[0]);//解析配置文件
        }

        // Start and schedule the the purge task
        //启动安排清除任务
        DatadirCleanupManager purgeMgr = new DatadirCleanupManager(config
                .getDataDir(), config.getDataLogDir(), config
                .getSnapRetainCount(), config.getPurgeInterval());
        purgeMgr.start();

        if (args.length == 1 && config.isDistributed()) {
            runFromConfig(config);//启动zookeepeer集群
        } else {
            LOG.warn("Either no config or no quorum defined in config, running "
                    + " in standalone mode");
            // there is only server in the quorum -- run as standalone
            ZooKeeperServerMain.main(args);
        }
    }

 

最后欢迎大家访问我的个人网站:1024s

 

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐