spring cloud集成canal
前提
win运行canal
加入canal依赖
<dependency>
<groupId>com.alibaba.otter</groupId>
<artifactId>canal.client</artifactId>
<version>1.1.3</version>
</dependency>
把ip、端口、监听表名做成配置文件

代码实现
package com.frame.modules.dabis.archives.thread; import com.alibaba.fastjson.JSONObject;
import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.client.CanalConnectors;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.alibaba.otter.canal.protocol.Message;
import com.frame.solr.em.SolrCode;
import com.frame.utils.PropertiesLoader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* @author liwei
* @date 2019/8/2 14:39
* @desc Created with IntelliJ IDEA.
*/
public class CanalThread implements Runnable { Log log = LogFactory.getLog(CanalThread.class); private String solrName = SolrCode.ARCHIVES.getValue(); @Override
public void run() {
PropertiesLoader loader = new PropertiesLoader("solrConfig.properties");
listener(loader.getProperty("canalHost"), loader.getProperty("canalPort"), loader.getProperty("canalTable"));
} public void listener(String canalHost, String canalPort, String table) {
// 创建链接
CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress(canalHost, Integer.valueOf(canalPort)), "example", "", "");
int batchSize = 1000;
try {
// 连接
connector.connect();
// 监听表
connector.subscribe(table);
connector.rollback();
// 一直循环监听
while (true) {
// 获取指定数量的数据
Message message = connector.getWithoutAck(batchSize);
long batchId = message.getId();
if(-1 != batchId && 0 != message.getEntries().size()) {
printEntry(message.getEntries());
}
// 提交确认
connector.ack(batchId);
}
} finally {
connector.disconnect();
}
} /**
* 打印具体变化
* @param entrys
*/
private void printEntry(List<CanalEntry.Entry> entrys) {
for (CanalEntry.Entry entry : entrys) {
if (CanalEntry.EntryType.TRANSACTIONBEGIN.equals(entry.getEntryType()) || CanalEntry.EntryType.TRANSACTIONEND.equals(entry.getEntryType())) {
continue;
} CanalEntry.RowChange rowChage = null;
try {
rowChage = CanalEntry.RowChange.parseFrom(entry.getStoreValue());
} catch (Exception e) {
throw new RuntimeException("ERROR ## parser of eromanga-event has an error , data:" + entry.toString(),
e);
} CanalEntry.EventType eventType = rowChage.getEventType();
System.out.println(String.format("================> binlog[%s:%s] , 数据库:%s,表名%s , 类型: %s",
entry.getHeader().getLogfileName(), entry.getHeader().getLogfileOffset(),
entry.getHeader().getSchemaName(), entry.getHeader().getTableName(),
eventType)); for (CanalEntry.RowData rowData : rowChage.getRowDatasList()) {
if (eventType == CanalEntry.EventType.DELETE) {
printColumn(rowData.getBeforeColumnsList());
} else if (eventType == CanalEntry.EventType.INSERT) {
printColumn(rowData.getAfterColumnsList());
} else {
System.out.println("-------修改之前");
printColumn(rowData.getBeforeColumnsList());
System.out.println("-------修改之后");
printColumn(rowData.getAfterColumnsList());
}
}
}
} private void printColumn(List<CanalEntry.Column> columns) {
Map<String,Object> aaMap = new HashMap<>();
for (CanalEntry.Column column : columns) {
aaMap.put(column.getName(), column.getValue());
}
System.out.println( new JSONObject(aaMap).toJSONString());
}
}
启动线程

新增


修改



删除



注意:拿到的值都是字符串,建议拿到id反查数据库,拿到对象再同步到自己的缓存。
spring cloud集成canal的更多相关文章
- Spring Cloud集成相关优质项目推荐
Spring Cloud Config 配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储.Git以及Subversion. Spring Cloud Bus 事件.消 ...
- 【分布式事务】spring cloud集成lcn解决分布式事务
参考地址:https://blog.csdn.net/u010882691/article/details/82256587 参考地址:https://blog.csdn.net/oyh1203/ar ...
- spring cloud 集成分布式配置中心 apollo(单机部署apollo)
一.什么是apollo? Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性,适用 ...
- Spring Cloud集成RabbitMQ的使用
同步 or 异步 前言:我们现在有一个用微服务架构模式开发的系统,系统里有一个商品服务和订单服务,且它们都是同步通信的. 目前我们商品服务和订单服务之间的通信方式是同步的,当业务扩大之后,如果还继续使 ...
- spring cloud 集成 swagger2 构建Restful APIS 说明文档
在Pom.xml文件中引用依赖 <dependencies> <dependency> <groupId>org.springframework.cloud< ...
- spring cloud集成 consul源码分析
1.简介 1.1 Consul is a tool for service discovery and configuration. Consul is distributed, highly ava ...
- 【spring cloud】spring cloud集成zipkin报错:Prometheus requires that all meters with the same name have the same set of tag keys.
spring boot 2.0.X 的版本,整合zipkin2.10.1 zipkin服务启动后,访问zipkin的UI http://localhost:8002/zipkin/ 页面显示空白,cs ...
- Spring Cloud集成EDAS(替代Eureka)
https://help.aliyun.com/document_detail/72618.html?spm=5176.7946893.821398.spring-cloud.603123beXemW ...
- Spring Cloud分布式微服务云架构集成项目
Spring Cloud集成项目有很多,下面我们列举一下和Spring Cloud相关的优秀项目,我们的企业架构中用到了很多的优秀项目,说白了,也是站在巨人的肩膀上去整合的.在学习Spring Clo ...
随机推荐
- HTML页面 js返回上一页
<input type="button" name="Submit" onclick="javascript:history.back(-1); ...
- CentOS7下安装Python3和PIP3
为了方便以后编译,所以整合了下配置流程 先查看是否安装了Python3 python3 -V 如果没有安装,则先安装依赖包 yum install zlib-devel bzip2-devel ope ...
- LeetCode 841. Keys and Rooms
原题链接在这里:https://leetcode.com/problems/keys-and-rooms/ 题目: There are N rooms and you start in room 0. ...
- Python 06 Geany的基本使用2
原文:https://www.cnblogs.com/denny402/p/5096001.html 功能:打开图片 1.下载库 pip install matplotlib 2.编写代码,将代码粘贴 ...
- 【JOISC2018|2019】【20190622】mergers
题目 一\(n\)个节点的树,节点被分成\(k\)个集合,\(i\)属于\(S_i\), 一条边是可划分的当且仅当左右两边的子树不存在相同集合的点 你一次可以合并两个集合,求最少的操作次数使得所有边都 ...
- 02-线性结构3 Reversing Linked List (25 分)
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- 原创:协同过滤之spark FP-Growth树应用示例
上一篇博客中,详细介绍了UserCF和ItemCF,ItemCF,就是通过用户的历史兴趣,把两个物品关联起来,这两个物品,可以有很高的相似度,也可以没有联系,比如经典的沃尔玛的啤酒尿布案例.通过Ite ...
- 海底高铁(洛谷 P3406)
题目背景 大东亚海底隧道连接着厦门.新北.博艾.那霸.鹿儿岛等城市,横穿东海,耗资1000亿博艾元,历时15年,于公元2058年建成.凭借该隧道,从厦门可以乘坐火车直达台湾.博艾和日本,全程只需要4个 ...
- D3.js的v5版本入门教程(第十三章)—— 饼状图
D3.js的v5版本入门教程(第十三章) 这一章我们来绘制一个简单的饼状图,我们只绘制构成饼状图基本的元素——扇形.文字,从这一章开始,内容可能有点难理解,因为每一章都会引入比较多的难理解知识点,在这 ...
- [Beta]第四次 Scrum Meeting
[Beta]第四次 Scrum Meeting 写在前面 会议时间 会议时长 会议地点 2019/5/12 22:00 30min 大运村公寓6F楼道 附Github仓库:WEDO 例会照片 工作情况 ...