# zmigrate **Repository Path**: youkelike/zmigrate ## Basic Information - **Project Name**: zmigrate - **Description**: 不停机数据迁移组件 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-08-03 - **Last Updated**: 2025-08-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 方案概述 不停机数据迁移方案专为微服务拆分场景设计,实现平滑过渡与数据一致性保障。 - 通过全表扫描实现全量数据校验和修复,保障数据初始一致性。 - 通过接入 canal 进行数据增量校验和修复,确保数据最终一致,支持双向修复。 - 提供 gorm 双写插件,进一步保障数据迁移过程的实时一致性。 - 提供 HTTP 接口,控制数据迁移过程,满足平滑迁移需求。 - 支持多表同时迁移。 ### 迁移流程 - 微服务拆分后,使用本地调用与微服务调用并存的方式验证,数据库未变动。 - 确认微服务调用无误后,全面转向微服务调用,启动数据库拆分。 - 将目标微服务所需表全量备份至新库,开启不停机数据迁移。 ### 数据修复生产者与消费者 - 为 mysql 实例配置单一canal,所有表共享canal topic:“zbook_binlog”。 - 为每个迁移表创建独立canal消费者组:“migrator_incr_+表名” - 为每个迁移表构建独立的生产者-消费者对,使用topic:“migrator_表名”。 ### 增加一个迁移表的步骤: - 表模型实现 migrator.Entity 接口,给表模型字段加上 mapstructure tag, - 在 ioc/migrator.go 文件中对应位置增加构造 events.FixDataConsumer、events.FixDataProducer、canal.CanalConsumer、validator.FullValidator 代码, - 在 kafka 中新建 topic:“migrator_表名”, - 还可能要把对这张表的所有操作都拆分出来,单独注入双写的 connPool, - 如果是对整个微服务数据库拆分,只要做好微服务代码拆分就行, - 否则仅对单表的所有操作进行拆分, ### 数据迁移操作顺序 - 手动开启全量校验, - 切换双写方向 src_first,自动开启增量校验、双写, - 手动开启全量校验, - 切换双写方向 dst_first,检查日志, - 最后切换到 dst_only ### 启动相关服务 ```shell docker-compose up -d ``` ### 创建 kafka topic ```shell docker exec -it zmigrate_kafka_1 /opt/bitnami/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --topic zbook_binlog --create --partitions 3 --replication-factor 1 docker exec -it zmigrate_kafka_1 /opt/bitnami/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --topic migrator_interactives --create --partitions 3 --replication-factor 1 docker exec -it zmigrate_kafka_1 /opt/bitnami/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --topic migrator_user_like_bizs --create --partitions 3 --replication-factor 1 查看所有 topic docker exec -it zmigrate_kafka_1 /opt/bitnami/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --list 查看指定 topic 详细 docker exec -it zmigrate_kafka_1 /opt/bitnami/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --topic zbook_binlog --describe 查看所有消费者组 docker exec -it zmigrate_kafka_1 /opt/bitnami/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list 查看指定消费者组详细 docker exec -it zmigrate_kafka_1 /opt/bitnami/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group migrator-fix 启动监听客户端 go install github.com/IBM/sarama/tools/kafka-console-consumer kafka-console-consumer -topic=zbook_binlog -brokers=localhost:9094 ``` ### 启动服务 ```shell cd example/ go run main.go app.go wire_gen.go ``` ### 通过 http 接口控制迁移过程的双写方向、数据校验和修复 ```shell curl --location --request POST 'http://192.168.34.4:8082/migrator/src_only' curl --location --request POST 'http://192.168.34.4:8082/migrator/full/start' curl --location --request POST 'http://192.168.34.4:8082/migrator/src_first' curl --location --request POST 'http://192.168.34.4:8082/migrator/dst_first' curl --location --request POST 'http://192.168.34.4:8082/migrator/dst_only' curl --location --request GET 'http://192.168.34.4:8082/test/incr_read_cnt' \ --form 'biz="test"' \ --form 'biz_id="1"' curl --location --request GET 'http://192.168.34.4:8082/test/like' \ --form 'biz="test"' \ --form 'biz_id="1"' \ --form 'uid="101"'