canal 监控mysql数据更新

官方的quickStart

https://github.com/alibaba/canal/wiki/QuickStart

本文参考

https://blog.csdn.net/hackerwin7/article/details/37923607

过程

1.打开mysql的binlog-在mysql的my.cnf文件里

[mysqld]
log-bin=mysql-bin #添加这一行就ok
binlog-format=ROW #选择row模式
server_id=1 #配置mysql replaction需要定义,不能和canal的slaveId重复

2.给canal设定用户和赋予权限

CREATE USER canal IDENTIFIED BY 'canal';  
# 网上的一些配置,这里只赋予了一部分权限,在高版本的mysql中会提示权限不足,这里直接给所有权限
# 实际使用中应该根据自己的需要,赋予需要 权限
GRANT ALL PRIVILEGES ON *.* TO 'canal'@'%' ;
FLUSH PRIVILEGES;

3.访问:https://github.com/alibaba/canal/releases 下载一个deployer版本,解压

4.更改配置

vi conf/example/instance.properties
#################################################
## mysql serverId
canal.instance.mysql.slaveId = 1234
#p需要改成自己的数据库信息
canal.instance.master.address = 127.0.0.1:3306


#username/password,需要改成自己的数据库信息
#用户名和密码就是在上面创建的那个用户
canal.instance.dbUsername = canal
canal.instance.dbPassword = canal
canal.instance.defaultDatabaseName = test
canal.instance.connectionCharset = UTF-8


#table regex 表示只关注这些表的数据更新,可根据自己需要设置
canal.instance.filter.regex = .\..


#################################################

5.启动

sh bin/startup.sh
#查看日志,若出问题,也可以在这个日志中看到
vi logs/canal/canal.log
#看到以下信息说明启动成功

2019-02-14 15:39:37.577 [main] INFO  com.alibaba.otter.canal.deployer.CanalLauncher - ## set default uncaught exception handler
2019-02-14 15:39:37.643 [main] INFO  com.alibaba.otter.canal.deployer.CanalLauncher - ## load canal configurations
2019-02-14 15:39:37.659 [main] INFO  com.alibaba.otter.canal.deployer.CanalLauncher - ## start the canal server.
2019-02-14 15:39:37.711 [main] INFO  com.alibaba.otter.canal.deployer.CanalController - ## start the canal server[172.26.252.38:11111]

canal-client.java-获取mysql数据更新

package canal;

import java.net.InetSocketAddress;
import java.util.List;

import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.common.utils.AddressUtils;
import com.alibaba.otter.canal.protocol.Message;
import com.alibaba.otter.canal.protocol.CanalEntry.Column;
import com.alibaba.otter.canal.protocol.CanalEntry.Entry;
import com.alibaba.otter.canal.protocol.CanalEntry.EntryType;
import com.alibaba.otter.canal.protocol.CanalEntry.EventType;
import com.alibaba.otter.canal.protocol.CanalEntry.RowChange;
import com.alibaba.otter.canal.protocol.CanalEntry.RowData;
import com.alibaba.otter.canal.client.*;

public class ClientSample {

    public static void main(String args[]) {
        // 创建链接
        CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress(AddressUtils.getHostIp(),
                11111), "example", "", "");
        int batchSize = 1000;
        int emptyCount = 0;
        try {
            connector.connect();
            connector.subscribe(".*\\..*");
            connector.rollback();
            int totalEmtryCount = 1200;
            while (emptyCount < totalEmtryCount) {
                Message message = connector.getWithoutAck(batchSize);
                long batchId = message.getId();
                int size = message.getEntries().size();
                if (batchId == -1 || size == 0) {
                    emptyCount++;
                    System.out.println("empty count : " + emptyCount);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    emptyCount = 0;
                    printEntry(message.getEntries());
                }

                connector.ack(batchId);
            }

            System.out.println("empty too many times, exit");
        } finally {
            connector.disconnect();
        }
    }

    private static void printEntry( List entrys) {
        for (Entry entry : entrys) {
            if (entry.getEntryType() == EntryType.TRANSACTIONBEGIN || entry.getEntryType() == EntryType.TRANSACTIONEND) {
                continue;
            }

            RowChange rowChage = null;
            try {
                rowChage = RowChange.parseFrom(entry.getStoreValue());
            } catch (Exception e) {
                throw new RuntimeException("ERROR ## parser of eromanga-event has an error , data:" + entry.toString(),
                        e);
            }

            EventType eventType = rowChage.getEventType();
            System.out.println(String.format("================> binlog[%s:%s] , name[%s,%s] , eventType : %s",
                    entry.getHeader().getLogfileName(), entry.getHeader().getLogfileOffset(),
                    entry.getHeader().getSchemaName(), entry.getHeader().getTableName(),
                    eventType));

            for (RowData rowData : rowChage.getRowDatasList()) {
                if (eventType == EventType.DELETE) {
                    printColumn(rowData.getBeforeColumnsList());
                } else if (eventType == EventType.INSERT) {
                    printColumn(rowData.getAfterColumnsList());
                } else {
                    System.out.println("-------> before");
                    printColumn(rowData.getBeforeColumnsList());
                    System.out.println("-------> after");
                    printColumn(rowData.getAfterColumnsList());
                }
            }
        }
    }

    private static void printColumn(List columns) {
        for (Column column : columns) {
            System.out.println(column.getName() + " : " + column.getValue() + "    update=" + column.getUpdated());
        }
    }
}

对应的数据更新,都会在控制台显示出来

empty count : 14
empty count : 15
empty count : 16
================> binlog[mysql-bin.000001:3666] , name[tt,tt] , eventType : INSERT
id : 1    update=true
empty count : 1
empty count : 2
empty count : 3

 

你可能感兴趣的:(canal 监控mysql数据更新)