MySQL Binlog 监听方案

如果 EmbeddedEngine 类在 debezium-connector-mysql 中不可用,原因是 Debezium 的新版本移除了 EmbeddedEngine 。这是因为 Debezium 的架构变更,它现在鼓励使用 Kafka ConnectDebezium Server 来处理数据变更事件。

下面是几种替代方法来实现 MySQL Binlog 监听,你可以根据项目需求选择合适的方案。


替代方案 1:使用 Debezium Server 监听 Binlog

Debezium Server 是一个独立的服务,可以将 MySQL Binlog 的变更事件发送到目标系统,如 KafkaPulsarGoogle Pub/Sub 等。你可以将它作为一个独立服务运行,并将结果集成到 Spring Boot 中。


🔧 步骤:

1️⃣ 下载 Debezium Server

从官方 GitHub 下载最新的 Debezium Server:

👉 Debezium Server Releases


2️⃣ 配置 application.properties

在 Debezium Server 的配置文件中配置 MySQL 连接目标系统(如 Kafka)。

properties 复制代码
debezium.sink.type=kafka
debezium.sink.kafka.bootstrap.servers=localhost:9092

debezium.source.connector.class=io.debezium.connector.mysql.MySqlConnector
debezium.source.database.hostname=localhost
debezium.source.database.port=3306
debezium.source.database.user=root
debezium.source.database.password=your_password
debezium.source.database.server.id=85744
debezium.source.database.include.list=your_database
debezium.source.database.history.kafka.bootstrap.servers=localhost:9092
debezium.source.database.history.kafka.topic=schema-changes.your_database

3️⃣ 运行 Debezium Server

bash 复制代码
java -jar debezium-server-<version>.jar

4️⃣ 在 Spring Boot 中消费 Kafka 消息

你可以在 Spring Boot 中使用 Kafka 监听来自 Debezium 的事件。

添加依赖:
xml 复制代码
<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
</dependency>
创建 Kafka 消费者:
java 复制代码
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class KafkaBinlogConsumer {

    @KafkaListener(topics = "your_database.your_table", groupId = "binlog-group")
    public void listen(String message) {
        System.out.println("Received: " + message);
        // 解析并处理 Binlog 数据
    }
}

替代方案 2:使用 Kafka Connect

如果你已经使用 Kafka ,可以通过 Kafka Connect 部署 Debezium MySQL Connector 来监听 MySQL Binlog


🔧 配置步骤:

1️⃣ 启动 Kafka Connect

确保 Kafka Connect 已启动:

bash 复制代码
./bin/connect-distributed.sh config/connect-distributed.properties

2️⃣ 配置 MySQL Connector

创建一个 Debezium MySQL Connector 的 JSON 文件:

json 复制代码
{
  "name": "mysql-connector",
  "config": {
    "connector.class": "io.debezium.connector.mysql.MySqlConnector",
    "database.hostname": "localhost",
    "database.port": "3306",
    "database.user": "root",
    "database.password": "your_password",
    "database.server.id": "1",
    "database.include.list": "your_database",
    "database.history.kafka.bootstrap.servers": "localhost:9092",
    "database.history.kafka.topic": "schema-changes.your_database"
  }
}

将该配置文件 POST 到 Kafka Connect 的 REST API:

bash 复制代码
curl -X POST -H "Content-Type: application/json" --data @mysql-connector.json http://localhost:8083/connectors

替代方案 3:使用 Maxwell for Binlog 监听

Maxwell 是一个轻量级的工具,用于将 MySQL Binlog 转换成 JSON 格式 并发送到 Kafka、Pulsar、Redis、Elasticsearch 等系统。

👉 Maxwell GitHub


🔧 步骤:

1️⃣ 下载并配置 Maxwell

bash 复制代码
wget https://github.com/zendesk/maxwell/releases/download/v1.40.0/maxwell-1.40.0.tar.gz
tar -xvf maxwell-1.40.0.tar.gz

2️⃣ 运行 Maxwell

bash 复制代码
bin/maxwell --user='root' --password='your_password' --host='localhost' --producer=kafka

替代方案 4:使用 Canal for Binlog 监听

Alibaba Canal 是一个用于监听 MySQL Binlog 的工具,支持将变更数据发送到 KafkaRabbitMQ 等消息队列中。

👉 Canal GitHub


总结对比

工具 使用场景 优势 备注
Debezium Server 不依赖 Kafka 直接将数据推送到目标系统 官方推荐的架构变更方式
Kafka Connect 使用 Kafka 的场景 生态系统丰富,支持多种数据源 适合大规模数据同步
Maxwell 轻量级 JSON Binlog 监听器 简单易用,配置方便 适合小型项目
Canal 高性能、支持多种目标系统 支持多种 MQ,性能较高 需要更多配置

如果你不想引入 Kafka,可以使用 Debezium Server ,它支持直接将 MySQL Binlog 同步到 文件、Pulsar、Google Pub/Sub 等多种目标。

相关推荐
子非衣11 分钟前
MySQL修改JSON格式数据示例
android·mysql·json
qwy71522925816313 分钟前
13-R数据重塑
服务器·数据库·r语言
Bio Coder15 分钟前
R语言安装生物信息数据库包
开发语言·数据库·r语言
钊兵1 小时前
数据库驱动免费下载(Oracle、Mysql、达梦、Postgresql)
数据库·mysql·postgresql·oracle·达梦·驱动
weixin_425878232 小时前
Redis复制性能优化利器:深入解析replica-lazy-flush参数
数据库·redis·性能优化
左灯右行的爱情2 小时前
Redis数据结构总结-listPack
数据结构·数据库·redis
隔壁老王1563 小时前
mysql实时同步到es
数据库·mysql·elasticsearch
想要打 Acm 的小周同学呀4 小时前
Redis三剑客解决方案
数据库·redis·缓存
rkmhr_sef4 小时前
Redis 下载与安装 教程 windows版
数据库·windows·redis