实现监听Mysql-binlog
Java技术精选
共 837字,需浏览 2分钟
·
2021-07-31 01:25
引入依赖:
<dependency>
<groupId>com.github.shyiko</groupId>
<artifactId>mysql-binlog-connector-java</artifactId>
<version>0.21.0</version>
</dependency>
配置连接:
BinaryLogClient logClient = new BinaryLogClient( "127.0.0.1", 3306, "root", "123456" );
注册监听:
logClient.registerEventListener(event -> {
EventData data = event.getData();
//如果日志是更新记录
if(data instanceof UpdateRowsEventData){
System.out.println("update");
}else if(data instanceof WriteRowsEventData){
//写操作
System.out.println("write");
}else if(data instanceof DeleteRowsEventData){
//删除操作
System.out.println("delete");
}
});
开始监听:
logClient.connect();
评论