下载地址:https://github.com/alibaba/canal/releases
下载后解压,然后去到conf/example里面配置instance.properties,其他的先不用动,主要是为了体验一下,所以简单的修改一下数据库账号密码即可
我在window电脑下使用的,进入bin目录,双击start.bat 如果启动报错如下(如果你是linux的话,记得要开端口)
那么修改启动文件,这一段删掉即可
由于canal是模拟mysql的主从复制,所以我们去修改 一下mysql的配置文件,window电脑的为my.ini,修改完之后记得重启mysql,不然不生效。如果是docker安装,该文件在容器的 /etc/mysql/mysql.conf.d 目录下
log-bin=mysql-bin
binlog-format=ROW
server_id=1
设置之前
修改之后
然后我们来看一下示例代码:https://github.com/alibaba/canal/wiki/ClientExample
拷贝代码到我们项目中,其他的不用动,把数据库账号密码修改一下即可,由于我们是springboot项目,所以我们可以稍加改动一下,首先定义一下连接配置类
import java.net.InetSocketAddress;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.client.CanalConnectors;
/**
* canal链接配置类
* @author ljw
*
*/
@Configuration
public class CanalConfig {
@Bean
public CanalConnector initConnector() {
CanalConnector connector =
CanalConnectors.newSingleConnector(new InetSocketAddress("127.0.0.1",11111), "example", "root", "root");
connector.connect();
connector.subscribe(".*\\..*");//订阅所有数据库所有表
connector.rollback();
return connector;
}
}
使用类
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import com.alibaba.otter.canal.client.CanalConnector;
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;
@Component
public class CanalListener implements ApplicationRunner {
@Autowired
CanalConnector canalConnector;
@Override
public void run(ApplicationArguments args) throws Exception {
int batchSize = 1000;
try {
while (true) {
Message message = canalConnector.getWithoutAck(batchSize); // 获取指定数量的数据
long batchId = message.getId();
int size = message.getEntries().size();
if (batchId == -1 || size == 0) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
} else {
printEntry(message.getEntries());
}
canalConnector.ack(batchId); // 提交确认
}
} finally {
canalConnector.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) {
Map<String,Object> map = new HashMap<String, Object>();
for (Column column : columns) {
System.out.println(column.getName() + " : " + column.getValue() + " update=" + column.getUpdated());
map.put(column.getName(), column.getValue());
}
//System.err.println(map);
}
}
查看演示效果,我在数据库改动了一条数据,控制台输出如下
<dependency>
<groupId>com.alibaba.otter</groupId>
<artifactId>canal.client</artifactId>
<version>1.1.4</version>
</dependency>