【问题标题】:rabbitmq c++ client not connecting to rabbitmq-server at 5672 portrabbitmq c ++客户端未在5672端口连接到rabbitmq-server
【发布时间】:2017-01-23 11:27:08
【问题描述】:

我已经在我的 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 类中正确实现“监视器”方法: 虚拟 void 监视器(AMQP::TcpConnection *connection,int fd,int flags) 有人可以帮忙吗?

【问题讨论】:

  • 嗨,谁能帮帮我?请...
  • 您遇到的错误是什么?
  • 嗨@cantSleepNow 我无法将消息从生产者端发布到我的代理中的队列。即使我无法连接到经纪人。尽管我的制片人没有给出任何错误。我正在使用上面提到的 C++ 客户端。请帮帮我。我长期以来一直面临这个问题。提前致谢。

标签: c++ rabbitmq


【解决方案1】:

这个答案有点晚了。 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 代码是我的应用程序中的一个工作副本。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    • 2017-06-14
    • 2017-11-15
    • 2015-11-17
    • 1970-01-01
    • 2014-05-25
    相关资源
    最近更新 更多