网站建设安全措施,wordpress 右侧空白,网站建设长春,飘仙建站论坛当你已经用 MongoDB 承载业务数据#xff0c;但又希望具备 Elasticsearch 的全文检索、聚合分析与近实时查询能力时#xff0c;最经典的方案就是#xff1a;
业务写 MongoDB同步链路把变更实时投递到 Elasticsearch查询侧走 Elasticsearch#xff08;搜索/统计/多维筛选但又希望具备 Elasticsearch 的全文检索、聚合分析与近实时查询能力时最经典的方案就是业务写 MongoDB同步链路把变更实时投递到 Elasticsearch查询侧走 Elasticsearch搜索/统计/多维筛选很多人一开始会想到Kafka Debezium、Logstash、或者自己写同步服务。但它们要么架构偏重要么运维成本高要么“断点续传/一致性/重试/死信队列”很难一次做对。ElasticRelay 的思路是单一二进制 纯配置驱动 Go 低资源占用同时把 CDCChange Data Capture常见的工程问题快照/增量/Checkpoint/DLQ都内置掉让你用更轻的方式把链路跑起来。1. ElasticRelay 的 MongoDB → ES 同步是怎么工作的核心链路可以理解为四步初始快照可选启动时先把目标集合做一次全量扫表snapshot把现有数据写入 ES。增量同步Change Streams开启 MongoDB Change Streams 监听insert/update/replace/delete。Resume Token Checkpoint每条变更事件都会带一个 resume tokenElasticRelay 会把它落盘默认mongodb_checkpoints.json用于断点续传。ES Bulk 写入ElasticRelay 将事件转换成 JSON按批次 bulk 写入 Elasticsearch。索引命名规则默认index_prefix-collectioncollection 会转小写MongoDB 事件里集合名字段_collection快照记录里集合名字段_tableES sink 同时支持_table与_collection2. 前置条件别跳过2.1 MongoDB 必须是 Replica Set 或 Sharded ClusterChange Streams 的硬要求MongoDB 4.0推荐 6/7Replica Set或Sharded Cluster单机 standalone 不支持 Change Streams会报 “Change Stream not supported”2.2 MongoDB 用户权限最小化建议Change Streams 通常需要业务库的readWrite至少readlocal库的read用于 Change Streams/相关内部读取仓库自带的 Docker 初始化脚本已经帮你创建了用户用户elasticrelay_user密码elasticrelay_pass2.3 ElasticsearchES 7.x / 8.x 均可如果开启了安全认证准备好user/password3. 5 分钟跑通Docker 起 MongoDB ES本机跑 ElasticRelay下面这套流程尽量复用仓库现成的 compose 与配置文件。3.1 启动 MongoDB Replica Set仓库已内置在项目根目录执行docker-compose up -d mongodb mongodb-init它会拉起elasticrelay-mongodbMongoDB 7.0副本集 rs0elasticrelay-mongodb-init一次性初始化副本集与测试数据3.2 启动 Elasticsearch仓库的docker-compose.yml里 Elasticsearch 默认是注释掉的。你可以选择方式 A取消docker-compose.yml里elasticsearch服务的注释然后执行docker-compose up -d elasticsearch方式 B使用你已有的 Elasticsearch只要config/mongodb_config.json里addresses指向你的 ES 即可。3.3 构建 ElasticRelaychmodx scripts/build.sh ./scripts/build.sh产物默认在bin/elasticrelay。3.4 配置 MongoDB → ES 同步任务你可以直接使用仓库自带示例config/mongodb_config.json。其中最关键的四块配置data_sourcesMongoDB 连接信息、监听哪些集合table_filterssinksElasticsearch 地址与索引前缀index_prefixjobs把 source 与 sink 绑起来并配置是否先做全量initial_syncglobal日志、metrics、DLQ 等最小可跑版本示意与仓库示例一致{version:2.0,data_sources:[{id:mongodb-primary,type:mongodb,host:localhost,port:27017,user:elasticrelay_user,password:elasticrelay_pass,database:elasticrelay,table_filters:[users,orders,products]}],sinks:[{id:es-primary,type:elasticsearch,addresses:[http://localhost:9200],options:{index_prefix:elasticrelay_mongo}}],jobs:[{id:mongodb-to-es,source_id:mongodb-primary,sink_id:es-primary,enabled:true,options:{initial_sync:true}}],global:{log_level:info}}3.5 启动 ElasticRelay./bin/elasticrelay --config config/mongodb_config.json启动后你会看到类似日志连接 MongoDB 成功开始快照如果initial_synctrueChange Streams started写入 ES自动创建索引4. 验证同步插入/更新/删除都能实时反映到 ES4.1 查看 ES 索引是否生成如果index_prefixelasticrelay_mongo同步users集合会生成索引elasticrelay_mongo-users执行curl-X GEThttp://localhost:9200/_cat/indices?v4.2 查一条数据curl-X GEThttp://localhost:9200/elasticrelay_mongo-users/_search?pretty\-HContent-Type: application/json\-d{query:{match_all:{}},size:10}4.3 触发增量对 MongoDB 做写入如果你用的是仓库自带 Docker MongoDBdockerexec-it elasticrelay-mongodb mongosh\-u elasticrelay_user -p elasticrelay_pass\--authenticationDatabase admin然后执行use elasticrelay// insertdb.users.insertOne({name:Test User,email:testexample.com,age:30,created_at:newDate(),updated_at:newDate()})// updatedb.users.updateOne({email:testexample.com},{$set:{age:31,updated_at:newDate()}})// deletedb.users.deleteOne({email:testexample.com})回到 ES 再查一次你应该能看到变化实时出现或删除生效。5. 运维要点Checkpoint、重启恢复、DLQ5.1 Checkpoint断点续传MongoDB CDC 使用 Change Streams 的resume token做 checkpoint。默认落盘文件mongodb_checkpoints.json进程重启会先尝试加载该文件从上次位置继续常见注意事项如果 resume token 因 oplog 窗口太短而失效会出现类似 “Resume token not found / resume of change stream was not possible”。这时需要增大 oplog window生产推荐或者清理 checkpoint 重新从当前/全量开始谨慎5.2 DLQ死信队列当 Elasticsearch 不可用、索引创建失败、bulk 写入失败时ElasticRelay 可以把事件打到 DLQ避免直接丢数据并按策略重试。你可以在配置里打开global.dlq_config.enabledtrueglobal.dlq_config.storage_path./dlq6. 常见坑与排障高频Change Stream not supportedMongoDB 不是 replica set/sharded按副本集方式部署。认证失败核对用户名密码注意authSource仓库示例走admin。ES 写入 429/队列堆积降低写入批次、加大 flush interval、或扩容 ES。字段映射混乱MongoDB 文档结构变化大、字段类型不稳定时建议提前准备 ES template/mapping或至少保证关键字段类型稳定。想要试用 ElasticRelay查看我们的 GitHub 仓库 或阅读我们的 入门指南。有问题加入我们的 社区讨论 或在 Twitter 上联系我们。ElasticRelay 团队致力于构建更好的数据基础设施工具。关注我们的旅程我们让实时数据同步变得简单、可靠并且让每个开发者都能使用。