Search in sources :

Example 1 with CanalMQProducer

use of com.alibaba.otter.canal.connector.core.spi.CanalMQProducer in project canal by alibaba.

the class CanalKafkaProducerTest method testLoadKafkaProducer.

@Test
public void testLoadKafkaProducer() throws IOException {
    Properties pro = new Properties();
    FileInputStream in = new FileInputStream("../../deployer/src/main/resources/canal.properties");
    pro.load(in);
    ExtensionLoader<CanalMQProducer> loader = ExtensionLoader.getExtensionLoader(CanalMQProducer.class);
    CanalMQProducer canalMQProducer = loader.getExtension("kafka", "/../../deployer/target/canal/plugin", "/../../deployer/target/canal/plugin");
    canalMQProducer.init(pro);
    in.close();
}
Also used : CanalMQProducer(com.alibaba.otter.canal.connector.core.spi.CanalMQProducer) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 2 with CanalMQProducer

use of com.alibaba.otter.canal.connector.core.spi.CanalMQProducer in project canal by alibaba.

the class CanalStarter method start.

/**
 * 启动方法
 *
 * @throws Throwable
 */
public synchronized void start() throws Throwable {
    String serverMode = CanalController.getProperty(properties, CanalConstants.CANAL_SERVER_MODE);
    if (!"tcp".equalsIgnoreCase(serverMode)) {
        ExtensionLoader<CanalMQProducer> loader = ExtensionLoader.getExtensionLoader(CanalMQProducer.class);
        canalMQProducer = loader.getExtension(serverMode.toLowerCase(), CONNECTOR_SPI_DIR, CONNECTOR_STANDBY_SPI_DIR);
        if (canalMQProducer != null) {
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(canalMQProducer.getClass().getClassLoader());
            canalMQProducer.init(properties);
            Thread.currentThread().setContextClassLoader(cl);
        }
    }
    if (canalMQProducer != null) {
        MQProperties mqProperties = canalMQProducer.getMqProperties();
        // disable netty
        System.setProperty(CanalConstants.CANAL_WITHOUT_NETTY, "true");
        if (mqProperties.isFlatMessage()) {
            // 设置为raw避免ByteString->Entry的二次解析
            System.setProperty("canal.instance.memory.rawEntry", "false");
        }
    }
    logger.info("## start the canal server.");
    controller = new CanalController(properties);
    controller.start();
    logger.info("## the canal server is running now ......");
    shutdownThread = new Thread(() -> {
        try {
            logger.info("## stop the canal server");
            controller.stop();
            CanalLauncher.runningLatch.countDown();
        } catch (Throwable e) {
            logger.warn("##something goes wrong when stopping canal Server:", e);
        } finally {
            logger.info("## canal server is down.");
        }
    });
    Runtime.getRuntime().addShutdownHook(shutdownThread);
    if (canalMQProducer != null) {
        canalMQStarter = new CanalMQStarter(canalMQProducer);
        String destinations = CanalController.getProperty(properties, CanalConstants.CANAL_DESTINATIONS);
        canalMQStarter.start(destinations);
        controller.setCanalMQStarter(canalMQStarter);
    }
    // start canalAdmin
    String port = CanalController.getProperty(properties, CanalConstants.CANAL_ADMIN_PORT);
    if (canalAdmin == null && StringUtils.isNotEmpty(port)) {
        String user = CanalController.getProperty(properties, CanalConstants.CANAL_ADMIN_USER);
        String passwd = CanalController.getProperty(properties, CanalConstants.CANAL_ADMIN_PASSWD);
        CanalAdminController canalAdmin = new CanalAdminController(this);
        canalAdmin.setUser(user);
        canalAdmin.setPasswd(passwd);
        String ip = CanalController.getProperty(properties, CanalConstants.CANAL_IP);
        logger.debug("canal admin port:{}, canal admin user:{}, canal admin password: {}, canal ip:{}", port, user, passwd, ip);
        CanalAdminWithNetty canalAdminWithNetty = CanalAdminWithNetty.instance();
        canalAdminWithNetty.setCanalAdmin(canalAdmin);
        canalAdminWithNetty.setPort(Integer.parseInt(port));
        canalAdminWithNetty.setIp(ip);
        canalAdminWithNetty.start();
        this.canalAdmin = canalAdminWithNetty;
    }
    running = true;
}
Also used : CanalMQStarter(com.alibaba.otter.canal.server.CanalMQStarter) CanalMQProducer(com.alibaba.otter.canal.connector.core.spi.CanalMQProducer) CanalAdminController(com.alibaba.otter.canal.deployer.admin.CanalAdminController) CanalAdminWithNetty(com.alibaba.otter.canal.admin.netty.CanalAdminWithNetty) MQProperties(com.alibaba.otter.canal.connector.core.config.MQProperties)

Aggregations

CanalMQProducer (com.alibaba.otter.canal.connector.core.spi.CanalMQProducer)2 CanalAdminWithNetty (com.alibaba.otter.canal.admin.netty.CanalAdminWithNetty)1 MQProperties (com.alibaba.otter.canal.connector.core.config.MQProperties)1 CanalAdminController (com.alibaba.otter.canal.deployer.admin.CanalAdminController)1 CanalMQStarter (com.alibaba.otter.canal.server.CanalMQStarter)1 FileInputStream (java.io.FileInputStream)1 Properties (java.util.Properties)1 Test (org.junit.Test)1