Canal 代码 - EventParser

git 地址

https://github.com/alibaba/canal

架构

(图片来自:https://www.jianshu.com/p/0ccbd1a1a5ec

代码

  • Demo

com/alibaba/otter/canal/parse/MysqlBinlogDumpPerformanceTest.java

  • start

// com/alibaba/otter/canal/parse/inbound/AbstractEventParser.java
public void start() {
        super.start();
        MDC.put("destination", destination);
        // 配置transaction buffer
        // 初始化缓冲队列
        transactionBuffer.setBufferSize(transactionSize);// 设置buffer大小
        transactionBuffer.start();
        // 构造bin log parser
        binlogParser = buildParser();// 初始化一下BinLogParser
        binlogParser.start();
        // 启动工作线程
        parseThread = new Thread(new Runnable() {

            public void run() {
                MDC.put("destination", String.valueOf(destination));
                ErosaConnection erosaConnection = null;
                while (running) {
                    try {
                        // 开始执行replication
                        // 1. 构造Erosa连接,对于 Mysql 而言,就是连接 Mysql
                        erosaConnection = buildErosaConnection();

                        // 2. 启动一个心跳线程,定期往 EventSink 传输数据,
                        // 保证下游能收到数据(在上游 mysql 无 binlog 时),能知道 Canal server 自身是否工作正常。
                        startHeartBeat(erosaConnection);

                        // 3. 执行 dump 前的准备工作,包括如下:
                        // 检测 BinlogFormat,BinlogImage
                        // show variables like 'binlog_format',参考:https://www.cnblogs.com/xingyunfashi/p/8431780.html
                        // show variables like 'binlog_row_image'  ->  参考:https://blog.csdn.net/actiontech/article/details/81701362
                        preDump(erosaConnection);

                        // 连接源 mysql
                        erosaConnection.connect();

                        // show variables like 'server_id';
                        long queryServerId = erosaConnection.queryServerId();
                        if (queryServerId != 0) {
                            serverId = queryServerId;
                        }

                        // 4. 获取起始位点
                        long start = System.currentTimeMillis();
                        logger.warn("---> begin to find start position, it will be long time for reset or first position");
                        EntryPosition position = findStartPosition(erosaConnection);
                        final EntryPosition startPosition = position;
                        if (startPosition == null) {
                            throw new PositionNotFoundException("can't find start position for " + destination);
                        }

                        ........

                        // 重新链接,因为在找position过程中可能有状态,需要断开后重建
                        erosaConnection.reconnect();

                        final SinkFunction sinkHandler = new SinkFunction<EVENT>() {

                            private LogPosition lastPosition;

                            public boolean sink(EVENT event) {
                                try {
                                    CanalEntry.Entry entry = parseAndProfilingIfNecessary(event, false);

                                    if (!running) {
                                        return false;
                                    }

                                    if (entry != null) {
                                        exception = null; // 有正常数据流过,清空exception
                                        transactionBuffer.add(entry);
                                        // 记录一下对应的positions
                                        this.lastPosition = buildLastPosition(entry);
                                        // 记录一下最后一次有数据的时间
                                        lastEntryTime = System.currentTimeMillis();
                                    }
                                    return running;
                                } catch (TableIdNotFoundException e) {
                                    throw e;
                                } catch (Throwable e) {
                                    if (e.getCause() instanceof TableIdNotFoundException) {
                                        throw (TableIdNotF
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Kubernetes (K8s) 上部署 Canal Server,你可以使用 Deployment 和 Service 来创建和管理它。 首先,你需要创建一个 Canal Server 的配置文件,例如 `canal-server.yaml`,其内容类似于以下示例: ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: canal-server spec: replicas: 1 selector: matchLabels: app: canal-server template: metadata: labels: app: canal-server spec: containers: - name: canal-server image: canal/canal-server:v1.1.4 ports: - containerPort: 11111 env: - name: canal.destinations value: example --- apiVersion: v1 kind: Service metadata: name: canal-server spec: selector: app: canal-server ports: - port: 11111 targetPort: 11111 ``` 上述配置文件定义了一个名为 `canal-server` 的 Deployment 和一个名为 `canal-server` 的 Service。Deployment 中的容器使用 `canal/canal-server:v1.1.4` 镜像,并将容器的端口设置为 11111。Service 将流量转发到 Pod 的容器端口 11111。 执行以下命令来部署 Canal Server: ```shell kubectl apply -f canal-server.yaml ``` 这将创建一个名为 `canal-server` 的 Deployment 和一个名为 `canal-server` 的 Service。 你可以使用以下命令来检查 Deployment 和 Service 的状态: ```shell kubectl get deployments kubectl get services ``` 一旦 Deployment 和 Service 创建成功,你就可以通过 Service 的 Cluster IP 或 NodePort 来访问 Canal Server。 请注意,上述示例仅供参考,你需要根据自己的实际需求进行修改和调整。另外,还需要确保你的 Kubernetes 集群已正确配置,并且能够拉取指定的 Canal Server 镜像。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值