canal简介
canal,译意为水道/管道/沟渠,主要用途是基于 MySQL 数据库增量日志解析,提供增量数据订阅和消费
基于日志增量订阅和消费的业务包括
- 数据库镜像
- 数据库实时备份
- 索引构建和实时维护(拆分异构索引、倒排索引等)
- 业务 cache 刷新
- 带业务逻辑的增量数据处理
当前的 canal 支持源端 MySQL 版本包括 5.1.x , 5.5.x , 5.6.x , 5.7.x , 8.0.x
canal和canal-adapter下载
官方下载地址: https://github.com/alibaba/canal/releases 百度云-1.1.4:https://pan.baidu.com/s/1T1ZVjmluu99NlVRUWtDnUw 密码:snmc
Mysql配置
[mysqld]
log-bin=mysql-bin
binlog-format=ROW
server_id=1
查看是否开启bin-log show variables like ‘log_bin’;
查看bin-log的模式 show variables like ‘binlog_format’;
canal安装及配置
安装
下载的canal.deployer-1.1.4.tar.gz拷贝到linux目录下进行解压
tar -zxvf canal.deployer-1.1.4.tar.gz -C /opt/module/canal.deployer
注意:canal解压后是散的,我们在指定解压目录的时候需要将canal指定上
配置
本文测试的是canal实时同步mysql数据到kafka
修改 conf/canal.properties 的配置
canal.ip =
canal.register.ip =
canal.port = 11111
canal.metrics.pull.port = 11112
canal.admin.port = 11110
canal.admin.user = admin
canal.admin.passwd = 4ACFE3202A5FF5CF467898FC58AAB1D615029441
canal.zkServers =
canal.zookeeper.flush.period = 1000
canal.withoutNetty = false
canal.serverMode = kafka
canal.file.data.dir = ${canal.conf.dir}
canal.file.flush.period = 1000
canal.instance.memory.buffer.size = 16384
canal.instance.memory.buffer.memunit = 1024
canal.instance.memory.batch.mode = MEMSIZE
canal.instance.memory.rawEntry = true
canal.instance.detecting.enable = false
canal.instance.detecting.sql = select 1
canal.instance.detecting.interval.time = 3
canal.instance.detecting.retry.threshold = 3
canal.instance.detecting.heartbeatHaEnable = false
canal.instance.transaction.size = 1024
canal.instance.fallbackIntervalInSeconds = 60
canal.instance.network.receiveBufferSize = 16384
canal.instance.network.sendBufferSize = 16384
canal.instance.network.soTimeout = 30
canal.instance.filter.druid.ddl = true
canal.instance.filter.query.dcl = false
canal.instance.filter.query.dml = false
canal.instance.filter.query.ddl = false
canal.instance.filter.table.error = false
canal.instance.filter.rows = false
canal.instance.filter.transaction.entry = false
canal.instance.binlog.format = ROW,STATEMENT,MIXED
canal.instance.binlog.image = FULL,MINIMAL,NOBLOB
canal.instance.get.ddl.isolation = false
canal.instance.parser.parallel = true
canal.instance.parser.parallelThreadSize = 16
canal.instance.parser.parallelBufferSize = 256
canal.instance.tsdb.enable = true
canal.instance.tsdb.dir = ${canal.file.data.dir:../conf}/${canal.instance.destination:}
canal.instance.tsdb.url = jdbc:h2:${canal.instance.tsdb.dir}/h2;CACHE_SIZE=1000;MODE=MYSQL;
canal.instance.tsdb.dbUsername = canal
canal.instance.tsdb.dbPassword = canal
canal.instance.tsdb.snapshot.interval = 24
canal.instance.tsdb.snapshot.expire = 360
canal.aliyun.accessKey =
canal.aliyun.secretKey =
canal.destinations = example
canal.conf.dir = ../conf
canal.auto.scan = true
canal.auto.scan.interval = 5
canal.instance.tsdb.spring.xml = classpath:spring/tsdb/h2-tsdb.xml
canal.instance.global.mode = spring
canal.instance.global.lazy = false
canal.instance.global.manager.address = ${canal.admin.manager}
canal.instance.global.spring.xml = classpath:spring/file-instance.xml
canal.mq.servers = xxx:9092,xxx:9092,xxx:9092
canal.mq.retries = 0
canal.mq.batchSize = 16384
canal.mq.maxRequestSize = 1048576
canal.mq.lingerMs = 100
canal.mq.bufferMemory = 33554432
canal.mq.canalBatchSize = 50
canal.mq.canalGetTimeout = 100
canal.mq.flatMessage = true
canal.mq.compressionType = none
canal.mq.acks = all
canal.mq.producerGroup = test
canal.mq.accessChannel = local
canal.mq.kafka.kerberos.enable = false
canal.mq.kafka.kerberos.krb5FilePath = "../conf/kerberos/krb5.conf"
canal.mq.kafka.kerberos.jaasFilePath = "../conf/kerberos/jaas.conf"
详细参数配置: https://github.com/alibaba/canal/wiki/Canal-Kafka-RocketMQ-QuickStart
修改 example/instance.properties的配置
canal.instance.gtidon=false
canal.instance.master.address=127.0.0.1:3306
canal.instance.master.journal.name=
canal.instance.master.position=
canal.instance.master.timestamp=
canal.instance.master.gtid=
canal.instance.rds.accesskey=
canal.instance.rds.secretkey=
canal.instance.rds.instanceId=
canal.instance.tsdb.enable=true
canal.instance.dbUsername=canal
canal.instance.dbPassword=canal
canal.instance.connectionCharset = UTF-8
canal.instance.enableDruid=false
canal.instance.filter.regex=test.test
canal.mq.topic=kf_test
canal.mq.partition=0
启动 canal
到canal安装的bin路径下启动 看到 CanalLauncher 你表示启动成功,同时会创建 kafka主题
canal-adapter安装及配置
安装
下载的canal.adapter-1.1.4.tar.gz拷贝到linux目录下进行解压
tar -zxvf canal.adapter-1.1.4.tar.gz -C /opt/module/canal.adapter
配置
canal.adapter读取上面canal发送到kafka的数据到postgres中
修改conf/application.yml文件
server:
port: 8081
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
default-property-inclusion: non_null
canal.conf:
mode: kafka
mqServers: xxx:9092,xxx:9092,xxx:9092
batchSize: 5
syncBatchSize: 2
retries: 0
timeout:
accessKey:
secretKey:
srcDataSources:
defaultDS:
url: jdbc:127.0.0.1:3306/test?useUnicode=true
username: canal
password: canal
canalAdapters:
- instance: kf_test
groups:
- groupId: g1
outerAdapters:
- name: logger
- name: rdb
key: postgres1
properties:
jdbc.driverClassName: org.postgresql.Driver
jdbc.url: jdbc:postgresql:120.0.0.1:3433/dapeng
jdbc.username: canal
jdbc.password: canal
threads: 1000
commitSize: 3000
修改conf/rdb/下的mytest_user.yml文件
dataSourceKey: defaultDS
destination: topic1
groupId: g1
outerAdapterKey: postgres1
concurrent: true
dbMapping:
database: test
table: test
targetTable: public.test
targetPk:
id: id
mapAll: true
commitBatch: 3000
启动 canal-adapter
到canal-adapter安装的bin路径下启动 看到 CanalAdapterApplication表示启动成功
|