通过zk来保证集群中的canal实例只会有一个实例来工作,并将binlog解析进度保存在zk集群中,从而保证集群中某个实例出现故障时,及时切换为其他的实例,但是并不是实时保存而是延迟一秒保存,因此会出现binlog在主备切换时出现binlog重复解析的问题。
修改canal配置文件
修改2个配置
[root@master1 canal]# vi canal/conf/canal.properties
# 用逗号隔开且不留空格
canal.zkServers=10.105.10.123:2181,10.105.10.124:2181,10.105.10.125:2181
canal.instance.global.spring.xml = classpath:spring/default-instance.xml
拷贝canal到另一台机器并解压缩(作为从节点)
[root@master2 ~]# tar -zxvf canal.deployer-1.1.5.tar.gz -C /root/canal
修改slave配置文件
[root@master2 canal]# vi /root/canal/conf/example/instance.properties
## mysql serverId修改与master不同即可
canal.instance.mysql.slaveId = 1235
# position info修改成mysql地址即可
canal.instance.master.address = 10.105.10.121:3306
[root@master2 canal]# vi /root/canal/conf/canal.properties
# 用逗号隔开且不留空格
canal.zkServers=10.105.10.123:2181,10.105.10.124:2181,10.105.10.125:2181
canal.instance.global.spring.xml = classpath:spring/default-instance.xml
两台机器分别启动canal
[root@master2 canal]# sh /root/canal/bin/startup.sh
查看canal在zk中的状态
[zk: localhost:2181(CONNECTED) 11] get /otter/canal/destinations/example/running
{"active":true,"address":"10.105.10.121:11111","cid":1}
cZxid = 0x250000f037
ctime = Thu Apr 13 13:42:04 CST 2017
mZxid = 0x250000f037
mtime = Thu Apr 13 13:42:04 CST 2017
pZxid = 0x250000f037
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x15b5b11e9cd16e1
dataLength = 55
numChildren = 0
10.105.10.121为active,所以10.105.10.122位stangby节点
测试HA是否配置成功
stop掉10.105.10.121这台机器
[root@master1 canal]# sh /root/canal/bin/stop.sh
master1: stopping canal 6107 ...
Oook! cost:1
[zk: localhost:2181(CONNECTED) 28] get /otter/canal/destinations/example/running
{"active":true,"address":"10.105.10.122:11111","cid":1}
cZxid = 0x250000f0a7
ctime = Thu Apr 13 13:46:10 CST 2017
mZxid = 0x250000f0a7
mtime = Thu Apr 13 13:46:10 CST 2017
pZxid = 0x250000f0a7
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x35b5b11e9d816cb
dataLength = 55
客户端代码
CanalConnector connector = CanalConnectors.newClusterConnector("10.105.10.123:2181,10.105.10.124:2181,10.105.10.125:2181","example", "", "");