canal--canal+MQ 阳光穿透心脏的1/2处 2023-05-21 11:55 49阅读 0赞 首发网址:[canal--canal+MQ\_IT利刃出鞘的博客-CSDN博客\_canal mq][canal--canal_MQ_IT_-CSDN_canal mq] # 其他网址 # [https://github.com/alibaba/canal/wiki/Canal-Kafka-RocketMQ-QuickStart][https_github.com_alibaba_canal_wiki_Canal-Kafka-RocketMQ-QuickStart] [canal实现mysql实时数据binlog同步\_数据库\_阿拉斯加大闸蟹的博客-CSDN博客][canal_mysql_binlog_-CSDN] # 部署与配置 # ## 下载与安装 ## 环境版本 > 操作系统: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配置修改参数 ## **开启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' ## 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 > 1. linux启动完成后,会在bin目录下生成canal.pid,stop.sh会读取canal.pid进行进程关闭 > 2. startup.sh默认读取系统环境变量中的which java获得JAVA执行路径,需要设置PATH=$JAVA\_HOME/bin环境变量 windows启动:(windows支持相对比较弱) > startup.bat windows停止:直接关闭终端即可 **查看日志** 可以查看启动是否成功等 > canal日志:canal/logs/canal/canal.log > 实例日志:canal/logs/example/example.log **效果** ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmdnZWw_size_16_color_FFFFFF_t_70][] # mq相关参数说明 # <table> <tbody> <tr> <td style="width:219px;"> <p style="margin-left:0pt;"><strong><span style="color:#4f4f4f;"><strong>参数名</strong></span></strong></p> </td> <td style="width:395px;"> <p style="margin-left:0pt;"><strong><span style="color:#4f4f4f;"><strong>参数说明</strong></span></strong></p> </td> <td style="width:150px;"> <p style="margin-left:0pt;"><strong><span style="color:#4f4f4f;"><strong>默认值</strong></span></strong></p> </td> </tr> <tr> <td style="width:219px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">canal.mq.servers</span></p> </td> <td style="width:395px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">kafka为bootstrap.servers </span><br><span style="color:#4f4f4f;">rocketMQ中为nameserver列表</span></p> </td> <td style="width:150px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">127.0.0.1:6667</span></p> </td> </tr> <tr> <td style="width:219px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">canal.mq.retries</span></p> </td> <td style="width:395px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">发送失败重试次数</span></p> </td> <td style="width:150px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">0</span></p> </td> </tr> <tr> <td style="width:219px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">canal.mq.batchSize</span></p> </td> <td style="width:395px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">kafka为</span><span style="color:#4f4f4f;">ProducerConfig.BATCH_SIZE_CONFIG</span> <br><span style="color:#4f4f4f;">rocketMQ无意义</span></p> </td> <td style="width:150px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">16384</span></p> </td> </tr> <tr> <td style="width:219px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">canal.mq.maxRequestSize</span></p> </td> <td style="width:395px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">kafka为</span><span style="color:#4f4f4f;">ProducerConfig.MAX_REQUEST_SIZE_CONFIG</span> <br><span style="color:#4f4f4f;">rocketMQ无意义</span></p> </td> <td style="width:150px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">1048576</span></p> </td> </tr> <tr> <td style="width:219px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">canal.mq.lingerMs</span></p> </td> <td style="width:395px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">kafka为</span><span style="color:#4f4f4f;">ProducerConfig.LINGER_MS_CONFIG</span> <br><span style="color:#4f4f4f;">rocketMQ无意义</span></p> </td> <td style="width:150px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">1</span></p> </td> </tr> <tr> <td style="width:219px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">canal.mq.bufferMemory</span></p> </td> <td style="width:395px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">kafka为</span><span style="color:#4f4f4f;">ProducerConfig.BUFFER_MEMORY_CONFIG</span> <br><span style="color:#4f4f4f;">rocketMQ无意义</span></p> </td> <td style="width:150px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">33554432</span></p> </td> </tr> <tr> <td style="width:219px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">canal.mq.producerGroup</span></p> </td> <td style="width:395px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">kafka无意义 </span><br><span style="color:#4f4f4f;">rocketMQ为ProducerGroup名</span></p> </td> <td style="width:150px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">Canal-Producer</span></p> </td> </tr> <tr> <td style="width:219px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">canal.mq.canalBatchSize</span></p> </td> <td style="width:395px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">获取canal数据的批次大小</span></p> </td> <td style="width:150px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">50</span></p> </td> </tr> <tr> <td style="width:219px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">canal.mq.canalGetTimeout</span></p> </td> <td style="width:395px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">获取canal数据的超时时间</span></p> </td> <td style="width:150px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">100</span></p> </td> </tr> <tr> <td style="width:219px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">canal.mq.flatMessage</span></p> </td> <td style="width:395px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">是否为json格式 </span><br><span style="color:#4f4f4f;">如果设置为false,对应MQ收到的消息为protobuf格式</span><br><span style="color:#4f4f4f;">需要通过CanalMessageDeserializer进行解码</span></p> </td> <td style="width:150px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">true</span></p> </td> </tr> <tr> <td style="width:219px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">---</span></p> </td> <td style="width:395px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">---</span></p> </td> <td style="width:150px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">---</span></p> </td> </tr> <tr> <td style="width:219px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">canal.mq.topic</span></p> </td> <td style="width:395px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">mq里的topic名</span></p> </td> <td style="width:150px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">无</span></p> </td> </tr> <tr> <td style="width:219px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">canal.mq.dynamicTopic</span></p> </td> <td style="width:395px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">mq里的动态topic规则, 1.1.3版本支持</span></p> </td> <td style="width:150px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">无</span></p> </td> </tr> <tr> <td style="width:219px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">canal.mq.partition</span></p> </td> <td style="width:395px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">单队列模式的分区下标,</span></p> </td> <td style="width:150px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">1</span></p> </td> </tr> <tr> <td style="width:219px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">canal.mq.partitionsNum</span></p> </td> <td style="width:395px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">散列模式的分区数</span></p> </td> <td style="width:150px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">无</span></p> </td> </tr> <tr> <td style="width:219px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">canal.mq.partitionHash</span></p> </td> <td style="width:395px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">散列规则定义 </span><br><span style="color:#4f4f4f;">库名.表名 : 唯一主键,比如mytest.person: id </span><br><span style="color:#4f4f4f;">1.1.3版本支持新语法,见下文</span></p> </td> <td style="width:150px;"> <p style="margin-left:0pt;"><span style="color:#4f4f4f;">无</span></p> </td> </tr> </tbody> </table> ## canal.mq.dynamicTopic 表达式说明 ## canal 1.1.3版本之后, 支持配置格式:schema.table,多个配置之间使用逗号分隔 > 例子1:test\\\\.test 指定匹配的单表,发送到以 test\\\\.test为名字的topic上 > 例子2:.\\\\..\* 匹配所有表,每个表都会发送到各自表名的topic上 > 例子3:test 指定匹配对应的库,一个库的所有表都会发送到库名的topic上 > 例子4:test\\\\.\* 指定匹配的表达式,针对匹配的表会发送到各自表名的topic上 > 例子5:test,test1\\\\.test1,指定多个表达式,会将test库的表都发送到test的topic上,test1\\\\.test1的表发送到对应的test1\\\\.test1 topic上,其余的表发送到默认的canal.mq.topic值 大家可以结合自己的业务需求,设置匹配规则,建议MQ开启自动创建topic的能力 ## canal.mq.partitionHash 表达式说明 ## canal 1.1.3版本之后, 支持配置格式:schema.table:pk1^pk2,多个配置之间使用逗号分隔 > 例子1:test\\\\.test:pk1^pk2 指定匹配的单表,对应的hash字段为pk1 + pk2 > 例子2:.\\\\..\*:id 正则匹配,指定所有正则匹配的表对应的hash字段为id > 例子3:.\\\\..\*:$pk$ 正则匹配,指定所有正则匹配的表对应的hash字段为表主键(自动查找) > 例子4: 匹配规则啥都不写,则默认发到0这个partition上 > 例子5:.\\\\..\* ,不指定pk信息的正则匹配,将所有正则匹配的表,对应的hash字段为表名 > 按表hash: 一张表的所有数据可以发到同一个分区,不同表之间会做散列 (会有热点表分区过大问题) > 例子6: test\\\\.test:id,.\\\\..\* , 针对test的表按照id散列,其余的表按照table散列 注意:大家可以结合自己的业务需求,设置匹配规则,多条匹配规则之间是按照顺序进行匹配(命中一条规则就返回) 其他详细参数可参考[Canal AdminGuide][] # mq顺序性问题 # binlog本身是有序的,写入到mq之后如何保障顺序是很多人会比较关注,在issue里也有非常多人咨询了类似的问题,这里做一个统一的解答 1. canal目前选择支持的kafka/rocketmq,本质上都是基于本地文件的方式来支持了分区级的顺序消息的能力,也就是binlog写入mq是可以有一些顺序性保障,这个取决于用户的一些参数选择 2. canal支持MQ数据的几种路由方式:单topic单分区,单topic多分区、多topic单分区、多topic多分区 > * canal.mq.dynamicTopic,主要控制是否是单topic还是多topic,针对命中条件的表可以发到表名对应的topic、库名对应的topic、默认topic name > * canal.mq.partitionsNum、canal.mq.partitionHash,主要控制是否多分区以及分区的partition的路由计算,针对命中条件的可以做到按表级做分区、pk级做分区等 canal的消费顺序性,主要取决于描述2中的路由选择,举例说明: > * 单topic单分区,可以严格保证和binlog一样的顺序性,缺点就是性能比较慢,单分区的性能写入大概在2~3k的TPS > * 多topic单分区,可以保证表级别的顺序性,一张表或者一个库的所有数据都写入到一个topic的单分区中,可以保证有序性,针对热点表也存在写入分区的性能问题 > * 单topic、多topic的多分区,如果用户选择的是指定table的方式,那和第二部分一样,保障的是表级别的顺序性(存在热点表写入分区的性能问题),如果用户选择的是指定pk hash的方式,那只能保障的是一个pk的多次binlog顺序性 \*\* pk hash的方式需要业务权衡,这里性能会最好,但如果业务上有pk变更或者对多pk数据有顺序性依赖,就会产生业务处理错乱的情况. 如果有pk变更,pk变更前和变更后的值会落在不同的分区里,业务消费就会有先后顺序的问题,需要注意 ## MQ数据消费 ## canal.client下有对应的MQ数据消费的样例工程,包含数据编解码的功能 > kafka模式: com.alibaba.otter.canal.client.running.kafka.CanalKafkaClientExample > rocketMQ模式: com.alibaba.otter.canal.client.running.rocketmq.CanalRocketMQClientExample [canal--canal_MQ_IT_-CSDN_canal mq]: https://knife.blog.csdn.net/article/details/105605995 [https_github.com_alibaba_canal_wiki_Canal-Kafka-RocketMQ-QuickStart]: https://github.com/alibaba/canal/wiki/Canal-Kafka-RocketMQ-QuickStart [canal_mysql_binlog_-CSDN]: https://blog.csdn.net/singgel/article/details/86166154 [release]: https://github.com/alibaba/canal/releases [Canal QuickStart]: https://github.com/alibaba/canal/wiki/QuickStart [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmdnZWw_size_16_color_FFFFFF_t_70]: /images/20230521/c50948e7b3f44d1d9a41e8a1bac03174.png [Canal AdminGuide]: https://github.com/alibaba/canal/wiki/AdminGuide
还没有评论,来说两句吧...