canal 源码解析(1)-启动篇(2)

本文详细解析了Canal的启动过程,从start方法开始,包括初始化CanalInstances,启动监控类ServerRunningMonitor,以及配置文件的加载和Bean的初始化。在Spring模式下,配置文件如canal.properties和example/instance.properties被用来替换表达式,并初始化CanalInstance。最后,通过调用父类方法启动多个线程执行不同模块的任务。
摘要由CSDN通过智能技术生成

1)先看一下整个start方法构成

 

public void start() throws Throwable {
    logger.info("## start the canal server[{}:{}]", ip, port);
    // 创建整个canal的工作节点
    final String path = ZookeeperPathUtils.getCanalClusterNode(ip + ":" + port);
    initCid(path);
    if (zkclientx != null) {
        this.zkclientx.subscribeStateChanges(new IZkStateListener() {

            public void handleStateChanged(KeeperState state) throws Exception {

            }

            public void handleNewSession() throws Exception {
                initCid(path);
            }

            @Override
            public void handleSessionEstablishmentError(Throwable error) throws Exception {
                logger.error("failed to connect to zookeeper", error);
            }
        });
    }
    // 优先启动embeded服务
    embededCanalServer.start();
    // 尝试启动一下非lazy状态的通道
    for (Map.Entry<String, InstanceConfig> entry : instanceConfigs.entrySet()) {
        final String destination = entry.getKey();
        InstanceConfig config = entry.getValue();
        // 创建destination的工作节点
        if (!embededCanalServer.isStart(destination)) {
            // HA机制启动
            ServerRunningMonitor runningMonitor = ServerRunningMonitors.getRunningMonitor(destination);
            if (!config.getLazy() && !runningMonitor.isStart()) {
                runningMonitor.start();
            }
        }

        if (autoScan) {
            instanceConfigMonitors.get(config.getMode()).register(destination, defaultAction);
        }
    }

    if (autoScan) {
        instanceConfigMonitors.get(globalInstanceConfig.getMode()).start();
        for (InstanceConfigMonitor monitor : instanceConfigMonitors.values()) {
            if (!monitor.isStart()) {
                monitor.start();
            }
        }
    }

    // 启动网络接口
    canalServer.start();
}

1.1)path =/otter/canal/cluster/172.20.0.20:11111

1.2)initcid(path) 因为本地没有zookeeper 所以该方法,可暂时不用解释

 

private void initCid(String path) {
    // logger.info("## init the canalId = {}", cid);
    // 初始化系统目录
    if (zkclientx != null) {
        try {
            zkclientx.createEphemeral(path);
        } catch (ZkNoNodeException e) {
            // 如果父目录不存在,则创建
            String parentDir = path.substring(0, path.lastIndexOf('/'));
           
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值