上一篇已经介绍了canal实现 MySQL 数据库 binlog 日志解析,并且用官方提供的客户端程序成功读到了消息。但在生产环境下还不能这么用,更好的做法是将消息发送到消息队列,然后再从消息队列消费。
这里我选择的是 RabbitMQ
原来看官方文档发现只支持 Kafka
和 RocketMQ
,但好在最新版 1.1.5 也支持了 RabbitMQ
,而且镜像也已经打好了。
如果使用 Docker
部署的话,直接拉取最新的镜像即可。
# 1. 配置 canal
第一步拉取镜像:
docker pull canal/canal-server:latest
然后启动容器,从容器中拷贝出配置文件:
docker run -d -p 11111:11111 --name canal-server-v1.1.5 canal/canal-server:latest
docker cp canal-server:/home/admin/canal-server/conf/canal.properties ./
docker cp canal-server:/home/admin/canal-server/conf/test/instance.properties ./
修改 canal.properties 文件,配置输出到 RabbitMQ,有以下几处要改:
# 指定 RabbitMQ
canal.serverMode = rabbitMQ
# RabbitMQ 配置
##################################################
######### RabbitMQ #############
##################################################
rabbitmq.host = 127.0.0.1
rabbitmq.virtual.host = /
rabbitmq.exchange = core-business
rabbitmq.username = guest
rabbitmq.password = guest
rabbitmq.deliveryMode =
接下来修改 instance.properties 文件:
#################################################
## mysql serverId , v1.0.26+ will autoGen
# canal.instance.mysql.slaveId=0
# 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
# enable druid Decrypt database password
canal.instance.enableDruid=false
#canal.instance.pwdPublicKey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALK4BUxdDltRRE5/zXpVEVPUgunvscYFtEip3pmLlhrWpacX7y7GCMo2/JM6LeHmiiNdH1FWgGCpUfircSwlWKUCAwEAAQ==
# table regex
canal.instance.filter.regex=.*\\..*
# table black regex
canal.instance.filter.black.regex=mysql\\.slave_.*
# table field filter(format: schema1.tableName1:field1/field2,schema2.tableName2:field1/field2)
#canal.instance.filter.field=test1.t_product:id/subject/keywords,test2.t_company:id/name/contact/ch
# table field black filter(format: schema1.tableName1:field1/field2,schema2.tableName2:field1/field2)
#canal.instance.filter.black.field=test1.t_product:subject/product_image,test2.t_company:id/name/contact/ch
# mq config
canal.mq.topic=example
# dynamic topic route by schema or table regex
#canal.mq.dynamicTopic=mytest1.user,mytest2\\..*,.*\\..*
canal.mq.partition=0
# hash partition config
#canal.mq.partitionsNum=3
#canal.mq.partitionHash=test.table:id^name,.*\\..*
#canal.mq.dynamicTopicPartitionNum=test.*:4,mycanal:6
#################################################
主要修改如下:
# MySQL 地址 + 端口
canal.instance.master.address=host:port
canal.instance.dbUsername=xxxx
canal.instance.dbPassword=xxxx
# 对应到 RabbitMQ 的话是 Routing key
canal.mq.topic=canal-routing-key
# 2. 验证
{
"data":[
{
"id":"4",
"role_name":"3333333"
}
],
"database":"sql_106_15_195_7",
"es":1627113528000,
"id":1,
"isDdl":false,
"mysqlType":{
"id":"int(10) unsigned",
"role_name":"varchar(255)"
},
"old":[
{
"role_name":"测试0004"
}
],
"pkNames":[
"id"
],
"sql":"",
"sqlType":{
"id":4,
"role_name":12
},
"table":"role",
"ts":1627113621820,
"type":"UPDATE"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
"data":[
{
"id":"23",
"role_name":"测试0005"
}
],
"database":"sql_106_15_195_7",
"es":1627114953000,
"id":3,
"isDdl":false,
"mysqlType":{
"id":"int(10) unsigned",
"role_name":"varchar(255)"
},
"old":null,
"pkNames":[
"id"
],
"sql":"",
"sqlType":{
"id":4,
"role_name":12
},
"table":"role",
"ts":1627114953097,
"type":"INSERT"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28