2026-10-14 14:02:15 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 13:12:38 +08:00
|
|
|
|
// DeclareTopologies 先声明停泊链路,再逐个声明业务拓扑。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 顺序理由:业务队列被拒绝时,消息要立刻有地方可去——停泊链路晚一步,
|
|
|
|
|
|
// 那段时间里 broker 会直接丢弃,而 worker 以为自己已经处理完了。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 前提:调用方至少登记了一条业务拓扑(两个二进制都登记 ≥3 条)。
|
|
|
|
|
|
// rabbitMQ.go 的 declareAll 在 len(topo)==0 时会提前返回,
|
|
|
|
|
|
// 那种情况下这里的 declareParking 不会被执行。
|
2026-10-14 14:02:15 +08:00
|
|
|
|
func DeclareTopologies(d declarer, topo []Topology) error {
|
2026-10-30 13:12:38 +08:00
|
|
|
|
if err := declareParking(d); err != nil {
|
|
|
|
|
|
return fmt.Errorf("declare parking: %w", err)
|
|
|
|
|
|
}
|
2026-10-14 14:02:15 +08:00
|
|
|
|
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})
|
|
|
|
|
|
}
|