专栏首页码匠的流水账聊聊CanalInstanceGenerator

聊聊CanalInstanceGenerator

本文主要研究一下CanalInstanceGenerator

CanalInstanceGenerator

canal-1.1.4/instance/core/src/main/java/com/alibaba/otter/canal/instance/core/CanalInstanceGenerator.java

public interface CanalInstanceGenerator {

    /**
     * 通过 destination 产生特定的 {@link CanalInstance}
     *
     * @param destination
     * @return
     */
    CanalInstance generate(String destination);
}
  • CanalInstanceGenerator定义了generate方法用于创建指定destination的CanalInstance

PlainCanalInstanceGenerator

canal-1.1.4/instance/manager/src/main/java/com/alibaba/otter/canal/instance/manager/PlainCanalInstanceGenerator.java

public class PlainCanalInstanceGenerator implements CanalInstanceGenerator {

    private static final Logger    logger      = LoggerFactory.getLogger(SpringCanalInstanceGenerator.class);
    private String                 springXml;
    private PlainCanalConfigClient canalConfigClient;
    private String                 defaultName = "instance";
    private BeanFactory            beanFactory;
    private Properties             canalConfig;

    public PlainCanalInstanceGenerator(Properties canalConfig){
        this.canalConfig = canalConfig;
    }

    public CanalInstance generate(String destination) {
        synchronized (CanalInstanceGenerator.class) {
            try {
                PlainCanal canal = canalConfigClient.findInstance(destination, null);
                if (canal == null) {
                    throw new CanalException("instance : " + destination + " config is not found");
                }
                Properties properties = canal.getProperties();
                // merge local
                properties.putAll(canalConfig);

                // 设置动态properties,替换掉本地properties
                com.alibaba.otter.canal.instance.spring.support.PropertyPlaceholderConfigurer.propertiesLocal.set(properties);
                // 设置当前正在加载的通道,加载spring查找文件时会用到该变量
                System.setProperty("canal.instance.destination", destination);
                this.beanFactory = getBeanFactory(springXml);
                String beanName = destination;
                if (!beanFactory.containsBean(beanName)) {
                    beanName = defaultName;
                }

                return (CanalInstance) beanFactory.getBean(beanName);
            } catch (Throwable e) {
                logger.error("generator instance failed.", e);
                throw new CanalException(e);
            } finally {
                System.setProperty("canal.instance.destination", "");
            }
        }
    }

    // ================ setter / getter ================

    private BeanFactory getBeanFactory(String springXml) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(springXml);
        return applicationContext;
    }

    public void setCanalConfigClient(PlainCanalConfigClient canalConfigClient) {
        this.canalConfigClient = canalConfigClient;
    }

    public void setSpringXml(String springXml) {
        this.springXml = springXml;
    }

}
  • PlainCanalInstanceGenerator实现了CanalInstanceGenerator接口,其generate方法通过canal.getProperties()设置相关属性,然后再通过beanFactory.getBean(beanName)获取CanalInstance

ManagerCanalInstanceGenerator

canal-1.1.4/instance/manager/src/main/java/com/alibaba/otter/canal/instance/manager/ManagerCanalInstanceGenerator.java

public class ManagerCanalInstanceGenerator implements CanalInstanceGenerator {

    private CanalConfigClient canalConfigClient;

    public CanalInstance generate(String destination) {
        Canal canal = canalConfigClient.findCanal(destination);
        String filter = canalConfigClient.findFilter(destination);
        return new CanalInstanceWithManager(canal, filter);
    }

    // ================ setter / getter ================

    public void setCanalConfigClient(CanalConfigClient canalConfigClient) {
        this.canalConfigClient = canalConfigClient;
    }

}
  • ManagerCanalInstanceGenerator实现了CanalInstanceGenerator接口,其generate方法通过canalConfigClient获取canal及filter,然后创建CanalInstanceWithManager

SpringCanalInstanceGenerator

canal-1.1.4/instance/spring/src/main/java/com/alibaba/otter/canal/instance/spring/SpringCanalInstanceGenerator.java

public class SpringCanalInstanceGenerator implements CanalInstanceGenerator {

    private static final Logger logger      = LoggerFactory.getLogger(SpringCanalInstanceGenerator.class);
    private String              springXml;
    private String              defaultName = "instance";
    private BeanFactory         beanFactory;

    public CanalInstance generate(String destination) {
        synchronized (CanalInstanceGenerator.class) {
            try {
                // 设置当前正在加载的通道,加载spring查找文件时会用到该变量
                System.setProperty("canal.instance.destination", destination);
                this.beanFactory = getBeanFactory(springXml);
                String beanName = destination;
                if (!beanFactory.containsBean(beanName)) {
                    beanName = defaultName;
                }

                return (CanalInstance) beanFactory.getBean(beanName);
            } catch (Throwable e) {
                logger.error("generator instance failed.", e);
                throw new CanalException(e);
            } finally {
                System.setProperty("canal.instance.destination", "");
            }
        }
    }

    private BeanFactory getBeanFactory(String springXml) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(springXml);
        return applicationContext;
    }

    public void setSpringXml(String springXml) {
        this.springXml = springXml;
    }
}
  • SpringCanalInstanceGenerator实现了CanalInstanceGenerator接口,其generate方法通过beanFactory.getBean(beanName)获取CanalInstance

小结

CanalInstanceGenerator定义了generate方法用于创建指定destination的CanalInstance;它有三个实现类分别是PlainCanalInstanceGenerator、ManagerCanalInstanceGenerator、SpringCanalInstanceGenerator

doc

  • CanalInstanceGenerator

本文分享自微信公众号 - 码匠的流水账(geek_luandun),作者:码匠乱炖

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2020-04-16

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 聊聊CanalInstanceGenerator

    canal-1.1.4/instance/core/src/main/java/com/alibaba/otter/canal/instance/core/Ca...

    codecraft
  • 聊聊canal的PrometheusService

    canal-1.1.4/server/src/main/java/com/alibaba/otter/canal/spi/CanalMetricsService...

    codecraft
  • 聊聊canal的PrometheusService

    canal-1.1.4/server/src/main/java/com/alibaba/otter/canal/spi/CanalMetricsService...

    codecraft
  • 聊聊CanalInstanceGenerator

    canal-1.1.4/instance/core/src/main/java/com/alibaba/otter/canal/instance/core/Ca...

    codecraft
  • PHP7 性能翻倍关键大揭露

    20岁老牌网页程序语言PHP,最快将在10月底释出PHP 7新版,这是十年来的首次大改版,最大特色是在性能上的大突破,能比前一版PHP 5快上一倍,PHP之...

    wangxl
  • PHP之父:PHP7 性能翻倍关键大揭秘

    原文出处: ithome 20岁老牌网页程序语言PHP,最快将在10月底释出PHP 7新版,这是十年来的首次大改版,最大特色是在性能上的大突破,能比前一版PHP...

    wangxl
  • PHP小白要知道:PHP7 性能为何能翻倍的关键因素是什么

    沈唁
  • Android 应用架构演变

    代码被划分为两层结构:Data Layer(数据层)负责从数据源获取和存储数据;View Layer(视图层)负责处理并将数据展示在UI上

    用户3004328
  • 2 监听mysql表内容变化,使用canal

    mysql本身是支持主从的(master slave),原理就是master产生的binlog日志记录了所有的增删改语句,将binlog发送到slave节点进行...

    天涯泪小武
  • 字段规范 原

    wuweixiang

扫码关注云+社区

领取腾讯云代金券