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 等多种目标。

相关推荐
StarRocks_labs2 小时前
StarRocks Community Monthly Newsletter (Jun)
数据库·starrocks·数据湖·物化视图·存算分离
光电的一只菜鸡3 小时前
ubuntu之坑(十五)——设备树
linux·数据库·ubuntu
ob熔天使——武3 小时前
MySQL
数据库·mysql
小光学长4 小时前
基于vue框架的防疫物资仓库管理系统09y38(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库
野生技术架构师8 小时前
MySQL数据实时同步到Elasticsearch的高效解决方案
数据库·mysql·elasticsearch
白仑色8 小时前
Oracle 高可用性与安全性
数据库·oracle·数据安全·goldengate·高可用架构
紫无之紫9 小时前
SQL性能调优经验总结
数据库·sql·性能调优
CZZDg9 小时前
Redis Sentinel哨兵集群
java·网络·数据库
__风__9 小时前
PostgreSQL ExecInitIndexScan 函数解析
数据库·postgresql
小云数据库服务专线9 小时前
GaussDB in的用法
数据库·sql·gaussdb