canal.deployer-1.1.5.tar.gz canal.admin-1.1.5.tar.gz
1.修改配置文件 vim /etc/my.cnf
log-bin=mysql-bin
binlog-format=ROW
server_id=1
2.重启mysql
3.修改配置文件
vim /opt/canal/conf/example/instance.properties
canal.instance.mysql.slaveId=1234
canal.instance.master.address=hdp1:3306
canal.instance.dbUsername=root
canal.instance.dbPassword=root
canal.instance.connectionCharset = UTF-8
canal.instance.filter.regex=.*\\..*
4.启动
/opt/canal/bin/startup.sh
查看日志
tail -fn 300 /opt/canal/logs/canal/canal.log
显示 running now 则表示运行成功
tail -fn 300 /opt/canal/logs/example/example.log
显示start successful… 表示没问题
start successful....
ClientExample 导入pom依赖
<dependency>
<groupId>com.alibaba.otter</groupId>
<artifactId>canal.client</artifactId>
<version>1.1.0</version>
</dependency>
代码如下:
import java.net.InetSocketAddress;
import java.util.List;
import com.alibaba.otter.canal.client.CanalConnectors;
import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.common.utils.AddressUtils;
import com.alibaba.otter.canal.protocol.Message;
import com.alibaba.otter.canal.protocol.CanalEntry.Column;
import com.alibaba.otter.canal.protocol.CanalEntry.Entry;
import com.alibaba.otter.canal.protocol.CanalEntry.EntryType;
import com.alibaba.otter.canal.protocol.CanalEntry.EventType;
import com.alibaba.otter.canal.protocol.CanalEntry.RowChange;
import com.alibaba.otter.canal.protocol.CanalEntry.RowData;
public class SimpleCanalClientExample {
public static void main(String args[]) {
CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress("hdp1",
11111), "example", "", "");
int batchSize = 1000;
int emptyCount = 0;
try {
connector.connect();
connector.subscribe(".*\\..*");
connector.rollback();
int totalEmptyCount = 120;
while (emptyCount < totalEmptyCount) {
Message message = connector.getWithoutAck(batchSize);
long batchId = message.getId();
int size = message.getEntries().size();
if (batchId == -1 || size == 0) {
emptyCount++;
System.out.println("empty count : " + emptyCount);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
} else {
emptyCount = 0;
printEntry(message.getEntries());
}
connector.ack(batchId);
}
System.out.println("empty too many times, exit");
} finally {
connector.disconnect();
}
}
private static void printEntry(List<Entry> entrys) {
for (Entry entry : entrys) {
if (entry.getEntryType() == EntryType.TRANSACTIONBEGIN || entry.getEntryType() == EntryType.TRANSACTIONEND) {
continue;
}
RowChange rowChage = null;
try {
rowChage = RowChange.parseFrom(entry.getStoreValue());
} catch (Exception e) {
throw new RuntimeException("ERROR ## parser of eromanga-event has an error , data:" + entry.toString(),
e);
}
EventType eventType = rowChage.getEventType();
System.out.println(String.format("================> binlog[%s:%s] , name[%s,%s] , eventType : %s",
entry.getHeader().getLogfileName(), entry.getHeader().getLogfileOffset(),
entry.getHeader().getSchemaName(), entry.getHeader().getTableName(),
eventType));
for (RowData rowData : rowChage.getRowDatasList()) {
if (eventType == EventType.DELETE) {
printColumn(rowData.getBeforeColumnsList());
} else if (eventType == EventType.INSERT) {
printColumn(rowData.getAfterColumnsList());
} else {
System.out.println("-------> before");
printColumn(rowData.getBeforeColumnsList());
System.out.println("-------> after");
printColumn(rowData.getAfterColumnsList());
}
}
}
}
private static void printColumn(List<Column> columns) {
for (Column column : columns) {
System.out.println(column.getName() + " : " + column.getValue() + " update=" + column.getUpdated());
}
}
}
运行Client,首先启动Canal Server上述步骤4 启动Canal Client后,可以从控制台从看到类似消息:
empty count : 1
empty count : 2
empty count : 3
empty count : 4
此时代表当前数据库无变更数据 触发数据库变更 可以从控制台中看到:
================> binlog[mysql-bin.000004:168242] , name[presto,user_info] , eventType : UPDATE
-------> before
id : 4 update=false
nickname : job update=false
address : 上海 update=false
-------> after
id : 4 update=false
nickname : job2 update=true
address : 上海 update=false
Canal Kafka RocketMQ QuickStart
1.关闭服务
/opt/canal/bin/stop.sh
2.修改配置文件
vim /opt/canal/conf/example/instance.properties
canal.mq.partitionsNum=3
vim /opt/canal/conf/canal.properties
canal.serverMode = kafka
kafka.bootstrap.servers = hdp1:9092,hdp2:9092,hdp3:9092
创建kafka主题
./kafka-topic.sh example 3 2
启动canal
/opt/canal/bin/startup.sh
进入mysql进行修改数据
启动kafka消费者
./kafka-consumer.sh example
监听到数据:
{"data":[{"id":"4","nickname":"job3","address":"上海"}],"database":"presto","es":1629983070000,"id":2,"isDdl":false,"mysqlType":{"id":"int(11)","nickname":"varchar(255)","address":"varchar(255)"},"old":[{"nickname":"job2"}],"pkNames":["id"],"sql":"","sqlType":{"id":4,"nickname":12,"address":12},"table":"user_info","ts":1629983070348,"type":"UPDATE"}
Canal HA
修改配置文件
vim /opt/canal/conf/canal.properties
canal.zkServers = hdp1:2181,hdp2:2181,hdp3:2181
canal.instance.global.spring.xml = classpath:spring/default-instance.xml
将canal传到hdp2
xsync ./canal/ hdp 2 2
修改配置文件
vim /opt/canal/conf/example/instance.properties
canal.instance.mysql.slaveId=1235
启动hdp1 hdp2的canal
查看zookeeper
/opt/zookeeper-3.4.6/bin/zkCli.sh
[zk: localhost:2181(CONNECTED) 10] get /otter/canal/destinations/example/running
{"active":true,"address":"192.168.56.101:11111"}
cZxid = 0x1100000071
ctime = Thu Aug 26 21:19:09 CST 2021
mZxid = 0x1100000071
mtime = Thu Aug 26 21:19:09 CST 2021
pZxid = 0x1100000071
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x27b81de17bb0000
dataLength = 48
numChildren = 0
Canal Admin
修改配置文件
vim /opt/canal-admin/conf/application.yml
spring.datasource:
address: hdp1:3306
database: canal_manager
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
初始化脚本:
/opt/canal-admin/conf/canal_manager.sql
mysql> source canal_manager.sql;
mysql> use canal_manager;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-------------------------+
| Tables_in_canal_manager |
+-------------------------+
| canal_adapter_config |
| canal_cluster |
| canal_config |
| canal_instance_config |
| canal_node_server |
| canal_user |
+-------------------------+
6 rows in set (0.00 sec)
开启admin
/opt/canal-admin/bin/startup.sh
端口为:
1861 CanalAdminApplication
页面为:http://hdp1:8089/ admin 123456
1.新建集群 2.修改配置文件
vim /opt/canal/conf/canal_local.properties
canal.admin.manager = hdp1:8089
canal.admin.register.cluster = online
canal.admin.register.name = server1
此处的user passwd 对应
canal.admin.user = admin
canal.admin.passwd = 4ACFE3202A5FF5CF467898FC58AAB1D615029441
/opt/canal-admin/conf/application.yml
canal:
adminUser: admin
adminPasswd: admin
页面登陆密码为mysql中如图所示: 3.启动
/opt/canal/bin/startup.sh local
4.修改hdp2配置文件
vim /opt/canal/conf/canal_local.properties
canal.admin.manager = hdp1:8089
canal.admin.register.cluster = online
canal.admin.register.name = server2
5.启动hdp2
/opt/canal/bin/startup.sh local
保存即可
修改presto下表 打开kafka消费端
{"data":[{"id":"4","nickname":"job4","address":"上海"}],"database":"presto","es":1629989877000,"id":2,"isDdl":false,"mysqlType":{"id":"int(11)","nickname":"varchar(255)","address":"varchar(255)"},"old":[{"nickname":"job3"}],"pkNames":["id"],"sql":"","sqlType":{"id":4,"nickname":12,"address":12},"table":"user_info","ts":1629989877647,"type":"UPDATE"}
可以建多个instance进行监听
|