news 2026/3/26 19:52:29

【canal】canal同步msyql到redis

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【canal】canal同步msyql到redis

java层面 canal client配置pom文件

<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.13.2</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>33.5.0-jre</version> </dependency> <dependency> <groupId>com.alibaba.otter</groupId> <artifactId>canal.client</artifactId> <version>1.1.7</version> <!-- 使用最新版本 --> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- Redisson --> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.23.5</version> </dependency> <dependency> <groupId>com.alibaba.otter</groupId> <artifactId>canal.protocol</artifactId> <version>1.1.7</version> </dependency>

java client

1.将接收到的canal数据2进制转为10进制数据

2.redis client需要2进制转10进制,否则key是10进制,内部还是2进制

package org.example.cannal.redis; import com.alibaba.otter.canal.client.CanalConnectors; import com.alibaba.otter.canal.client.CanalConnector; import com.alibaba.otter.canal.protocol.Message; import com.alibaba.otter.canal.protocol.CanalEntry.*; import org.redisson.api.RMap; import org.redisson.api.RedissonClient; import org.redisson.config.Config; import java.net.InetSocketAddress; import java.util.HashMap; import java.util.List; import java.util.Map; public class CanalBinaryParseWithRedisson { public static void main(String[] args) { // 1. 创建 Canal 连接 CanalConnector connector = CanalConnectors.newSingleConnector( new InetSocketAddress("127.0.0.1", 11111), "example", "", "" ); // 2. 创建 Redisson 客户端 RedissonClient redisson = createRedissonClient(); try { connector.connect(); connector.subscribe(".*\\..*"); connector.rollback(); while (true) { Message message = connector.getWithoutAck(100); // 每次取100条 long batchId = message.getId(); int size = message.getEntries().size(); if (batchId == -1 || size == 0) { try { Thread.sleep(1000); } catch (InterruptedException ignored) {} } else { handleEntries(message.getEntries(), redisson); connector.ack(batchId); // 确认消费 } } } finally { connector.disconnect(); redisson.shutdown(); } } private static void handleEntries(List<Entry> entrys, RedissonClient redisson) { for (Entry entry : entrys) { if (entry.getEntryType() == EntryType.TRANSACTIONBEGIN || entry.getEntryType() == EntryType.TRANSACTIONEND) { continue; } RowChange rowChange; try { // 关键:把二进制解析成 RowChange rowChange = RowChange.parseFrom(entry.getStoreValue()); } catch (Exception e) { throw new RuntimeException("解析 RowChange 失败", e); } EventType eventType = rowChange.getEventType(); String tableName = entry.getHeader().getTableName(); for (RowData rowData : rowChange.getRowDatasList()) { if (eventType == EventType.INSERT || eventType == EventType.UPDATE) { Map<String, String> dataMap = new HashMap<>(); String id = null; for (Column column : rowData.getAfterColumnsList()) { dataMap.put(column.getName(), column.getValue()); if ("id".equals(column.getName())) { id = column.getValue(); } } // 用 Redisson 写入 Redis Hash RMap<String, String> map = redisson.getMap(tableName + ":" + id); map.putAll(dataMap); System.out.println("写入 Redis: " + tableName + ":" + id + " -> " + dataMap); } else if (eventType == EventType.DELETE) { String id = null; for (Column column : rowData.getBeforeColumnsList()) { if ("id".equals(column.getName())) { id = column.getValue(); break; } } // 删除 Redis Key redisson.getMap(tableName + ":" + id).delete(); System.out.println("删除 Redis: " + tableName + ":" + id); } } } } private static RedissonClient createRedissonClient() { Config config = new Config(); // 1. 配置Redis连接(单节点为例) config.useSingleServer() .setAddress("redis://127.0.0.1:6379") // .setPassword("你的密码") // 无密码则省略 .setDatabase(1); // 2. 关键:设置序列化器为StringCodec(明文字符串序列化) config.setCodec(new org.redisson.client.codec.StringCodec()); return org.redisson.Redisson.create(config); } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/24 18:54:51

如何快速解决Horos版本兼容性问题:macOS用户的完整指南

如何快速解决Horos版本兼容性问题&#xff1a;macOS用户的完整指南 【免费下载链接】horos Horos™ is a free, open source medical image viewer. The goal of the Horos Project is to develop a fully functional, 64-bit medical image viewer for OS X. Horos is based u…

作者头像 李华
网站建设 2026/3/25 7:30:55

WaveTools鸣潮工具箱完整评测:免费提升游戏性能的终极利器

WaveTools鸣潮工具箱完整评测&#xff1a;免费提升游戏性能的终极利器 【免费下载链接】WaveTools &#x1f9f0;鸣潮工具箱 项目地址: https://gitcode.com/gh_mirrors/wa/WaveTools 作为一名游戏性能优化工具深度体验者&#xff0c;经过数周的全面测试&#xff0c;我发…

作者头像 李华
网站建设 2026/3/4 11:59:20

QMC音频终极解密指南:快速解锁加密音乐文件

QMC音频终极解密指南&#xff1a;快速解锁加密音乐文件 【免费下载链接】qmc-decoder Fastest & best convert qmc 2 mp3 | flac tools 项目地址: https://gitcode.com/gh_mirrors/qm/qmc-decoder 还在为无法播放QMC加密音乐而烦恼吗&#xff1f;qmc-decoder项目为您…

作者头像 李华
网站建设 2026/3/25 10:43:15

微信好友管理轻松搞定:快速检测单向好友的实用指南

微信好友管理轻松搞定&#xff1a;快速检测单向好友的实用指南 【免费下载链接】WechatRealFriends 微信好友关系一键检测&#xff0c;基于微信ipad协议&#xff0c;看看有没有朋友偷偷删掉或者拉黑你 项目地址: https://gitcode.com/gh_mirrors/we/WechatRealFriends 你…

作者头像 李华
网站建设 2026/3/24 9:07:53

Reloaded-II项目更新后P3R启动失败终极排查指南

Reloaded-II项目更新后P3R启动失败终极排查指南 【免费下载链接】Reloaded-II Next Generation Universal .NET Core Powered Mod Loader compatible with anything X86, X64. 项目地址: https://gitcode.com/gh_mirrors/re/Reloaded-II 问题速览&#xff1a;启动异常全…

作者头像 李华
网站建设 2026/3/21 18:23:32

Qwen3-VL分析清华镜像站Anaconda包索引:Python环境搭建推荐

Qwen3-VL分析清华镜像站Anaconda包索引&#xff1a;Python环境搭建推荐 在高校实验室的某个深夜&#xff0c;一位研究生正皱着眉头盯着浏览器页面——屏幕上是密密麻麻的链接&#xff0c;成千上万个Python包名像代码雨般滚动而下。他想配置一个用于深度学习实验的Conda环境&am…

作者头像 李华