实现监听Mysql-binlog
引入依赖:
<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();
评论