Zookeeper服务器单机版启动

本文详细介绍了ZooKeeper服务器单机版的启动过程,包括预启动和初始化两个阶段。预启动涉及配置文件解析、历史文件清理器创建、模式判断等;初始化则涵盖了服务器统计器创建、数据管理器初始化、网络连接工厂设置等步骤,最终完成ZooKeeper服务的对外提供。
摘要由CSDN通过智能技术生成

先来看下ZooKeeper服务端的整体构架。

1.单机版服务器启动

        ZooKeeper服务器的启动,大体分为以下5个步骤:配置文件解析、初始化数据管理器、初始化网络I/O管理器、数据恢复与对外服务。下图为单机版的服务器启动流程。

1.1 预启动

预启动步骤如下。

(1)统一由QuorumPeerMain作为启动类。

         无论是单机版还是集群版,在启动ZooKeeper服务器时,在zkServer.cmd和zkServer.sh两个脚本中,都配置了使用org.apach.zookeeper.server.quorum.QuorumPeerMain作为启动类入口。

public class QuorumPeerMain {
    private static final Logger LOG = LoggerFactory.getLogger(QuorumPeerMain.class);
 
    private static final String USAGE = "Usage: QuorumPeerMain configfile";
 
    protected QuorumPeer quorumPeer;
 
    /**
     * To start the replicated server specify the configuration file name on
     * the command line.
     * @param args path to the configfile
     */
    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 (Exception e) {
            LOG.error("Unexpected exception, exiting abnormally", e);
            System.exit(1);
        }
        LOG.info("Exiting normally");
        System.exit(0);
    }
 
    protected void initializeAndRun(String[] args)
        throws ConfigException, IOException
    {
        QuorumPeerConfig config = new QuorumPeerConfig();
        if (args.length == 1) {
            config.parse(args[0]);  //解析配置文件zoo.cfg
        }
 
        // 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.servers.size() > 0) {   //集群模式
            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
            ZooKeeperServerMa
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值