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 ...
随机推荐
- linux中如何升级Python
一.使用wget 下载Python 安装包 我是在虚拟中当中安装的: wget http://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz 报错: ...
- vue饿了么UI库-笔记
1. :rules="{required: true, message: '有效期不能为空'}" :rules="{type:'date',required: true, ...
- GSM/GPRS模块 AT指令集C语言编程——基于有方M660+和MSP430单片机
GSM/GPRS芯片是手机中负责收发短信.拨打电话以及访问GPRS网络的核心器件.有方M660+为深圳有方公司生产的一款超小封装的GSM/GPRS工业无线模块,可以提供高品质的语音.短信.数据业务等功 ...
- 【NOIP2017】逛公园 D1 T3
记忆化搜索 跑一次反向的最短路求出MinDis(u,n)MinDis(u,n)MinDis(u,n) f[u][k]f[u][k]f[u][k]表示dis(u,n)<=MinDis(u,n)+d ...
- 使用 DML 自定义调试器输出
调试器标记语言 (DML) 提供了一种机制增强来自调试器和扩展的输出. 与 HTML 类似,调试器的标记支持允许将输出包括显示指令和额外非显示的标记窗体中的信息. 调试器用户界面,WinDbg 等中分 ...
- 移动端touch触摸事件(滑动效果和手势操作)
一.定义 ①touch是移动端的触摸事件,而且是一组事件,主要有以下事件: touchstart 事件:当手指触摸屏幕的时候触发 touchmove 事件:当手指在屏幕来回滑动的时候触发 touche ...
- windowns server 2008 r2 AD桌面文件重定向设置
1.创建将要进行重定向的组(此处为chongdingxiangzu) 2.选择要重定向的用户,并将此用户加入到要重定向的组里 3.打开组策略管理,右击刚才用户所属的组织单位(OU)进行新建GPO(此处 ...
- 洛谷 P4568 [JLOI2011]飞行路线 题解
P4568 [JLOI2011]飞行路线 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在\(n\)个城市设有业务,设这些城市分别标记为\(0\)到\( ...
- 【洛谷P3369】普通平衡树——Splay学习笔记(一)
二叉搜索树(二叉排序树) 概念:一棵树,若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉搜索树 ...
- C博客作业00——我的第一篇博客
1.你对网络专业或计算机专业了解是怎样? 初看字眼,便觉得是理工性很强的专业,所以需要较强的开拓思维,创新精神,探索未知事物的勇气,才能掌握并且熟练应用相关知识.计算机类专业都需要学习计算机语言,而计 ...