当前位置: 首页 > news >正文

监听MySQL binlog

文章目录

    • 背景
    • 快速入门
    • 监听普通文件内容变动

背景

1、内容

通过监听mysql的主库的写、删、更新操作产生的binlog日志,来做业务(eg:监控数据、同步数据等)

2、作用

  • 无感知

服务A编辑数据库,服务B监听A的mysql内容,对于A而言,感知不到服务B的存在

3、缺点

  • 主库压力大

如果写的并发很高,服务B监听binlog,同步信息,会对主库造成压力

  • 必须写库成功后,才能监听到binlog同步的内容

假如B站用户点赞行为,A服务需要写库,写完库后,B服务需要同步财务结算up主收益

假如A写库失败了,B服务监听不到binlog,本次点赞行为就丢失了。这种场景不适合

这种场景可以使用MQ来解决


快速入门

1、pom

<!-- https://mvnrepository.com/artifact/com.gitee.Jmysy/binlog4j-spring-boot-starter -->
<dependency><groupId>com.gitee.Jmysy</groupId><artifactId>binlog4j-spring-boot-starter</artifactId><version>1.9.1</version>
</dependency>
  • 支持集群模式
  • 宕机续读(服务A写mysql,服务B监听,假如在服务B宕机期间,A写了新数据,则B重新启动后,仍可以获取到期间的binlog同步内容),实现:通过redis配置的serverId,去对应的binlogfileName + position获取
  • 数据转换

2、yml配置

spring:binlog4j:redis-config:host: 127.0.0.1port: 6379database: 1client-configs:master:host: "127.0.0.1"port: 3306username: "root"password: "941123"mode: standalonepersistence: trueserver-id: 1234

2、code实现

package com.mjp.binlog;import com.gitee.Jmysy.binlog4j.core.BinlogEvent;
import com.gitee.Jmysy.binlog4j.core.IBinlogEventHandler;
import com.gitee.Jmysy.binlog4j.springboot.starter.annotation.BinlogSubscriber;/*** Author:majinpeng* Date: 2024/08/23 10:47*/
@BinlogSubscriber(clientName = "master")
public class Binlog4jServiceHandler implements IBinlogEventHandler<Object> {// 这里的Object即你数据库中的实体类,比如Order、User// 如果可以确定,则使用具体的User// 如果是Object,则binlogEvent中的T data属性是map@Overridepublic void onInsert(BinlogEvent<Object> binlogEvent) {}@Overridepublic void onUpdate(BinlogEvent<Object> binlogEvent) {System.out.println(binlogEvent.toString());}@Overridepublic void onDelete(BinlogEvent<Object> binlogEvent) {}@Overridepublic boolean isHandle(String s, String s1) {// 如果将<Object>使用具体的<User>接收,则一定需要在isHandle中,判断当前Binlog4jServiceHandler是否执行了// 否则可能会因为映射的对象不符合,造成数据丢失return false;}
}

监听普通文件内容变动

1、实现CommandLineRunner接口 或 ApplicationRunner接口

2、使用hutool的WatchMonitor工具类(底层封装了JDK的WatchService)

<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.31</version>
</dependency>

3、使用Tailer工具类

@Component
public class MonitorFile implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {File file = FileUtil.file("C:\\Users\\admin\\Desktop\\test.txt");try (WatchMonitor watchMonitor = WatchMonitor.create(file, WatchMonitor.ENTRY_MODIFY)) {watchMonitor.setWatcher(new Watcher() {@Overridepublic void onCreate(WatchEvent<?> event, Path currentPath) {}@Overridepublic void onModify(WatchEvent<?> event, Path currentPath) {Object context = event.context();System.out.println("modify!!!" + context.toString());}@Overridepublic void onDelete(WatchEvent<?> event, Path currentPath) {}@Overridepublic void onOverflow(WatchEvent<?> event, Path currentPath) {}});// 监听目录的足最大深度,watchMonitor.setMaxDepth(3);watchMonitor.start();}Tailer tailer = new Tailer(file, line -> System.out.println("变动的数据内容为:" + line));tailer.start();}
}

http://www.mrgr.cn/news/5927.html

相关文章:

  • 酷家乐 同盾滑块分析
  • ISO7841标准数字隔离器在现代电子系统中的作用
  • 2024最新50道NLP和人工智能领域面试题+答案(中文+英文双版本)
  • linux基础命令(超级详细)
  • oracle的dataguard physical standby转 snapshot standby操作文档
  • react 的学习随记
  • 如何通过变更让 PostgreSQL 翻车
  • 【STM32单片机_(HAL库)】3-2-3【中断EXTI】【电动车报警器项目】433M无线收发模块实验
  • Flutter Listview 缓存item滑动后不进行重新渲染
  • SQL - 索引
  • Thinkphp6 反序列化漏洞分析
  • 【数学建模备赛】Ep06:多元线性回归分析
  • 80页WORD方案深入了解大数据治理+大数据资产管理+数据运营
  • 阿里云ubuntu系统安装jdk + tomcat
  • 单例模式 详解
  • 128. 最长连续序列
  • 科技大通缩
  • 框架——MyBatis(!!!MyBatis 环境搭建步骤)
  • JAVA设计模式之【单例模式】
  • 一文掌握 Web 测试:功能、界面、兼容与安全的综合测试指南!