ClientSample

    • 可以在eclipse里,直接打开com.alibaba.otter.canal.example.SimpleCanalClientTest,直接运行
    • 在工程的example目录下运行命令行:
      mvn exec:java -Dexec.mainClass="com.alibaba.otter.canal.example.SimpleCanalClientTest"

    • 下载example包: https://github.com/alibaba/canal/releases,解压缩后,直接运行sh startup.sh脚本c. 触发数据变更d. 在控制台或者logs中查看,可以看到如下信息 :


    依赖配置:

    1. <dependency>
    2. <groupId>com.alibaba.otter</groupId>
    3. <artifactId>canal.client</artifactId>
    4. <version>1.0.12</version>
    5. </dependency>
    1. 创建mvn标准工程:
    1. mvn archetype:create -DgroupId=com.alibaba.otter -DartifactId=canal.sample
    maven3.0.5以上版本舍弃了create,使用generate生成项目
    1. mvn archetype:generate -DgroupId=com.alibaba.otter -DartifactId=canal.sample
    1. 修改pom.xml,添加依赖
    1. ClientSample代码
    1. package com.alibaba.otter.canal.sample;
    2.  
    3. import java.net.InetSocketAddress;
    4. import java.util.List;
    5.  
    6.  
    7. import com.alibaba.otter.canal.client.CanalConnectors;
    8. import com.alibaba.otter.canal.client.CanalConnector;
    9. import com.alibaba.otter.canal.common.utils.AddressUtils;
    10. import com.alibaba.otter.canal.protocol.Message;
    11. import com.alibaba.otter.canal.protocol.CanalEntry.Column;
    12. import com.alibaba.otter.canal.protocol.CanalEntry.Entry;
    13. import com.alibaba.otter.canal.protocol.CanalEntry.EntryType;
    14. import com.alibaba.otter.canal.protocol.CanalEntry.EventType;
    15. import com.alibaba.otter.canal.protocol.CanalEntry.RowChange;
    16. import com.alibaba.otter.canal.protocol.CanalEntry.RowData;
    17.  
    18.  
    19. public class SimpleCanalClientExample {
    20.  
    21.  
    22.     // 创建链接
    23.     CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress(AddressUtils.getHostIp(),
    24.                                                                                         11111), "example", "", "");
    25.     int batchSize = 1000;
    26.     int emptyCount = 0;
    27.     try {
    28.         connector.subscribe(".*\\..*");
    29.         connector.rollback();
    30.         int totalEmptyCount = 120;
    31.         while (emptyCount < totalEmptyCount) {
    32.             Message message = connector.getWithoutAck(batchSize); // 获取指定数量的数据
    33.             long batchId = message.getId();
    34.             int size = message.getEntries().size();
    35.             if (batchId == -1 || size == 0) {
    36.                 emptyCount++;
    37.                 System.out.println("empty count : " + emptyCount);
    38.                 try {
    39.                     Thread.sleep(1000);
    40.                 } catch (InterruptedException e) {
    41.                 }
    42.             } else {
    43.                 // System.out.printf("message[batchId=%s,size=%s] \n", batchId, size);
    44.                 printEntry(message.getEntries());
    45.             connector.ack(batchId); // 提交确认
    46.             // connector.rollback(batchId); // 处理失败, 回滚数据
    47.         }
    48.         System.out.println("empty too many times, exit");
    49.     } finally {
    50.         connector.disconnect();
    51.     }
    52. }
    53. private static void printEntry(List<Entry> entrys) {
    54.     for (Entry entry : entrys) {
    55.         if (entry.getEntryType() == EntryType.TRANSACTIONBEGIN || entry.getEntryType() == EntryType.TRANSACTIONEND) {
    56.             continue;
    57.         }
    58.         RowChange rowChage = null;
    59.         try {
    60.             rowChage = RowChange.parseFrom(entry.getStoreValue());
    61.         } catch (Exception e) {
    62.             throw new RuntimeException("ERROR ## parser of eromanga-event has an error , data:" + entry.toString(),
    63.                                        e);
    64.         }
    65.         System.out.println(String.format("================&gt; binlog[%s:%s] , name[%s,%s] , eventType : %s",
    66.                                          entry.getHeader().getLogfileName(), entry.getHeader().getLogfileOffset(),
    67.                                          entry.getHeader().getSchemaName(), entry.getHeader().getTableName(),
    68.                                          eventType));
    69.         for (RowData rowData : rowChage.getRowDatasList()) {
    70.             if (eventType == EventType.DELETE) {
    71.                 printColumn(rowData.getBeforeColumnsList());
    72.             } else if (eventType == EventType.INSERT) {
    73.                 printColumn(rowData.getAfterColumnsList());
    74.             } else {
    75.                 System.out.println("-------&gt; before");
    76.                 printColumn(rowData.getBeforeColumnsList());
    77.                 System.out.println("-------&gt; after");
    78.                 printColumn(rowData.getAfterColumnsList());
    79.             }
    80.         }
    81.     }
    82. }
    83. private static void printColumn(List<Column> columns) {
    84.     for (Column column : columns) {
    85.         System.out.println(column.getName() + " : " + column.getValue() + "    update=" + column.getUpdated());
    86.     }
    87.  
    88.  
    89.  

    1. 运行Client

    首先启动Canal Server,可参见

    启动Canal Client后,可以从控制台从看到类似消息:

    1. empty count : 1
    2. empty count : 2
    3. empty count : 3
    4. empty count : 4
    1. 触发数据库变更

    可以从控制台中看到:

    1. empty count : 1
    2. empty count : 2
    3. empty count : 3
    4. empty count : 4
    5. ================> binlog[mysql-bin.001946:313661577] , name[test,xdual] , eventType : INSERT
    6. ID : 4 update=true
    7. X : 2013-02-05 23:29:46 update=true

    如果需要更详细的exmpale例子,请下载canal当前最新源码包,里面有个example工程,谢谢.