refactor(rabbitmq): 拓扑常量改为导出,worker/router 复用同一份定义
Some checks failed
CI/CD / Deploy (push) Failing after 5s

原先交换机名 / 队列名 / 绑定键存在三份副本:rabbitmq 包内的未导出常量、cmd/worker/main.go 的本地常量块、internal/http/router.go 的字符串字面量。任一处改名都会让其余两处静默失联——消费一个没人发布的队列,或发布到一个没人监听的交换机,编译与运行都不报错。

- 5 个 MQ 文件的拓扑常量改为导出(仅首字母大写,字符串值一字未改)
- 新增 notification.go,补上 3 个此前只以字面量存在的通知队列常量
- cmd/worker/main.go 删除 12 个本地常量,改用 mqrabbit.*
- router.go 的 13 处字面量改用 rabbitmq.*
- notificationworker.go 的路由键比较改用常量,与发布端同源
- 新增 constants_test.go 锁定 26 个 wire 名称,改名必须配套迁移

附带:outbox 轮询失败现在单独打日志,不再与没有待发消息共用一条静默分支;rabbitMQ.go 合并一处注释换行。
This commit is contained in:
2026-10-28 21:54:35 +08:00
parent 9b8a116852
commit 5f30f264f7
12 changed files with 149 additions and 77 deletions

View File

@@ -22,24 +22,6 @@ import (
"gorm.io/gorm"
)
const (
socialExchange = "social.events"
socialQueue = "social.events"
socialBindingKey = "social.*"
likeExchange = "like.events"
likeQueue = "like.events"
likeBindingKey = "like.*"
commentExchange = "comment.events"
commentQueue = "comment.events"
commentBindingKey = "comment.*"
popularityExchange = "video.popularity.events"
popularityQueue = "video.popularity.events"
popularityBindingKey = "video.popularity.*"
)
// 带重试机制的基础设施连接函数
func connectWithRetry(name string, maxRetries int, fn func() error) {
for i := 0; i < maxRetries; i++ {
@@ -126,14 +108,16 @@ func main() {
// 登记拓扑:每一代连接(含重连后的新连接)都会自动重放这些声明。
// 这是"broker 侧拓扑丢失"也能恢复的关键,不能只在启动时声明一次。
// 交换机名 / 队列名 / 绑定键一律取自 rabbitmq 包的导出常量——它们是跨进程的
// 线缆契约,本地再定义一份就会漂移成"消费一个没人发布的队列"。
rmqMgr.RegisterTopology(
mqrabbit.Topology{Exchange: socialExchange, Queue: socialQueue, BindingKey: socialBindingKey},
mqrabbit.Topology{Exchange: likeExchange, Queue: likeQueue, BindingKey: likeBindingKey},
mqrabbit.Topology{Exchange: commentExchange, Queue: commentQueue, BindingKey: commentBindingKey},
mqrabbit.Topology{Exchange: mqrabbit.SocialExchange, Queue: mqrabbit.SocialQueue, BindingKey: mqrabbit.SocialBindingKey},
mqrabbit.Topology{Exchange: mqrabbit.LikeExchange, Queue: mqrabbit.LikeQueue, BindingKey: mqrabbit.LikeBindingKey},
mqrabbit.Topology{Exchange: mqrabbit.CommentExchange, Queue: mqrabbit.CommentQueue, BindingKey: mqrabbit.CommentBindingKey},
)
if cache != nil {
rmqMgr.RegisterTopology(
mqrabbit.Topology{Exchange: popularityExchange, Queue: popularityQueue, BindingKey: popularityBindingKey},
mqrabbit.Topology{Exchange: mqrabbit.PopularityExchange, Queue: mqrabbit.PopularityQueue, BindingKey: mqrabbit.PopularityBindingKey},
)
}
@@ -160,17 +144,17 @@ func main() {
// 每个 Worker 独立 Channel;连接断开由管理器统一重拨 + 重放拓扑 + 重建 Channel
go mqrabbit.RunConsumer(ctx, rmqMgr, "SocialWorker", 50, func(ch *amqp.Channel) error {
return worker.NewSocialWorker(ch, socialRepo, socialQueue).Run(ctx)
return worker.NewSocialWorker(ch, socialRepo, mqrabbit.SocialQueue).Run(ctx)
})
go mqrabbit.RunConsumer(ctx, rmqMgr, "LikeWorker", 50, func(ch *amqp.Channel) error {
return worker.NewLikeWorker(ch, likeRepo, videoRepo, likeQueue).Run(ctx)
return worker.NewLikeWorker(ch, likeRepo, videoRepo, mqrabbit.LikeQueue).Run(ctx)
})
go mqrabbit.RunConsumer(ctx, rmqMgr, "CommentWorker", 50, func(ch *amqp.Channel) error {
return worker.NewCommentWorker(ch, commentRepo, videoRepo, commentQueue).Run(ctx)
return worker.NewCommentWorker(ch, commentRepo, videoRepo, mqrabbit.CommentQueue).Run(ctx)
})
if cache != nil {
go mqrabbit.RunConsumer(ctx, rmqMgr, "PopularityWorker", 50, func(ch *amqp.Channel) error {
return worker.NewPopularityWorker(ch, cache, popularityQueue).Run(ctx)
return worker.NewPopularityWorker(ch, cache, mqrabbit.PopularityQueue).Run(ctx)
})
}