refactor(rabbitmq): 抽出可测试的拓扑声明层
把"交换机 + 队列 + 绑定键 + 死信队列"的声明逻辑从 rabbitMQ.go 抽成独立的 Topology 类型与 DeclareTopology/DeclareTopologies,并引入声明所需的最小接口 declarer(*amqp.Channel 天然满足)。 两处动机:其一,后续连接管理器需要在每次重连后重放同一批拓扑,声明逻辑必须 可复用、可枚举;其二,声明顺序(交换机 → 队列 → 绑定)与队列上的 x-dead-letter-exchange 参数是重构中最容易静默丢失的两件事,抽出接口后即可用 假实现断言它们,无需真实 broker。 DeclareDLX 新增 declareDLX(declarer, ...) 供拓扑重放复用;DeclareTopic 与 DeclareDLX 均保留原有签名,现有调用点无需改动。
This commit is contained in:
@@ -11,24 +11,32 @@ const (
|
|||||||
MaxRetryCount = 3
|
MaxRetryCount = 3
|
||||||
)
|
)
|
||||||
|
|
||||||
// DeclareDLX 声明死信交换机和对应的死信队列
|
// DeclareDLX 声明死信交换机和对应的死信队列(保留原有签名)。
|
||||||
func DeclareDLX(ch *amqp.Channel, queueName string) error {
|
func DeclareDLX(ch *amqp.Channel, queueName string) error {
|
||||||
if ch == nil {
|
if ch == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err := ch.ExchangeDeclare(
|
return declareDLX(ch, queueName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// declareDLX 是 DeclareDLX 的接口版本,供拓扑重放路径复用。
|
||||||
|
func declareDLX(d declarer, queueName string) error {
|
||||||
|
if d == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err := d.ExchangeDeclare(
|
||||||
DLXExchange, "topic", true, false, false, false, nil,
|
DLXExchange, "topic", true, false, false, false, nil,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
dlxQueue := queueName + ".dlx"
|
dlxQueue := queueName + ".dlx"
|
||||||
_, err := ch.QueueDeclare(
|
_, err := d.QueueDeclare(
|
||||||
dlxQueue, true, false, false, false, nil, // 死信队列不设置 DLX
|
dlxQueue, true, false, false, false, nil, // 死信队列不设置 DLX
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := ch.QueueBind(dlxQueue, "#", DLXExchange, false, nil); err != nil {
|
if err := d.QueueBind(dlxQueue, "#", DLXExchange, false, nil); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Printf("DLX ready: exchange=%s queue=%s", DLXExchange, dlxQueue)
|
log.Printf("DLX ready: exchange=%s queue=%s", DLXExchange, dlxQueue)
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"feedsystem_video_go/internal/config"
|
"feedsystem_video_go/internal/config"
|
||||||
"log"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -48,53 +47,6 @@ func (r *RabbitMQ) NewChannel() (*amqp.Channel, error) {
|
|||||||
return r.Conn.Channel()
|
return r.Conn.Channel()
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeclareTopic(ch *amqp.Channel, exchange string, queue string, bindingKey string) error {
|
|
||||||
if ch == nil {
|
|
||||||
return errors.New("channel is not initialized")
|
|
||||||
}
|
|
||||||
if exchange == "" || queue == "" || bindingKey == "" {
|
|
||||||
return errors.New("exchange/queue/bindingKey is required")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := ch.ExchangeDeclare(
|
|
||||||
exchange,
|
|
||||||
"topic",
|
|
||||||
true, // 持久化参数
|
|
||||||
false, // autoDelete 是否在未使用时自动删除
|
|
||||||
false, // internal 是否交给其他交换机用而不直接收消息
|
|
||||||
false, // noWait 是否不等待 broker 确认
|
|
||||||
nil,
|
|
||||||
); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
q, err := ch.QueueDeclare(
|
|
||||||
queue, // 队列名称
|
|
||||||
true, // 持久化
|
|
||||||
false, // autoDelete 是否在未使用时自动删除
|
|
||||||
false, // exclusive 是否排他(允许多个消费者共享)
|
|
||||||
false, // noWait 是否不等待 broker 确认
|
|
||||||
amqp.Table{"x-dead-letter-exchange": DLXExchange}, // 死信交换机
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := ch.QueueBind(
|
|
||||||
q.Name,
|
|
||||||
bindingKey,
|
|
||||||
exchange,
|
|
||||||
false, // noWait
|
|
||||||
nil, // args
|
|
||||||
); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := DeclareDLX(ch, queue); err != nil {
|
|
||||||
log.Printf("DLX declare failed for %s: %v", queue, err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func PublishJSON(ctx context.Context, ch *amqp.Channel, exchange string, routingKey string, payload any) error {
|
func PublishJSON(ctx context.Context, ch *amqp.Channel, exchange string, routingKey string, payload any) error {
|
||||||
if ch == nil {
|
if ch == nil {
|
||||||
return errors.New("channel is not initialized")
|
return errors.New("channel is not initialized")
|
||||||
|
|||||||
103
backend/internal/middleware/rabbitmq/topology.go
Normal file
103
backend/internal/middleware/rabbitmq/topology.go
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
package rabbitmq
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
amqp "github.com/rabbitmq/amqp091-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Topology 描述一组"交换机 + 队列 + 绑定键",是可重复声明的幂等单元。
|
||||||
|
//
|
||||||
|
// 连接管理器会在每一代连接上重放已登记的拓扑,因此 broker 侧拓扑丢失
|
||||||
|
// (数据卷重建、队列被手工删除、集群节点替换)之后也能自动恢复——
|
||||||
|
// 这一条与"连接重连"同等重要:只重连不重放拓扑,
|
||||||
|
// 会退化为"连接健康但队列不存在"的 404 死循环。
|
||||||
|
type Topology struct {
|
||||||
|
Exchange string
|
||||||
|
Queue string
|
||||||
|
BindingKey string
|
||||||
|
}
|
||||||
|
|
||||||
|
// declarer 是 *amqp.Channel 上拓扑声明所需方法的最小集合。
|
||||||
|
//
|
||||||
|
// 抽出接口的唯一目的是可测性:声明顺序与 DLX 参数是"重构时最容易静默丢失"
|
||||||
|
// 的两件事,有了它就能在没有 broker 的情况下断言它们。
|
||||||
|
type declarer interface {
|
||||||
|
ExchangeDeclare(name, kind string, durable, autoDelete, internal, noWait bool, args amqp.Table) error
|
||||||
|
QueueDeclare(name string, durable, autoDelete, exclusive, noWait bool, args amqp.Table) (amqp.Queue, error)
|
||||||
|
QueueBind(name, key, exchange string, noWait bool, args amqp.Table) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeclareTopology 声明一组拓扑:交换机 → 队列(带 DLX)→ 绑定 → 死信队列。
|
||||||
|
//
|
||||||
|
// 注意:d 为 nil 的判断只能拦住"nil 接口"。若调用方传入类型化的 nil
|
||||||
|
// (如 var ch *amqp.Channel = nil),此处无法识别——因此 *amqp.Channel
|
||||||
|
// 的入口(DeclareTopic)保留了自己的 nil 检查。
|
||||||
|
func DeclareTopology(d declarer, t Topology) error {
|
||||||
|
if d == nil {
|
||||||
|
return errors.New("channel is not initialized")
|
||||||
|
}
|
||||||
|
if t.Exchange == "" || t.Queue == "" || t.BindingKey == "" {
|
||||||
|
return errors.New("exchange/queue/bindingKey is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := d.ExchangeDeclare(
|
||||||
|
t.Exchange,
|
||||||
|
"topic",
|
||||||
|
true, // durable
|
||||||
|
false, // autoDelete
|
||||||
|
false, // internal
|
||||||
|
false, // noWait
|
||||||
|
nil,
|
||||||
|
); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
q, err := d.QueueDeclare(
|
||||||
|
t.Queue,
|
||||||
|
true, // durable
|
||||||
|
false, // autoDelete
|
||||||
|
false, // exclusive
|
||||||
|
false, // noWait
|
||||||
|
amqp.Table{"x-dead-letter-exchange": DLXExchange},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := d.QueueBind(
|
||||||
|
q.Name,
|
||||||
|
t.BindingKey,
|
||||||
|
t.Exchange,
|
||||||
|
false, // noWait
|
||||||
|
nil,
|
||||||
|
); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DLX 只声明一次即可;重复声明幂等。失败降级为告警(与改造前行为一致)。
|
||||||
|
if err := declareDLX(d, t.Queue); err != nil {
|
||||||
|
log.Printf("DLX declare failed for %s: %v", t.Queue, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeclareTopologies 按登记顺序重放拓扑。顺序有意义:交换机先于队列先于绑定。
|
||||||
|
func DeclareTopologies(d declarer, topo []Topology) error {
|
||||||
|
for _, t := range topo {
|
||||||
|
if err := DeclareTopology(d, t); err != nil {
|
||||||
|
return fmt.Errorf("declare %s/%s: %w", t.Exchange, t.Queue, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeclareTopic 保留原有签名,既有调用点无需改动。
|
||||||
|
func DeclareTopic(ch *amqp.Channel, exchange string, queue string, bindingKey string) error {
|
||||||
|
if ch == nil {
|
||||||
|
return errors.New("channel is not initialized")
|
||||||
|
}
|
||||||
|
return DeclareTopology(ch, Topology{Exchange: exchange, Queue: queue, BindingKey: bindingKey})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user