Binlog4j 1.2.0 发布,Java 轻量级 Binary Log 客户端
Binlog4j
轻量级 Mysql Binlog 客户端, 提供宕机续读, 高可用集群等特性
项目特性
-
集群模式, 通过集群部署的方式,保证服务高可用。
-
宕机续读, 避免宕机期间造成数据丢失。
-
数据转换, 基于泛型封装 binlog Event 的序列化数据。
-
兼容 传统项目 与 Spring Boot / Cloud 项目。
-
兼容 Spring Boot 2.x 与 Spring Boot 3.x 版本。项目特性
更新日志(1.2.0)
[新增] BinlogClient 类 connect 方法日志打印。
[新增] Binlog4jException.class 代替 RuntimeException.class 异常处理。
[新增] binlog4j-spring-boot-starter 包 spring.binlog4j.enabled 配置, 默认为 true。
[新增] GsonUtils 工具类到 utils 包 , 提供 Gson 方法。
[升级] spring-boot 到 2.7.14 版本。
[升级] redisson 到 3.23.3 版本。
[升级] gson 到 2.10.1 版本。
[升级] jedis 到 4.4.3 版本
下载安装
<dependency> <groupId>com.gitee.Jmysy</groupId> <artifactId>binlog4j-core</artifactId> <version>latest.version</version> </dependency>
或
implementation group: 'com.gitee.Jmysy', name: 'binlog4j-core', version: 'latest.version'
简单使用
通过 BinlogClient 创建 binlog 客户端, IBinlogEventHandler 用于接受 binlog 事件通知, 该接口允许使用泛型, 数据将遵循驼峰规则进行封装。
public class BootStrap { public static void main(String[] args) { BinlogClientConfig clientConfig = new BinlogClientConfig(); clientConfig.setHost("127.0.0.1"); clientConfig.setPort(3306); clientConfig.setUsername("root"); clientConfig.setPassword("taoren@123"); clientConfig.setServerId(1990); IBinlogClient binlogClient = new BinlogClient(clientConfig); binlogClient.registerEventHandler("database", "table", new IBinlogEventHandler() { @Override public void onInsert(BinlogEvent event) { System.out.println("插入数据:{}", event.getData()); } @Override public void onUpdate(BinlogEvent event) { System.out.println("修改数据:{}", event.getData()); } @Override public void onDelete(BinlogEvent event) { System.out.println("删除数据:{}", event.getData()); } }); binlogClient.connect(); } }
高级特性
通过 Persistence 配置为 true 启用宕机续读功能, Binlog4j 会将 binlog 的 filename 与 position 记录到 redis, 所以同时你需要设置 Redis 配置。
public class BootStrap { public static void main(String[] args) { RedisConfig redisConfig = new RedisConfig(); redisConfig.setHost("127.0.0.1"); redisConfig.setPort(6379); redisConfig.setPassword("taoren@123"); BinlogClientConfig clientConfig = new BinlogClientConfig(); clientConfig.setHost("127.0.0.1"); clientConfig.setPort(3306); clientConfig.setUsername("root"); clientConfig.setPassword("taoren@123"); clientConfig.setServerId(1990); // Client 编号 clientConfig.setRedisConfig(redisConfig); // Redis 配置 clientConfig.setPersistence(true); // 启用持久化 (宕机重启后, 从上次读取的位置开始) clientConfig.setMode(BinlogClientMode.cluster); // 高可用集群 BinlogClient binlogClient = new BinlogClient(clientConfig); binlogClient.registerEventHandler("database", "table", new IBinlogEventHandler<User>() { @Override public void onInsert(BinlogEvent<User> event) { System.out.println("插入数据:{}", event.getData()); } @Override public void onUpdate(BinlogEvent<User> event) { System.out.println("修改数据:{}", event.getData()); } @Override public void onDelete(BinlogEvent<User> event) { System.out.println("删除数据:{}", event.getData()); } }); binlogClient.connect(); } }
Spring Boot Starter
<dependency> <groupId>com.gitee.Jmysy</groupId> <artifactId>binlog4j-spring-boot-starter</artifactId> <version>latest.version</version> </dependency>
或
implementation group: 'com.gitee.Jmysy', name: 'binlog4j-spring-boot-starter', version: 'latest.version'
首先, 在 application.yml 中填写 BinlogClient 配置
spring: binlog4j: redis-config: host: 127.0.0.1 port: 6379 password: taoren@123 client-configs: master: username: root password: taoren@123 host: 127.0.0.1 port: 3306 serverId: 1990
单表监听
使用 @BinlogSubscriber 注解, 指定 IBinlogEventHandler 需要注册到哪个客户端, 并且指定监听的 database 与 table。
@BinlogSubscriber(clientName = "master", database = "pear-admin", table ="sys_user") public class UserEventHandler implements IBinlogEventHandler<User> { @Override public void onInsert(BinlogEvent<User> event) { System.out.println("插入数据:" + event.getData()); } @Override public void onUpdate(BinlogEvent<User> event) { System.out.println("修改数据:" + event.getData()); } @Override public void onDelete(BinlogEvent<User> event) { System.out.println("删除数据:" + event.getData()); } }
多表监听
@BinlogSubscriber 注解 database 与 table 属性支持 Pattern 匹配, IBinlogEventHandler 在不指定泛型的情况下, event.getData() 为 Map<String, Object> 类型, 用于兼容不同表的数据结构。
@BinlogSubscriber(clientName = "master", database = ".*", table ="sys.*") public class UserEventHandler implements IBinlogEventHandler { @Override public void onInsert(BinlogEvent event) { System.out.println("数据库:" + event.getDatabase()); System.out.println("数据表:" + event.getTable()); System.out.println("新数据:" + event.getData()); } @Override public void onUpdate(BinlogEvent event) { System.out.println("数据库:" + event.getDatabase()); System.out.println("数据表:" + event.getTable()); System.out.println("原数据:" + event.getOriginalData()); System.out.println("新数据:" + event.getData()); } @Override public void onDelete(BinlogEvent event) { System.out.println("数据库:" + event.getDatabase()); System.out.println("数据表:" + event.getTable()); System.out.println("新数据:" + event.getData()); } }
监听 master 数据源所有数据库中所有表的数据变化【最灵活】
@BinlogSubscriber(clientName = "master")
监听 master 数据源 pear-admin 数据库中所有表的数据变化
@BinlogSubscriber(clientName = "master", database="pear-admin")
监听 master 数据源 pear-admin 数据库中 user 表的数据变化
@BinlogSubscriber(clientName = "master", database="pear-admin", table="user")
监听 master 数据源所有数据库中 user 表的数据变化
@BinlogSubscriber(clientName = "master", table="user")
监听 master 数据源所有数据库中以 user 开头的表的数据变化
@BinlogSubscriber(clientName = "master", table="user.*")
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
不容错过 | IvorySQL外部贡献者计划开启,寻找开源锦鲤!
在广袤的开源世界中,众多优秀的开发者默默耕耘,为技术进步贡献着自己的力量。然而,有时候机会就像一条巨大的锦鲤,突然跃出水面,让你成为众人瞩目的焦点。这一次,你的机会来了,你就是IvorySQL要找的锦鲤!为IvorySQL代码做贡献,赢取「“象牙币”,换取等价奖金!」 你的每一行代码、每一个改进都将为IvorySQL增添新的活力和可能,享受这个过程,相信自己的能力,你将成为IvorySQL重要核心开发成员。无论你是有经验的开发者还是刚刚入门的新手,我们都欢迎您的加入! PART.01 参与步骤 1、登录GitHub,进入IvorySQL的项目主页 https://github.com/IvorySQL 2、选择/创建您想要贡献的Issue 点击页面中的“Projects”按钮,然后点击“IvorySQL Developer Event 2023”进入本次开发者活动的Issue列表,选择/创建您想要贡献的Issue。 3、提交代码,完成合并 将对应项目的代码Fork至您的个人 GitHub 账户,创建一个新的Branch进行代码修改,本地测试完成后提交PR,等待维护者Review,...
- 下一篇
Dante Cloud 3.1.3.0 发布,采用领域驱动设计 (DDD) 的微服务架构
Dante Cloud 是一款企业级微服务架构和服务能力开发平台,是采用领域驱动模型(DDD)设计思想的、全面拥抱 Spring Authorization Server 的、基于 OAuth2.1 协议的、支持智能电视、IoT等物联网设备认证的微服务架构。基于 Spring Authorization Server 1.1.2、Spring Boot 3.1.3、Spring Cloud 2022.0.4、Spring Cloud Tencent 1.11.9-2022.0.1、Spring Cloud Alibaba 2022.0.0.0、Nacos 2.2.4 等主流技术栈开发的多租户系统,遵循 SpringBoot 编程思想,高度模块化和可配置化。具备服务发现、配置、熔断、限流、降级、监控、多级缓存、分布式事务、工作流等功能 平台定位 - 构建成熟的、完善的、全面的,基于 OAuth2.1 的、前后端分离的微服务架构解决方案。 - 面向企业级应用和互联网应用设计开发,既兼顾传统项目的微服务化,又满足互联网应用开发建设、快速迭代的使用需求。 - 平台架构使用微服务领域及周边相关的...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Linux系统CentOS6、CentOS7手动修改IP地址
- 2048小游戏-低调大师作品
- CentOS8安装Docker,最新的服务器搭配容器使用
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Windows10,CentOS7,CentOS8安装Nodejs环境
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS7,8上快速安装Gitea,搭建Git服务器
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16