zookeeper(一)服务端启动

zookeeper服务端启动

流程

在这里插入图片描述

源码

// QuorumPeerMain
public static void main(String[] args) {
   
       QuorumPeerMain main = new QuorumPeerMain();
       try {
   
           main.initializeAndRun(args);
       } catch (IllegalArgumentException e) {
   
           LOG.error("Invalid arguments, exiting abnormally", e);
           LOG.info(USAGE);
           System.err.println(USAGE);
           System.exit(2);
       } catch (ConfigException e) {
   
           LOG.error("Invalid config, exiting abnormally", e);
           System.err.println("Invalid config, exiting abnormally");
           System.exit(2);
       } catch (DatadirException e) {
   
           LOG.error("Unable to access datadir, exiting abnormally", e);
           System.err.println("Unable to access datadir, exiting abnormally");
           System.exit(3);
       } catch (AdminServerException e) {
   
           LOG.error("Unable to start AdminServer, exiting abnormally", e);
           System.err.println("Unable to start AdminServer, exiting abnormally");
           System.exit(4);
       } catch (Exception e) {
   
           LOG.error("Unexpected exception, exiting abnormally", e);
           System.exit(1);
       }
       LOG.info("Exiting normally");
       System.exit(0);
   }
// QuorumPeerMain
protected void initializeAndRun(String[] args)
        throws ConfigException, IOException, AdminServerException
{
   
	// 1. args[0]代表配置文件的路径,读取配置文件,加载到QuorumPeerConfig中
    QuorumPeerConfig config = new QuorumPeerConfig();
    if (args.length == 1) {
   
        config.parse(args[0]);
    }

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

	// 3. 判断是否是集群模式
    if (args.length == 1 && config.isDistributed()) {
   
    	// 集群模式运行
        runFromConfig(config);
    } 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
        // 当前只有一个节点,切换到standalone模式
        // 单机模式运行
        ZooKeeperServerMain.main(args);
    }
}

解析配置文件

// QuorumPeerConfig
public void parse(String path) throws ConfigException {
   
   LOG.info("Reading configuration from: " + path);
  
   try {
   
   	   // 根据路径,读取配置文件
       File configFile = (new VerifyingFileFactory.Builder(LOG)
           .warnForRelativePath()
           .failForNonExistingPath()
           .build()).create(path);
       
       // 将配置文件中的每一行解析成键值对,放到properties中
       Properties cfg = new Properties();
       FileInputStream in = new FileInputStream(configFile);
       try {
   
           cfg.load(in);
           configFileStr = path;
       } finally {
   
           in.close();
       }
       
       // 获取Properties中的键值对,设置到QuorumPeerConfig对应的属性上
       // 后续QuorumPeer的启动会使用到这些属性
       parseProperties(cfg);
   } catch (IOException e) {
   
       throw new ConfigException("Error processing " 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值