c++ - rabbitmq c++ 客户端未在 5672 端口连接到 rabbitmq-server

标签 c++ rabbitmq

我已经在我的 Linux 机器上安装了 rabbitmq-server。我安装了RABBIRMQ官网给的AMQP-CPP客户端库。 enter link description here

现在我想作为生产者连接到 rabbitmq-server 并想在队列中发布消息。 我做了一个连接处理程序,主文件如下:

int main(int argc, char ** argv) {
    const std:: string exchange = "my-exchange";
    const std:: string routingKey = "my-routing-key";
    const char* message = "HELLO WORLD";

    MyTcpHandler myHandler;
    // address of the server
    cout << "TcpHandler object created.." << endl;
    AMQP:: Address address("amqp://guest:guest@localhost:5672");
    //("amqp://guest:guest@localhost/vhost");
    cout << "address object created.." << endl;
    // create a AMQP connection object
    AMQP:: TcpConnection connection(& myHandler, address);
    cout << "connection object created.." << endl;
    // and create a channel
    AMQP:: TcpChannel channel(& connection);
    cout << "channel object created.." << endl;

    // use the channel object to call the AMQP method you like
    channel.declareExchange("my-exchange", AMQP:: fanout);
    channel.declareQueue("my-queue");
    channel.bindQueue("my-exchange", "my-queue", "my-routing-key");

    cout << "before publish.." << endl;

    // start a transaction
    channel.startTransaction();

    int pubVal = channel.publish(exchange, routingKey, message);

我无法连接到队列。也许我没有在 MyTcpHandler 类中正确实现“monitor”方法: 虚拟无效监视器(AMQP::TcpConnection *连接,int fd,int flags) 有人可以帮忙吗?

最佳答案

现在回答有点晚了。 AMQP-CPP 包装器对我来说也一直是一场噩梦。好吧,如果你不是在寻找一些非常具体的功能,你可以使用不同的 c++ 包装器来代替 https://github.com/alanxz/SimpleAmqpClient

//SimpleAMQPClient Producer    
Channel::ptr_t connection = Channel::Create("MQ server IP",5672,"username","password");
string producerMessage = "this is a test message";
connection->BasicPublish("exchange","routing key",BasicMessage::Create(producerMessage));

完成!!!

现在,如果您坚持使用 AMQP-CPP,我想在上面的代码中指出几件事。

AMQP::Address address("amqp://guest:guest@localhost:5672");

我不确定这个地址,它是你的服务器运行的地方,尝试在端口为 15672 的浏览器中输入相同的地址,看看它是否为你提供服务器的 UI。

amqp://guest:guest@localhost:15672

// use the channel object to call the AMQP method you like
channel.declareExchange("my-exchange", AMQP::fanout);
channel.declareQueue("my-queue");
channel.bindQueue("my-exchange", "my-queue", "my-routing-key");

每次运行这段代码都会声明一个交换器并创建一个队列,大多数情况下它只会在第一次运行,然后每次都会发生名称冲突(如果我的假设是正确的)

更好的方法是从 Rabbit-MQ 服务器 UI 创建队列。 确保你已经安装了 rabbitMQ-server(RabbitMQ-C 是客户端)https://www.rabbitmq.com/download.html#server-installation-guides 并通过 C++ 代码发送消息。

现在您需要做的就是打开浏览器并输入以下链接

您的服务器运行的IP:15672

对于本地,它应该是 127.0.0.1:15672(注意端口号,它是 15672)并输入用户名和密码。

//SimpleAMQPClient Consumer    
string consumer_tag=connection->BasicConsume("queue","",true,false);
Envelope::ptr_t envelope = connection->BasicConsumeMessage(consumer_tag);
BasicMessage::ptr_t bodyBasicMessage=envelope->Message();
string messageBody=bodyBasicMessage->Body();

上面的 simpleAMQPClient 代码是我应用程序中的工作拷贝。

希望对您有所帮助。

关于c++ - rabbitmq c++ 客户端未在 5672 端口连接到 rabbitmq-server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41805270/

相关文章:

c++ - 创建管道并从 C++ 写入 gnuplot 终端

c++ - 将本地 JSONCpp 安装与 CMake 和 ExternalProject_Add 链接

java - Spring 和 AMQP RabbitMQ 主题交换不起作用

php - RabbitMQ 错误超时

php - 如何使用 php 在 RabbitMQ 中检索相同的消息两次或更多次

c++ - 调用复制 ctor 而不是移动 ctor - 编译器可以发出警告吗?

C++ wstringstream << NULL

python - Celeryd 运行多个守护进程

c++ - 多重继承中的虚表实现

c# - RabbitMQ - 将未确认的消息传递到不同的队列