Search in sources :

Example 1 with SimpleCanalConnector

use of com.alibaba.otter.canal.client.impl.SimpleCanalConnector in project canal by alibaba.

the class CanalConnectors method newSingleConnector.

/**
 * 创建单链接的客户端链接
 *
 * @param address
 * @param destination
 * @param username
 * @param password
 * @return
 */
public static CanalConnector newSingleConnector(SocketAddress address, String destination, String username, String password) {
    SimpleCanalConnector canalConnector = new SimpleCanalConnector(address, username, password, destination);
    canalConnector.setSoTimeout(60 * 1000);
    canalConnector.setIdleTimeout(60 * 60 * 1000);
    return canalConnector;
}
Also used : SimpleCanalConnector(com.alibaba.otter.canal.client.impl.SimpleCanalConnector)

Example 2 with SimpleCanalConnector

use of com.alibaba.otter.canal.client.impl.SimpleCanalConnector in project canal by alibaba.

the class CanalTCPConsumer method init.

@Override
public void init(Properties properties, String destination, String groupId) {
    // load config
    String host = properties.getProperty(TCPConstants.CANAL_TCP_HOST);
    String username = properties.getProperty(TCPConstants.CANAL_TCP_USERNAME);
    String password = properties.getProperty(TCPConstants.CANAL_TCP_PASSWORD);
    String zkHosts = properties.getProperty(TCPConstants.CANAL_TCP_ZK_HOSTS);
    String batchSizePro = properties.getProperty(TCPConstants.CANAL_TCP_BATCH_SIZE);
    if (batchSizePro != null) {
        batchSize = Integer.parseInt(batchSizePro);
    }
    if (StringUtils.isNotBlank(host)) {
        String[] ipPort = host.split(":");
        SocketAddress sa = new InetSocketAddress(ipPort[0], Integer.parseInt(ipPort[1]));
        this.canalConnector = new SimpleCanalConnector(sa, username, password, destination);
    } else {
        this.canalConnector = new ClusterCanalConnector(username, password, destination, new ClusterNodeAccessStrategy(destination, ZkClientx.getZkClient(zkHosts)));
    }
}
Also used : ClusterNodeAccessStrategy(com.alibaba.otter.canal.client.impl.ClusterNodeAccessStrategy) InetSocketAddress(java.net.InetSocketAddress) ClusterCanalConnector(com.alibaba.otter.canal.client.impl.ClusterCanalConnector) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) SimpleCanalConnector(com.alibaba.otter.canal.client.impl.SimpleCanalConnector)

Example 3 with SimpleCanalConnector

use of com.alibaba.otter.canal.client.impl.SimpleCanalConnector 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

SimpleCanalConnector (com.alibaba.otter.canal.client.impl.SimpleCanalConnector)3 InetSocketAddress (java.net.InetSocketAddress)2 CanalConnector (com.alibaba.otter.canal.client.CanalConnector)1 ClusterCanalConnector (com.alibaba.otter.canal.client.impl.ClusterCanalConnector)1 ClusterNodeAccessStrategy (com.alibaba.otter.canal.client.impl.ClusterNodeAccessStrategy)1 Message (com.alibaba.otter.canal.protocol.Message)1 SocketAddress (java.net.SocketAddress)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1