首页 > 解决方案 > RabbitMQ:ACCESS_REFUSED 即使队列是非独占的

问题描述

我正在尝试通过 C++ 中的 RabbitMQ 在两台计算机之间发送和接收消息。我的发件人代码如下所示:

AmqpClient::Channel::ptr_t channel = AmqpClient::Channel::Create("192.168.1.1", 5672, "test", "test", "/");
channel->DeclareQueue("rabbit", false, true, false, false);
AmqpClient::BasicMessage::ptr_t msg = AmqpClient::BasicMessage::Create("Hello Rabbit!");
channel->BasicPublish("", "rabbit", msg);
std::cout << "[x] message sent!" << std::endl;

我在另一台具有 ip 地址的计算机上的接收器代码192.168.1.1如下所示:

AmqpClient::Channel::ptr_t connection = AmqpClient::Channel::Create("localhost", 5672, "test", "test", "/");
connection->DeclareQueue("rabbit", false, true, false, false);

std::string consumer = connection->BasicConsume("rabbit", "", true, false);
while(true)
{
    AmqpClient::Envelope::ptr_t envelope = connection->BasicConsumeMessage(consumer);
    std::cout << "received message: " << envelope->Message()->Body() << std::endl;
    std::cout << "[x] ready to receive new message..." << std::endl;
    connection->BasicAck(envelope);
}

当接收方在发送方代码完成并终止后开始接收时,这工作得很好。但是当我第一次打开接收器让它待机并发送消息时,在发送方,我收到以下错误:

terminate called after throwing an instance of 'AmqpClient::AccessRefusedException'
  what(): channel error: 403: AMQP_BASIC_CONSUME_METHOD caused: ACCESS_REFUSED - queue 'rabbit' in vhost '/' in exclusive use

我知道当声明的队列处于独占模式时应该引发此错误,但在这里我将其声明为非独占(中的第三个布尔参数channel->DeclareQueue)。此外,我的测试用户拥有完全权限。这里可能是什么问题?谢谢你。

标签: rabbitmq

解决方案


好的,我发现 is 的默认值BasicConsumeexclusive所以在声明你的消费者时让它是很重要的false(第三个布尔参数):

std::string consumer = connection->BasicConsume("rabbit", "", true, false, false);
                                                                           ^^^^^

有趣的是,我在官方文档中没有找到任何关于此的句子。


推荐阅读