1 Star 13 Fork 0

Stan / springboot-canal-sync

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

写在前面

东西是从一个依赖canal同步mysql到es的服务里拿出来的
用的时候canal刚推出同步es的方案,但是那个sql写复杂了就出问题(我的问题)
那就直接监听binlog看表数据变动就好了,如果相关文档(数据)依赖的原始数据变动,就根据key更新这个文档(数据)为最新数据

使用

启动

@EnableSyncListener
public class XXApplication {  }

Trigger类中方法

  • 当book_info发生更新时,触发此方法,默认传入参数为变更的记录中id字段值
@Service
public class BookInfoIndexSyncTrigger {
    @SyncListener(table = {"book_info"}, type = EventType.UPDATE)
    public void updateBookInfo(long bookId) {
        logger.info("更新同步book_info..." + bookId);
        syncService.syncBookInfoIndexById(bookId);
    }
}
  • 当book_isbn_info发生增、删、改时,触发此方法,传入参数为变更表中book_id字段值
@Service
public class BookBaseInfoIndexSyncTrigger {
    @SyncListener(table = {"book_isbn_info"}, type = EventType.ALL, key = {"book_id"})
    public void updateBookInfoFromIsbn(long bookId) {
        logger.info("ALL 非主表批量同步book_isbn_info..." + bookId);
        syncService.syncBookInfoIndexById(bookId);
    }
}
  • 若需要获取更多变更参数

该功能依赖于执行前将RowData加入了上下文,但是不一定转换成Map放入上下文
若为DELETE监听器,则加入变更前数据到Map(该Map的key为字段名,value为字段值),则getCurrentDataAfter方法返回值为空
若为INSERT\UPDATE监听器,则加入变更后文档到Map,则getCurrentDataBefore方法返回值为空

  1. 以下几个方法根据当前监听CRUD的类型可知是否为null
- 获取当前默认加载文档类型(true则为变更前文档false则变更后文档)
	SyncContext.getCurrentIsDataBefore();
- 直接获取RowData
    SyncContext.getCurrentRowData();
- 获取文档变更前记录Map(若未加载则返回值为空)
    SyncContext.getCurrentDataBefore();
- 获取文档变更后记录Map(若未加载则返回值为空)
    SyncContext.getCurrentDataAfter();
- 获取变更前字段值(若未加载则返回值为空)
    SyncContext.getBeforeParam(field, clazz)
- 获取变更后字段值(若未加载则返回值为空)
    SyncContext.getAfterParam(field, clazz)
- 根据默认加载文档类型获取参数
    SyncContext.getParam(field, clazz)
  1. 以下方法不会为null,如果是未加载过的数据会加载后取出返回

    • 获取当前监听器 SyncContext.getCurrentListener()
    • 从上下文中获取监听器获取默认载入数据的字段值(如果值为null会加载另一部分数据再取出返回) SyncContext.getCurrentListener().getParam("id", Long.class);
    • 从上下文中获取监听器后获取变更前字段值(如是未转换加载的数据,会进行加载后返回该数据) SyncContext.getCurrentListener().getBeforeParam("id", Long.class);
    • 从上下文中获取监听器后获取变更后参数(如是未转换加载的数据,会进行加载后返回该数据) SyncContext.getCurrentListener().getAfterParam("id", Long.class);

配置

  • 是否连接canal,开启数据同步,默认true(因为canal客户端jar指定了clientId,所以连到同一集群的服务只有一个能fetch消息,但是有些是非主要的或者不用来做更新的机器,特别像扩容提高当前服务接口并发量时候,不需要主备扩容同步服务,可以关掉canal客户端)
canal:
  sync: false
  • canal集群配置
canal:
  # 是否使用dubbo的zookeeper集群配置,如果为true则下面的host,port节点配置将失效,会读取dubbo.registry.address信息,默认fasle
  enable: true
  host: 127.0.0.1
  port: 11111
  destination: example
  username:
  password:

相关

  • 开启数据库binlog
[mysqld]
log-bin=mysql-bin # 开启 binlog
binlog-format=ROW # 选择 ROW 模式
server_id=1 # 配置 MySQL replaction 需要定义,不要和 canal 的 slaveId 重复
    canal.instance.filter.regex=test_lib\\..*
MIT License Copyright (c) 2020 Stan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于springboot整合canal的快速开发库,同步数据到elasticsearch、redis、mongo、MQ,支持集群部署 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/sense7/spring-canal-sync.git
git@gitee.com:sense7/spring-canal-sync.git
sense7
spring-canal-sync
springboot-canal-sync
master

搜索帮助