欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 国际 > rabbitmq引入C++详细步骤

rabbitmq引入C++详细步骤

2025/4/20 23:56:48 来源:https://blog.csdn.net/weixin_45737215/article/details/147249798  浏览:    关键词:rabbitmq引入C++详细步骤

1. 安装RabbitMQ服务器

  • 在Windows上:先安装Erlang,再安装RabbitMQ服务器。安装完成后,可通过访问http://localhost:15672来检查RabbitMQ服务器是否正常运行,默认的用户名和密码是guest/guest

  • 在Linux上:可使用包管理器安装,如在Ubuntu上可使用命令sudo apt-get install --no-install-recommends rabbitmq-server,然后启动和启用RabbitMQ Server,并安装管理插件。

2. 安装librabbitmq库

  • 在Windows上:通过vcpkg安装librabbitmq库。首先克隆vcpkg仓库,然后运行安装脚本,接着使用vcpkg安装librabbitmq,并将vcpkg集成到项目中。

  • 在Linux上:可从RabbitMQ-C的官方GitHub仓库获取源代码,然后编译并安装依赖。

3. 配置C++项目

  • 在Visual Studio中创建新的C++项目,并确保项目中包含了vcpkg的安装路径。vcpkg会自动配置项目,使其能够找到并链接到librabbitmq库。

4. 编写代码实现消息发送和接收

发送消息的程序(Producer)
#include <iostream>
#include <string>
#include <amqp.h>
#include <amqp_tcp_socket.h>
#include <thread>
#include <chrono>// 用于处理 AMQP 错误并输出错误信息
void die_on_error(amqp_rpc_reply_t x, const char* context) {if (x.reply_type != AMQP_RESPONSE_NORMAL) {std::cerr << "Error in " << context << ": "<< amqp_error_string2(x.library_error) << std::endl;exit(1);}
}int main() {const std::string hostname = "localhost";  // RabbitMQ 服务器地址const int port = 5672;  // RabbitMQ 默认端口const std::string exchange = "example_exchange";  // 交换机名称const std::string routing_key = "example_key";  // 路由键,用于绑定队列// 初始化连接amqp_connection_state_t conn = amqp_new_connection();amqp_socket_t* socket = amqp_tcp_socket_new(conn);if (!socket) {std::cerr << "Creating TCP socket failed" << std::endl;return 1;}// 打开 TCP 连接int status = amqp_socket_open(socket, hostname.c_str(), port);if (status) {std::cerr << "Opening TCP socket failed" << std::endl;return 1;}// 登录 RabbitMQdie_on_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in");amqp_channel_open(conn, 1);  // 打开信道die_on_error(amqp_get_rpc_reply(conn), "Opening channel");// 声明交换机(类型为 direct)amqp_exchange_declare(conn, 1, amqp_cstring_bytes(exchange.c_str()), amqp_cstring_bytes("direct"),0, 0, 0, 0, amqp_empty_table);die_on_error(amqp_get_rpc_reply(conn), "Declaring exchange");// 循环发送多条消息for (int i = 1; i <= 1000; ++i) {  // 发送 1000 条消息std::string message = "Hello, RabbitMQ! Message number: " + std::to_string(i);amqp_bytes_t message_bytes = amqp_cstring_bytes(message.c_str());// 设置消息属性amqp_basic_properties_t props;props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG;props.content_type = amqp_cstring_bytes("text/plain");props.delivery_mode = 2;  // 持久化模式// 发送消息到交换机int result = amqp_basic_publish(conn, 1, amqp_cstring_bytes(exchange.c_str()), amqp_cstring_bytes(routing_key.c_str()),0, 0, &props, message_bytes);if (result < 0) {std::cerr << "Error publishing message " << i << std::endl;} else {std::cout << "Message " << i << " published: " << message << std::endl;}// 每次发送后等待 1 秒std::this_thread::sleep_for(std::chrono::seconds(1));}// 清理连接amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS);amqp_connection_close(conn, AMQP_REPLY_SUCCESS);amqp_destroy_connection(conn);return 0;
}

接收消息的程序(Consumer) 

#include <iostream>
#include <string>
#include <amqp.h>
#include <amqp_tcp_socket.h>void die_on_error(amqp_rpc_reply_t x, const char* context) {if (x.reply_type != AMQP_RESPONSE_NORMAL) {std::cerr << "Error in " << context << ": "<< amqp_error_string2(x.library_error) << std::endl;exit(1);}
}int main() {const std::string hostname = "localhost";  // RabbitMQ 服务器地址const int port = 5672;  // RabbitMQ 默认端口const std::string exchange = "example_exchange";  // 交换机名称const std::string routing_key = "example_key";  // 路由键const std::string queue_name = "example_queue";  // 队列名称amqp_connection_state_t conn = amqp_new_connection();amqp_socket_t* socket = amqp_tcp_socket_new(conn);if (!socket) {std::cerr << "Creating TCP socket failed" << std::endl;return 1;}int status = amqp_socket_open(socket, hostname.c_str(), port);if (status) {std::cerr << "Opening TCP socket failed" << std::endl;return 1;}die_on_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in");amqp_channel_open(conn, 1);die_on_error(amqp_get_rpc_reply(conn), "Opening channel");// 声明交换机amqp_exchange_declare(conn, 1, amqp_cstring_bytes(exchange.c_str()), amqp_cstring_bytes("direct"),0, 0, 0, 0, amqp_empty_table);die_on_error(amqp_get_rpc_reply(conn), "Declaring exchange");// 声明队列amqp_queue_declare_ok_t* r = amqp_queue_declare(conn, 1, amqp_cstring_bytes(queue_name.c_str()), 0, 0, 0, 0, amqp_empty_table);die_on_error(amqp_get_rpc_reply(conn), "Declaring queue");// 绑定队列到交换机amqp_queue_bind(conn, 1, amqp_cstring_bytes(queue_name.c_str()), amqp_cstring_bytes(exchange.c_str()), amqp_cstring_bytes(routing_key.c_str()), amqp_empty_table);die_on_error(amqp_get_rpc_reply(conn), "Binding queue");// 开始消费消息amqp_basic_consume(conn, 1, amqp_cstring_bytes(queue_name.c_str()), amqp_empty_bytes, 0, 1, 0, amqp_empty_table);std::cout << "Waiting for messages. To exit press CTRL+C" << std::endl;while (true) {amqp_rpc_reply_t res = amqp_simple_wait_for_event(conn);if (res.reply_type == AMQP_RESPONSE_NORMAL) {amqp_envelope_t envelope;amqp_maybe_release_buffers(conn);if (amqp_consume_message(conn, &envelope, NULL, 0) == AMQP_STATUS_OK) {std::cout << "Received message: " << amqp_bytes_to_string(envelope.message.body, NULL) << std::endl;amqp_destroy_envelope(&envelope);}} else {die_on_error(res, "Waiting for event");}}amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS);amqp_connection_close(conn, AMQP_REPLY_SUCCESS);amqp_destroy_connection(conn);return 0;
}

5. 编译和运行项目

  • 在Windows上,使用Visual Studio编译和运行项目。

  • 在Linux上,使用g++编译器进行编译,并链接相应的库文件。

以上步骤提供了在C++中引入RabbitMQ的基本流程,包括安装服务器、安装客户端库、配置项目、编写代码以及编译运行等。通过这些步骤,你可以在C++项目中实现与RabbitMQ服务器的消息通信。

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词