2026-10-14 14:02:15 +08:00
|
|
|
|
package rabbitmq
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"fmt"
|
2026-10-30 15:23:06 +08:00
|
|
|
|
"time"
|
2026-10-14 14:02:15 +08:00
|
|
|
|
|
|
|
|
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Topology 描述一组"交换机 + 队列 + 绑定键",是可重复声明的幂等单元。
|
|
|
|
|
|
type Topology struct {
|
|
|
|
|
|
Exchange string
|
|
|
|
|
|
Queue string
|
|
|
|
|
|
BindingKey string
|
2026-10-31 10:08:03 +08:00
|
|
|
|
// RetryDelay 是本队列重试队列的 TTL,零值取 DefaultRetryDelay;不参与去重。
|
2026-10-30 15:23:06 +08:00
|
|
|
|
RetryDelay time.Duration
|
2026-10-14 14:02:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-31 10:08:03 +08:00
|
|
|
|
// declarer 是拓扑声明所需方法的最小集合,抽出接口只为可测性。
|
2026-10-14 14:02:15 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-31 10:08:03 +08:00
|
|
|
|
// DeclareTopology 按固定顺序声明一组拓扑。
|
2026-10-30 15:23:06 +08:00
|
|
|
|
//
|
2026-10-31 10:08:03 +08:00
|
|
|
|
// 顺序不可换:业务队列被拒绝时必须能找到一个已存在的 DLX,否则 broker 直接丢弃消息。
|
2026-10-14 14:02:15 +08:00
|
|
|
|
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")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 15:23:06 +08:00
|
|
|
|
// 1. 业务交换机
|
2026-10-31 10:08:03 +08:00
|
|
|
|
if err := d.ExchangeDeclare(t.Exchange, "topic", true, false, false, false, nil); err != nil {
|
2026-10-14 14:02:15 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 15:23:06 +08:00
|
|
|
|
// 2. 本队列专属的重试交换机,必须先于业务队列
|
|
|
|
|
|
retryExchange := RetryExchange(t.Queue)
|
2026-10-31 10:08:03 +08:00
|
|
|
|
if err := d.ExchangeDeclare(retryExchange, "topic", true, false, false, false, nil); err != nil {
|
2026-10-30 15:23:06 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-31 10:08:03 +08:00
|
|
|
|
// 3. 业务队列:DLX 指向重试交换机
|
|
|
|
|
|
q, err := d.QueueDeclare(t.Queue, true, false, false, false, RetryArgs(t.Queue))
|
2026-10-14 14:02:15 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 15:23:06 +08:00
|
|
|
|
// 4. 业务绑定
|
2026-10-31 10:08:03 +08:00
|
|
|
|
if err := d.QueueBind(q.Name, t.BindingKey, t.Exchange, false, nil); err != nil {
|
2026-10-14 14:02:15 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-31 10:08:03 +08:00
|
|
|
|
// 5. 重试队列:没有消费者,靠 TTL 到期经默认交换机回源
|
2026-10-30 15:23:06 +08:00
|
|
|
|
delay := t.RetryDelay
|
|
|
|
|
|
if delay <= 0 {
|
|
|
|
|
|
delay = DefaultRetryDelay
|
2026-10-14 14:02:15 +08:00
|
|
|
|
}
|
2026-10-31 10:08:03 +08:00
|
|
|
|
rq, err := d.QueueDeclare(RetryQueueName(t.Queue), true, false, false, false, RetryQueueArgs(t.Queue, delay))
|
2026-10-30 15:23:06 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-31 10:08:03 +08:00
|
|
|
|
// 6. 重试绑定:第 1 次被拒时保留的是原路由键,一条 # 绑定覆盖全部
|
2026-10-30 15:23:06 +08:00
|
|
|
|
return d.QueueBind(rq.Name, "#", retryExchange, false, nil)
|
2026-10-14 14:02:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 13:12:38 +08:00
|
|
|
|
// DeclareTopologies 先声明停泊链路,再逐个声明业务拓扑。
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-31 10:08:03 +08:00
|
|
|
|
// DeclareTopic 保留原有签名。
|
2026-10-14 14:02:15 +08:00
|
|
|
|
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})
|
|
|
|
|
|
}
|