赞
踩
原文网址:canal--canal+MQ_IT利刃出鞘的博客-CSDN博客_canal mq
环境版本
操作系统:CentOS release 6.6 (Final)
java版本: jdk1.8
canal 版本: 请下载最新的安装包,本文以当前v1.1.1 的canal.deployer-1.1.1.tar.gz为例
MySQL版本 :5.7.18(这里有一个坑,就是说mysql使用时请注意你的版本,不然报错一直连接不上)
注意 : 关闭所有机器的防火墙,同时注意启动可以相互telnet ip 端口
到官网地址(release)下载最新压缩包,请下载 canal.deployer-latest.tar.gz
将canal.deployer 复制到固定目录并解压
- mkdir -p /usr/local/canal
- cp canal.deployer-1.1.1.tar.gz /usr/local/canal
- tar -zxvf canal.deployer-1.1.1.tar.gz
开启mysql的binlog写入功能
canal的原理是基于mysql binlog技术,所以这里一定需要开启mysql的binlog写入功能,并且配置binlog模式为row.
- [mysqld]
- log-bin=mysql-bin #启用binlog
- binlog-format=ROW #选择row模式
- server_id=1 #配置mysql replaction,不能和canal的slaveId重复
创建canal用户
canal的原理是模拟自己为mysql slave,所以这里一定需要做为mysql slave的相关权限。
- CREATE USER canal IDENTIFIED BY 'canal';
- GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
- -- GRANT ALL PRIVILEGES ON *.* TO 'canal'@'%' ;
- FLUSH PRIVILEGES;
针对已有的账户可通过grants查询权限
show grants for 'canal'
a. 修改instance 配置文件 vi conf/example/instance.properties(要是你配置困难,建议你直接复制)
- #################################################
- ## mysql serverId , v1.0.26+ will autoGen
- canal.instance.mysql.slaveId=1234
-
- # enable gtid use true/false
- canal.instance.gtidon=false
-
- # position info
- 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=
-
- # rds oss binlog
- canal.instance.rds.accesskey=
- canal.instance.rds.secretkey=
- canal.instance.rds.instanceId=
-
- # table meta tsdb info
- canal.instance.tsdb.enable=true
- #canal.instance.tsdb.url=jdbc:mysql://127.0.0.1:3306/canal_tsdb
- #canal.instance.tsdb.dbUsername=canal
- #canal.instance.tsdb.dbPassword=canal
-
- #canal.instance.standby.address =
- #canal.instance.standby.journal.name =
- #canal.instance.standby.position =
- #canal.instance.standby.timestamp =
- #canal.instance.standby.gtid=
-
- # username/password
- canal.instance.dbUsername=canal
- canal.instance.dbPassword=canal
- canal.instance.connectionCharset = UTF-8
- canal.instance.defaultDatabaseName =
- # enable druid Decrypt database password
- canal.instance.enableDruid=false
- #canal.instance.pwdPublicKey=MFwwDQYJltRRE5/zXpVX7y7GCMo2/JM6fircSAwEAAQ==
-
- # table regex
- canal.instance.filter.regex=.*\\..*
- # table black regex
- canal.instance.filter.black.regex=
-
- # mq config
- canal.mq.topic=test
- # dynamic topic route by table regex
- #canal.mq.dynamicTopic=.*,mytest\\..*,mytest2.user
- canal.mq.partition=0
- # hash partition config
- #canal.mq.partitionsNum=3
- #canal.mq.partitionHash=test.table:id^name,.*\\..*
- #################################################
对应ip 地址的MySQL 数据库需进行相关初始化与设置, 可参考 Canal QuickStart
a1.多数据源配置通道:https://www.2cto.com/database/201609/547661.html
a2.常见的异常通道:https://blog.csdn.net/my201110lc/article/details/77885720
b. 修改canal 配置文件/usr/local/canal/conf/canal.properties(一般使用默认即可,可不修改。)
- #################################################
- ######### common argument #############
- #################################################
- #canal.manager.jdbc.url=jdbc:mysql://127.0.0.1:3306/canal_manager?useUnicode=true&characterEncoding=UTF-8
- #canal.manager.jdbc.username=root
- #canal.manager.jdbc.password=121212
- canal.id = 1
- canal.ip =
- canal.port = 11111
- canal.metrics.pull.port = 11112
- canal.zkServers =
- # flush data to zk
- canal.zookeeper.flush.period = 1000
- canal.withoutNetty = false
- # tcp, kafka, RocketMQ
- canal.serverMode = kafka
- # flush meta cursor/parse position to file
- canal.file.data.dir = ${canal.conf.dir}
- canal.file.flush.period = 1000
- ## memory store RingBuffer size, should be Math.pow(2,n)
- canal.instance.memory.buffer.size = 16384
- ## memory store RingBuffer used memory unit size , default 1kb
- canal.instance.memory.buffer.memunit = 1024
- ## meory store gets mode used MEMSIZE or ITEMSIZE
- canal.instance.memory.batch.mode = MEMSIZE
- canal.instance.memory.rawEntry = true
-
- ## detecing config
- canal.instance.detecting.enable = false
- #canal.instance.detecting.sql = insert into retl.xdual values(1,now()) on duplicate key update x=now()
- canal.instance.detecting.sql = select 1
- canal.instance.detecting.interval.time = 3
- canal.instance.detecting.retry.threshold = 3
- canal.instance.detecting.heartbeatHaEnable = false
-
- # support maximum transaction size, more than the size of the transaction will be cut into multiple transactions delivery
- canal.instance.transaction.size = 1024
- # mysql fallback connected to new master should fallback times
- canal.instance.fallbackIntervalInSeconds = 60
-
- # network config
- canal.instance.network.receiveBufferSize = 16384
- canal.instance.network.sendBufferSize = 16384
- canal.instance.network.soTimeout = 30
-
- # binlog filter config
- 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
-
- # binlog format/image check
- canal.instance.binlog.format = ROW,STATEMENT,MIXED
- canal.instance.binlog.image = FULL,MINIMAL,NOBLOB
-
- # binlog ddl isolation
- canal.instance.get.ddl.isolation = false
-
- # parallel parser config
- canal.instance.parser.parallel = true
- ## concurrent thread number, default 60% available processors, suggest not to exceed Runtime.getRuntime().availableProcessors()
- #canal.instance.parser.parallelThreadSize = 16
- ## disruptor ringbuffer size, must be power of 2
- canal.instance.parser.parallelBufferSize = 256
-
- # table meta tsdb info
- 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
- # dump snapshot interval, default 24 hour
- canal.instance.tsdb.snapshot.interval = 24
- # purge snapshot expire , default 360 hour(15 days)
- canal.instance.tsdb.snapshot.expire = 360
-
- # aliyun ak/sk , support rds/mq
- canal.aliyun.accesskey =
- canal.aliyun.secretkey =
-
- #################################################
- ######### destinations #############
- #################################################
- canal.destinations = example
- # conf root dir
- canal.conf.dir = ../conf
- # auto scan instance dir add/remove and start/stop instance
- canal.auto.scan = true
- canal.auto.scan.interval = 5
-
- canal.instance.tsdb.spring.xml = classpath:spring/tsdb/h2-tsdb.xml
- #canal.instance.tsdb.spring.xml = classpath:spring/tsdb/mysql-tsdb.xml
-
- canal.instance.global.mode = spring
- canal.instance.global.lazy = false
- #canal.instance.global.manager.address = 127.0.0.1:1099
- #canal.instance.global.spring.xml = classpath:spring/memory-instance.xml
- canal.instance.global.spring.xml = classpath:spring/file-instance.xml
- #canal.instance.global.spring.xml = classpath:spring/default-instance.xml
-
- ##################################################
- ######### MQ #############
- ##################################################
- canal.mq.servers = 127.0.0.1:9092
- canal.mq.retries = 0
- canal.mq.batchSize = 16384
- canal.mq.maxRequestSize = 1048576
- canal.mq.lingerMs = 1
- canal.mq.bufferMemory = 33554432
- canal.mq.canalBatchSize = 50
- canal.mq.canalGetTimeout = 100
- canal.mq.flatMessage = true
- canal.mq.compressionType = none
- canal.mq.acks = all
启动
- cd /usr/local/canal/
- sh bin/startup.sh //关闭是sh bin/stop.sh
inux带debug方式启动:(默认使用suspend=y,阻塞等待你remote debug链接成功)
/bin/startup.sh debug 9099
windows启动:(windows支持相对比较弱)
startup.bat
windows停止:直接关闭终端即可
查看日志
可以查看启动是否成功等
canal日志:canal/logs/canal/canal.log
实例日志:canal/logs/example/example.log
效果
参数名 | 参数说明 | 默认值 |
canal.mq.servers | kafka为bootstrap.servers | 127.0.0.1:6667 |
canal.mq.retries | 发送失败重试次数 | 0 |
canal.mq.batchSize | kafka为ProducerConfig.BATCH_SIZE_CONFIG | 16384 |
canal.mq.maxRequestSize | kafka为ProducerConfig.MAX_REQUEST_SIZE_CONFIG | 1048576 |
canal.mq.lingerMs | kafka为ProducerConfig.LINGER_MS_CONFIG | 1 |
canal.mq.bufferMemory | kafka为ProducerConfig.BUFFER_MEMORY_CONFIG | 33554432 |
canal.mq.producerGroup | kafka无意义 | Canal-Producer |
canal.mq.canalBatchSize | 获取canal数据的批次大小 | 50 |
canal.mq.canalGetTimeout | 获取canal数据的超时时间 | 100 |
canal.mq.flatMessage | 是否为json格式 | true |
--- | --- | --- |
canal.mq.topic | mq里的topic名 | 无 |
canal.mq.dynamicTopic | mq里的动态topic规则, 1.1.3版本支持 | 无 |
canal.mq.partition | 单队列模式的分区下标, | 1 |
canal.mq.partitionsNum | 散列模式的分区数 | 无 |
canal.mq.partitionHash | 散列规则定义 | 无 |
canal 1.1.3版本之后, 支持配置格式:schema.table,多个配置之间使用逗号分隔。
例:
表达式 | 含义 |
test\\.test | 指定匹配的单表,发送到以 test\\.test为名字的topic上 |
.\\..* | 匹配所有表,每个表都会发送到各自表名的topic上 |
test | 指定匹配对应的库,一个库的所有表都会发送到库名的topic上 |
test\\.* | 指定匹配的表达式,针对匹配的表会发送到各自表名的topic上 |
test,test1\\.test1 | test1指定多个表达式,会将test库的表都发送到test的topic上,test1\\.test1的表发送到对应的test1\\.test1 topic上,其余的表发送到默认的canal.mq.topic值 |
大家可以结合自己的业务需求,设置匹配规则,建议MQ开启自动创建topic的能力
canal 1.1.3版本之后, 支持配置格式:schema.table:pk1^pk2,多个配置之间使用逗号分隔。
表达式 | 含义 |
test\\.test:pk1^pk2 | 指定匹配的单表,对应的hash字段为pk1 + pk2 |
.\\..*:id | 匹配,指定所有正则匹配的表对应的hash字段为id |
.\\..*:$pk$ | 正则匹配,指定所有正则匹配的表对应的hash字段为表主键(自动查找) |
匹配规则啥都不写,则默认发到0这个partition上 | |
.\\..* | 不指定pk信息的正则匹配,将所有正则匹配的表,对应的hash字段为表名 按表hash: 一张表的所有数据可以发到同一个分区,不同表之间会做散列 (会有热点表分区过大问题) |
test\\.test:id,.\\..* | 针对test的表按照id散列,其余的表按照table散列 |
注意:大家可以结合自己的业务需求,设置匹配规则,多条匹配规则之间是按照顺序进行匹配(命中一条规则就返回)
其他详细参数可参考Canal AdminGuide
binlog本身是有序的,写入到mq之后如何保障顺序是很多人会比较关注,在issue里也有非常多人咨询了类似的问题,这里做一个统一的解答
canal的消费顺序性,主要取决于描述2中的路由选择,举例说明:
canal.client下有对应的MQ数据消费的样例工程,包含数据编解码的功能
kafka模式: com.alibaba.otter.canal.client.running.kafka.CanalKafkaClientExample
rocketMQ模式: com.alibaba.otter.canal.client.running.rocketmq.CanalRocketMQClientExample
https://github.com/alibaba/canal/wiki/Canal-Kafka-RocketMQ-QuickStart
canal实现mysql实时数据binlog同步_数据库_阿拉斯加大闸蟹的博客-CSDN博客
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。