前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊CanalInstanceGenerator

聊聊CanalInstanceGenerator

原创
作者头像
code4it
修改2020-04-17 10:09:19
6970
修改2020-04-17 10:09:19
举报
文章被收录于专栏:码匠的流水账

本文主要研究一下CanalInstanceGenerator

CanalInstanceGenerator

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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • CanalInstanceGenerator
  • PlainCanalInstanceGenerator
  • ManagerCanalInstanceGenerator
  • SpringCanalInstanceGenerator
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档