Search in sources :

Example 1 with PlainCanal

use of com.alibaba.otter.canal.instance.manager.plain.PlainCanal in project canal by alibaba.

the class PlainCanalInstanceGenerator method generate.

public CanalInstance generate(String destination) {
    synchronized (CanalEventParser.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", "");
        }
    }
}
Also used : CanalInstance(com.alibaba.otter.canal.instance.core.CanalInstance) PlainCanal(com.alibaba.otter.canal.instance.manager.plain.PlainCanal) CanalEventParser(com.alibaba.otter.canal.parse.CanalEventParser) Properties(java.util.Properties) CanalException(com.alibaba.otter.canal.common.CanalException)

Example 2 with PlainCanal

use of com.alibaba.otter.canal.instance.manager.plain.PlainCanal in project canal by alibaba.

the class PlainCanalConfigClientIntegration method testSimple.

@Test
public void testSimple() {
    PlainCanalConfigClient client = new PlainCanalConfigClient("http://127.0.0.1:8089", "admin", "4ACFE3202A5FF5CF467898FC58AAB1D615029441", "127.0.0.1", 11110);
    PlainCanal plain = client.findServer(null);
    Assert.notNull(plain);
    plain = client.findServer(plain.getMd5());
    Assert.isNull(plain);
    String instances = client.findInstances(null);
    Assert.notNull(instances);
    plain = client.findInstance("example", null);
    Assert.notNull(plain);
    plain = client.findInstance("example", plain.getMd5());
    Assert.isNull(plain);
}
Also used : PlainCanal(com.alibaba.otter.canal.instance.manager.plain.PlainCanal) PlainCanalConfigClient(com.alibaba.otter.canal.instance.manager.plain.PlainCanalConfigClient) Test(org.junit.Test)

Example 3 with PlainCanal

use of com.alibaba.otter.canal.instance.manager.plain.PlainCanal in project canal by alibaba.

the class ManagerInstanceConfigMonitor method scan.

private void scan() {
    String instances = configClient.findInstances(null);
    if (instances == null) {
        return;
    }
    final List<String> is = Lists.newArrayList(StringUtils.split(instances, ','));
    List<String> start = new ArrayList<>();
    List<String> stop = new ArrayList<>();
    List<String> restart = new ArrayList<>();
    for (String instance : is) {
        if (!configs.containsKey(instance)) {
            PlainCanal newPlainCanal = configClient.findInstance(instance, null);
            if (newPlainCanal != null) {
                configs.put(instance, newPlainCanal);
                start.add(instance);
            }
        } else {
            PlainCanal plainCanal = configs.get(instance);
            PlainCanal newPlainCanal = configClient.findInstance(instance, plainCanal.getMd5());
            if (newPlainCanal != null) {
                // 配置有变化
                restart.add(instance);
                configs.put(instance, newPlainCanal);
            }
        }
    }
    configs.forEach((instance, plainCanal) -> {
        if (!is.contains(instance)) {
            stop.add(instance);
        }
    });
    stop.forEach(instance -> {
        notifyStop(instance);
    });
    restart.forEach(instance -> {
        notifyReload(instance);
    });
    start.forEach(instance -> {
        notifyStart(instance);
    });
}
Also used : PlainCanal(com.alibaba.otter.canal.instance.manager.plain.PlainCanal) ArrayList(java.util.ArrayList)

Example 4 with PlainCanal

use of com.alibaba.otter.canal.instance.manager.plain.PlainCanal in project canal by alibaba.

the class CanalLauncher method main.

public static void main(String[] args) {
    try {
        logger.info("## set default uncaught exception handler");
        setGlobalUncaughtExceptionHandler();
        logger.info("## load canal configurations");
        String conf = System.getProperty("canal.conf", "classpath:canal.properties");
        Properties properties = new Properties();
        if (conf.startsWith(CLASSPATH_URL_PREFIX)) {
            conf = StringUtils.substringAfter(conf, CLASSPATH_URL_PREFIX);
            properties.load(CanalLauncher.class.getClassLoader().getResourceAsStream(conf));
        } else {
            properties.load(new FileInputStream(conf));
        }
        final CanalStarter canalStater = new CanalStarter(properties);
        String managerAddress = CanalController.getProperty(properties, CanalConstants.CANAL_ADMIN_MANAGER);
        if (StringUtils.isNotEmpty(managerAddress)) {
            String user = CanalController.getProperty(properties, CanalConstants.CANAL_ADMIN_USER);
            String passwd = CanalController.getProperty(properties, CanalConstants.CANAL_ADMIN_PASSWD);
            String adminPort = CanalController.getProperty(properties, CanalConstants.CANAL_ADMIN_PORT, "11110");
            boolean autoRegister = BooleanUtils.toBoolean(CanalController.getProperty(properties, CanalConstants.CANAL_ADMIN_AUTO_REGISTER));
            String autoCluster = CanalController.getProperty(properties, CanalConstants.CANAL_ADMIN_AUTO_CLUSTER);
            String name = CanalController.getProperty(properties, CanalConstants.CANAL_ADMIN_REGISTER_NAME);
            String registerIp = CanalController.getProperty(properties, CanalConstants.CANAL_REGISTER_IP);
            if (StringUtils.isEmpty(registerIp)) {
                registerIp = AddressUtils.getHostIp();
            }
            final PlainCanalConfigClient configClient = new PlainCanalConfigClient(managerAddress, user, passwd, registerIp, Integer.parseInt(adminPort), autoRegister, autoCluster, name);
            PlainCanal canalConfig = configClient.findServer(null);
            if (canalConfig == null) {
                throw new IllegalArgumentException("managerAddress:" + managerAddress + " can't not found config for [" + registerIp + ":" + adminPort + "]");
            }
            Properties managerProperties = canalConfig.getProperties();
            // merge local
            managerProperties.putAll(properties);
            int scanIntervalInSecond = Integer.valueOf(CanalController.getProperty(managerProperties, CanalConstants.CANAL_AUTO_SCAN_INTERVAL, "5"));
            executor.scheduleWithFixedDelay(new Runnable() {

                private PlainCanal lastCanalConfig;

                public void run() {
                    try {
                        if (lastCanalConfig == null) {
                            lastCanalConfig = configClient.findServer(null);
                        } else {
                            PlainCanal newCanalConfig = configClient.findServer(lastCanalConfig.getMd5());
                            if (newCanalConfig != null) {
                                // 远程配置canal.properties修改重新加载整个应用
                                canalStater.stop();
                                Properties managerProperties = newCanalConfig.getProperties();
                                // merge local
                                managerProperties.putAll(properties);
                                canalStater.setProperties(managerProperties);
                                canalStater.start();
                                lastCanalConfig = newCanalConfig;
                            }
                        }
                    } catch (Throwable e) {
                        logger.error("scan failed", e);
                    }
                }
            }, 0, scanIntervalInSecond, TimeUnit.SECONDS);
            canalStater.setProperties(managerProperties);
        } else {
            canalStater.setProperties(properties);
        }
        canalStater.start();
        runningLatch.await();
        executor.shutdownNow();
    } catch (Throwable e) {
        logger.error("## Something goes wrong when starting up the canal Server:", e);
    }
}
Also used : PlainCanal(com.alibaba.otter.canal.instance.manager.plain.PlainCanal) PlainCanalConfigClient(com.alibaba.otter.canal.instance.manager.plain.PlainCanalConfigClient) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Aggregations

PlainCanal (com.alibaba.otter.canal.instance.manager.plain.PlainCanal)4 PlainCanalConfigClient (com.alibaba.otter.canal.instance.manager.plain.PlainCanalConfigClient)2 Properties (java.util.Properties)2 CanalException (com.alibaba.otter.canal.common.CanalException)1 CanalInstance (com.alibaba.otter.canal.instance.core.CanalInstance)1 CanalEventParser (com.alibaba.otter.canal.parse.CanalEventParser)1 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1