路由键不匹配,但消息仍被发送到队列

Routing key does not match but still message gets sent to queue

我正在尝试让兔子向不同主题发送循环消息。 我有 1 个名为 "endpoint/1" 的队列 我正在向 "endpoint/1" 和 "endpoint/2" 发送消息。 "endpoint/2" 不存在,所以我希望这些消息会消失,但它们会被发送到队列 "endpoint/1" 事件,尽管没有绑定到它!

我不知道为什么会这样,我做错了什么吗?

// declare exchange
ch.ExchangeDeclare("uop_fanout", "fanout", false, false, false, false, nil)

//send
ch.Publish("uop_fanout", topic, false, false, amqp.Publishing{Body: msg})

// listend
q, err := ch.QueueDeclare(topic, false, false, false, false, nil)
    if err != nil {
        return nil, err
    }
    err = ch.QueueBind(q.Name, topic, "uop_fanout", false, nil)
    if err != nil {
        return nil, err
    }
    messagesFanout, err := ch.Consume(q.Name, "", false, false, false, false, nil)
    if err != nil {
        return nil, err
    }

用这个语句:

ch.ExchangeDeclare("uop_fanout", "fanout", false, false, false, false, nil)

您正在声明类型为 fanout 的交换。这意味着到达交换器的消息被克隆并发送到所有绑定到该交换器的队列。

不清楚你所说的"round robin messages to different topics"是什么意思。

如果您只想进行循环负载平衡,您可以简单地将消息路由到单个队列,并为该队列设置两个或更多消费者。

如果您想按主题分发消息,可以使用具有特定路由键的 direct 交换。到达的消息将被发送到与匹配的路由键绑定的队列。

当然,您可以将这些概念结合起来。

来源:https://www.rabbitmq.com/tutorials/amqp-concepts.html