Search in sources :

Example 1 with PlainCanalConfigClient

use of com.alibaba.otter.canal.instance.manager.plain.PlainCanalConfigClient 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 2 with PlainCanalConfigClient

use of com.alibaba.otter.canal.instance.manager.plain.PlainCanalConfigClient 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)2 PlainCanalConfigClient (com.alibaba.otter.canal.instance.manager.plain.PlainCanalConfigClient)2 FileInputStream (java.io.FileInputStream)1 Properties (java.util.Properties)1 Test (org.junit.Test)1