官方文档:https://github.com/alibaba/canal
使用示例:https://github.com/alibaba/canal/wiki/ClientExample
安装示例:https://github.com/alibaba/canal/wiki/QuickStart
可能会有坑的地方,cancl日志会报错canal用户没权限,根据报错日志执行对应sql可以解决问题。

代码使用
<!-- canal java -->
<dependency>

<groupId>top.javatool</groupId>
<artifactId>canal-client</artifactId>
<version>1.2.1-RELEASE</version>

</dependency>
<!-- canal springboot -->
<dependency>

<groupId>top.javatool</groupId>
<artifactId>canal-spring-boot-starter</artifactId>
<version>1.2.1-RELEASE</version>

</dependency>

canal:
server: 192.168.84.22:11111 # 本机IP地址,这个也需要在canal配置文件配下
destination: example
官网有java的使用示例,下面是基于springboot
//@Slf4j
//@Component
//@CanalTable("user_info")
//public class UserCanal implements EntryHandler<UserInfo> {
//
// @Override
// public void insert(UserInfo o) {
// log.info("insert:{}", JSON.toJSONString(o));
//
// }
// @Override
// public void update(UserInfo before, UserInfo after) {
// log.info("updateBefore:{}", JSON.toJSONString(before));
// log.info("updateAfter:{}", JSON.toJSONString(after));
//
// }
// @Override
// public void delete(UserInfo o) {
// log.info("delete:{}", JSON.toJSONString(o));
//
// }
//}
@CanalTable(value = "all")
@Component
@Slf4j
class DefaultEntryHandler implements EntryHandler<Map<String, String>> {

@Override
public void insert(Map<String, String> map) {
    CanalModel canalModel = CanalContext.getModel();
    log.info("canalModel:{}", canalModel);
    log.info("insert message  {}", map);
}

@Override
public void update(Map<String, String> before, Map<String, String> after) {
    CanalModel canalModel = CanalContext.getModel();
    log.info("canalModel:{}", canalModel);
    log.info("update before {} ", before);
    log.info("update after {}", after);
}

@Override
public void delete(Map<String, String> map) {
    CanalModel canalModel = CanalContext.getModel();
    log.info("canalModel:{}", canalModel);
    log.info("delete  {}", map);
}

}

参考:
https://github.com/NormanGyllenhaal/canal-client
https://segmentfault.com/a/1190000022773865
https://www.cnblogs.com/shaozhiqi/p/11534658.html

最后修改:2020 年 10 月 09 日 05 : 10 PM