使用开源工具监听Binlog的演示
1.添加依赖
<!-- binlog 监听与解析: https://github.com/shyiko/mysql-binlog-connector-java -->
<dependency>
<groupId>com.github.shyiko</groupId>
<artifactId>mysql-binlog-connector-java</artifactId>
<version>0.13.0</version>
</dependency>
2.新建包service
3.编写BinlogServiceTest类
package com.imooc.ad.service;
import com.github.shyiko.mysql.binlog.BinaryLogClient;
import com.github.shyiko.mysql.binlog.event.DeleteRowsEventData;
import com.github.shyiko.mysql.binlog.event.EventData;
import com.github.shyiko.mysql.binlog.event.UpdateRowsEventData;
import com.github.shyiko.mysql.binlog.event.WriteRowsEventData;
import java.io.IOException;
public class BinlogServiceTest {
public static void main(String[] args) throws IOException {
//构造BinaryLogClient,填充mysql链接信息
BinaryLogClient client = new BinaryLogClient("127.0.0.1", 3306,
"root", "12345678"
);
//设置需要读取的Binlog的文件以及位置,否则,client会从"头"开始读取Binlog并监听
// client.setBinlogFilename("binlog.000035");
// client.setBinlogPosition();
//给客户端注册监听器,实现对Binlog的监听和解析
//event 就是监听到的Binlog变化信息,event包含header & data 两部分
client.registerEventListener(event -> {
EventData data = event.getData();
if (data instanceof UpdateRowsEventData) {
System.out.println("--------Update-----------");
System.out.println(data.toString());
} else if (data instanceof WriteRowsEventData) {
System.out.println("--------Insert-----------");
System.out.println(data.toString());
} else if (data instanceof DeleteRowsEventData) {
System.out.println("--------Delete-----------");
System.out.println(data.toString());
}
});
client.connect();
}
}
4.插入一行记录
