Search in sources :

Example 1 with CanalConnector

use of com.alibaba.otter.canal.client.CanalConnector in project java-example by 1479005017.

the class CanalTest2 method test.

@Test
public void test() {
    CanalProperties properties = new CanalProperties();
    SocketAddress address = new InetSocketAddress(properties.getHost(), properties.getPort());
    CanalConnector connector = CanalConnectors.newSingleConnector(address, properties.getDestination(), properties.getUsername(), properties.getPassword());
    UserEntryProcessor userEntryProcessor = new UserEntryProcessor();
    CanalMessageHandler handler = new CanalMessageHandler();
    handler.setProperties(properties);
    handler.setConnector(connector);
    handler.addEntryProcessor(userEntryProcessor);
    handler.start();
    while (Thread.activeCount() > 1) Thread.yield();
}
Also used : CanalProperties(com.example.canal.CanalProperties) InetSocketAddress(java.net.InetSocketAddress) CanalConnector(com.alibaba.otter.canal.client.CanalConnector) CanalMessageHandler(com.example.canal.CanalMessageHandler) UserEntryProcessor(com.example.canal.processor.UserEntryProcessor) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 2 with CanalConnector

use of com.alibaba.otter.canal.client.CanalConnector in project canal by alibaba.

the class SimpleCanalClientTest method main.

public static void main(String[] args) {
    // 根据ip,直接创建链接,无HA的功能
    String destination = "example";
    String ip = AddressUtils.getHostIp();
    CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress(ip, 11111), destination, "canal", "canal");
    final SimpleCanalClientTest clientTest = new SimpleCanalClientTest(destination);
    clientTest.setConnector(connector);
    clientTest.start();
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        try {
            logger.info("## stop the canal client");
            clientTest.stop();
        } catch (Throwable e) {
            logger.warn("##something goes wrong when stopping canal:", e);
        } finally {
            logger.info("## canal client is down.");
        }
    }));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) CanalConnector(com.alibaba.otter.canal.client.CanalConnector)

Example 3 with CanalConnector

use of com.alibaba.otter.canal.client.CanalConnector in project canal by alibaba.

the class ClusterCanalClientTest method main.

public static void main(String[] args) {
    String destination = "example";
    // 基于固定canal server的地址,建立链接,其中一台server发生crash,可以支持failover
    // CanalConnector connector = CanalConnectors.newClusterConnector(
    // Arrays.asList(new InetSocketAddress(
    // AddressUtils.getHostIp(),
    // 11111)),
    // "stability_test", "", "");
    // 基于zookeeper动态获取canal server的地址,建立链接,其中一台server发生crash,可以支持failover
    CanalConnector connector = CanalConnectors.newClusterConnector("127.0.0.1:2181", destination, "canal", "canal");
    final ClusterCanalClientTest clientTest = new ClusterCanalClientTest(destination);
    clientTest.setConnector(connector);
    clientTest.start();
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        try {
            logger.info("## stop the canal client");
            clientTest.stop();
        } catch (Throwable e) {
            logger.warn("##something goes wrong when stopping canal:", e);
        } finally {
            logger.info("## canal client is down.");
        }
    }));
}
Also used : CanalConnector(com.alibaba.otter.canal.client.CanalConnector)

Example 4 with CanalConnector

use of com.alibaba.otter.canal.client.CanalConnector in project java-example by 1479005017.

the class CanalTest method test.

@Test
public void test() {
    // 创建链接
    CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress(AddressUtils.getHostIp(), 11111), "example", "", "");
    try {
        connector.connect();
        connector.subscribe(".*\\.user");
        while (true) {
            // 获取指定数量的数据
            Message message = connector.getWithoutAck(1000);
            try {
                if (message.getId() == -1 || message.getEntries().size() == 0) {
                    Thread.sleep(3000);
                    continue;
                }
                CanalPrinter.printSummary(message);
                CanalPrinter.printEntry(message);
                // 提交确认
                connector.ack(message.getId());
            } catch (Exception e) {
                e.printStackTrace();
                // 处理失败,回滚数据
                connector.rollback(message.getId());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        connector.disconnect();
    }
}
Also used : Message(com.alibaba.otter.canal.protocol.Message) InetSocketAddress(java.net.InetSocketAddress) CanalConnector(com.alibaba.otter.canal.client.CanalConnector) Test(org.junit.Test)

Example 5 with CanalConnector

use of com.alibaba.otter.canal.client.CanalConnector in project canal by alibaba.

the class SimpleCanalClientPermanceTest method main.

public static void main(String[] args) {
    String destination = "example";
    String ip = "127.0.0.1";
    int batchSize = 1024;
    int count = 0;
    int sum = 0;
    int perSum = 0;
    long start = System.currentTimeMillis();
    long end = 0;
    final ArrayBlockingQueue<Long> queue = new ArrayBlockingQueue<>(100);
    try {
        final CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress(ip, 11111), destination, "canal", "canal");
        Thread ackThread = new Thread(() -> {
            while (true) {
                try {
                    long batchId = queue.take();
                    connector.ack(batchId);
                } catch (InterruptedException e) {
                }
            }
        });
        ackThread.start();
        ((SimpleCanalConnector) connector).setLazyParseEntry(true);
        connector.connect();
        connector.subscribe();
        while (true) {
            Message message = connector.getWithoutAck(batchSize, 100L, TimeUnit.MILLISECONDS);
            long batchId = message.getId();
            int size = message.getRawEntries().size();
            sum += size;
            perSum += size;
            count++;
            queue.add(batchId);
            if (count % 10 == 0) {
                end = System.currentTimeMillis();
                if (end - start != 0) {
                    long tps = (perSum * 1000) / (end - start);
                    System.out.println(" total : " + sum + " , current : " + perSum + " , cost : " + (end - start) + " , tps : " + tps);
                    start = end;
                    perSum = 0;
                }
            }
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
Also used : Message(com.alibaba.otter.canal.protocol.Message) InetSocketAddress(java.net.InetSocketAddress) SimpleCanalConnector(com.alibaba.otter.canal.client.impl.SimpleCanalConnector) CanalConnector(com.alibaba.otter.canal.client.CanalConnector) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) SimpleCanalConnector(com.alibaba.otter.canal.client.impl.SimpleCanalConnector)

Aggregations

CanalConnector (com.alibaba.otter.canal.client.CanalConnector)5 InetSocketAddress (java.net.InetSocketAddress)4 Message (com.alibaba.otter.canal.protocol.Message)2 Test (org.junit.Test)2 SimpleCanalConnector (com.alibaba.otter.canal.client.impl.SimpleCanalConnector)1 CanalMessageHandler (com.example.canal.CanalMessageHandler)1 CanalProperties (com.example.canal.CanalProperties)1 UserEntryProcessor (com.example.canal.processor.UserEntryProcessor)1 SocketAddress (java.net.SocketAddress)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1