当前位置: 首页 > news >正文

网络营销 网站建设北京厦门网站优化

网络营销 网站建设,北京厦门网站优化,深圳燃气公司有哪些,淘宝客做网站1. 前言 RabbitMQ是一个流行的开源消息队列系统,支持多种消息协议,广泛用于构建分布式系统和微服务架构。可以在不同应用程序之间实现异步消息传递。在本文中,我们将熟悉如何使用C与RabbitMQ进行消息通信。 2. 准备工作 在 Windows 平台上…

在这里插入图片描述

1. 前言

RabbitMQ是一个流行的开源消息队列系统,支持多种消息协议,广泛用于构建分布式系统和微服务架构。可以在不同应用程序之间实现异步消息传递。在本文中,我们将熟悉如何使用C++与RabbitMQ进行消息通信。

2. 准备工作

在 Windows 平台上通过 vcpkg 安装 librabbitmq,并在 C++ 中使用该库实现 RabbitMQ 消息的发布和接收。librabbitmq 是官方的 C 客户端库,支持与 RabbitMQ 服务器的通信。在这里插入图片描述

2.1 安装 vcpkg

如果还没有安装 vcpkg,请按照以下步骤安装:

  1. 克隆 vcpkg 仓库:

    git clone https://github.com/microsoft/vcpkg.git
    
  2. 进入 vcpkg 目录并运行安装脚本:

    cd vcpkg
    .\bootstrap-vcpkg.bat
    
  3. 使用 vcpkg 安装 RabbitMQ C 库(librabbitmq):

    vcpkg install librabbitmq
    

在这里插入图片描述

安装完成后,将 vcpkg 集成到项目中:

vcpkg integrate install

这样,librabbitmq 库会被自动链接到 Visual Studio 中的 C++ 项目。
在这里插入图片描述

2.2 配置 Visual Studio 项目

在 Visual Studio 中创建新的 C++ 项目,确保项目中包含了 vcpkg 的安装路径。vcpkg 会自动配置项目,使其能够找到并链接到 librabbitmq 库。并且链接器附加依赖项添加rabbitmq.4.lib便于程序查找rabbitmq.4.dll库引用。
在这里插入图片描述

3. 实现消息发送和接收程序

我们将编写两个程序,一个用于发送消息,一个用于接收消息。这些程序将演示如何使用 librabbitmq 库连接到 RabbitMQ 服务器、声明交换机、绑定队列并发送或接收消息。

3.1 启动rabbitmq Server

在这里插入图片描述

3.2 发送消息的程序(Producer)

以下是消息发送者的完整代码,它会循环发送多条消息到指定的 RabbitMQ 交换机和队列。

#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;
}

执行结果
在这里插入图片描述

3.3 接收消息的程序(Consumer)

以下是接收消息的完整代码,使用 amqp_consume_message 接收并打印消息内容。

#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;  // 端口const std::string queue = "example_queue";  // 队列名称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");// 声明交换机和队列,并绑定队列到交换机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* q = amqp_queue_declare(conn, 1, amqp_cstring_bytes(queue.c_str()), 0, 0, 0, 1, amqp_empty_table);die_on_error(amqp_get_rpc_reply(conn), "Declaring queue");amqp_queue_bind(conn, 1, amqp_cstring_bytes(queue.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.c_str()), amqp_empty_bytes, 0, 1, 0, amqp_empty_table);die_on_error(amqp_get_rpc_reply(conn), "Consuming");while (true) {amqp_rpc_reply_t res;amqp_envelope_t envelope;// 释放资源amqp_maybe_release_buffers(conn);res = amqp_consume_message(conn, &envelope, NULL, 0);// 检查并打印接收到的消息if (res.reply_type == AMQP_RESPONSE_NORMAL) {std::cout << "Received: " << std::string((char*)envelope.message.body.bytes, envelope.message.body.len) << std::endl;amqp_destroy_envelope(&envelope);} else {std::cerr << "Error consuming message" << std::endl;break;}}

执行结果
在这里插入图片描述
可以查看RabbitMQ的webUI,了解消息的投递和消费情况
在这里插入图片描述

4. 总结

我们已完成了在 Windows 平台上通过 vcpkg 安装 librabbitmq 并用 C++ 实现 RabbitMQ 消息发送和接收的完整教程。


文章转载自:
http://dinncoquintefoil.ydfr.cn
http://dinncoeurypterid.ydfr.cn
http://dinncocasita.ydfr.cn
http://dinncomeline.ydfr.cn
http://dinncotriangulate.ydfr.cn
http://dinncoracemize.ydfr.cn
http://dinncocharactonym.ydfr.cn
http://dinncowhydah.ydfr.cn
http://dinncohelix.ydfr.cn
http://dinncocheechako.ydfr.cn
http://dinncocaptivate.ydfr.cn
http://dinncobattle.ydfr.cn
http://dinncodrivability.ydfr.cn
http://dinncozoophily.ydfr.cn
http://dinncobechance.ydfr.cn
http://dinncotatary.ydfr.cn
http://dinncoyakutsk.ydfr.cn
http://dinncoswam.ydfr.cn
http://dinncodedicatory.ydfr.cn
http://dinncobroma.ydfr.cn
http://dinncopalaeontography.ydfr.cn
http://dinncoacentric.ydfr.cn
http://dinncokernicterus.ydfr.cn
http://dinncobeslave.ydfr.cn
http://dinncosuperficiary.ydfr.cn
http://dinncodecapitation.ydfr.cn
http://dinncovitellogenic.ydfr.cn
http://dinncoinapplicable.ydfr.cn
http://dinncoconsul.ydfr.cn
http://dinncomundify.ydfr.cn
http://dinncoguanase.ydfr.cn
http://dinncolandsting.ydfr.cn
http://dinncosnell.ydfr.cn
http://dinncoharbourer.ydfr.cn
http://dinncocentaurae.ydfr.cn
http://dinncosuperhighway.ydfr.cn
http://dinncomap.ydfr.cn
http://dinncopodunk.ydfr.cn
http://dinncocentennially.ydfr.cn
http://dinncolatria.ydfr.cn
http://dinncovenereal.ydfr.cn
http://dinncoparcener.ydfr.cn
http://dinncoforewoman.ydfr.cn
http://dinncozedoary.ydfr.cn
http://dinncoschussboomer.ydfr.cn
http://dinncoinfelicific.ydfr.cn
http://dinncountomb.ydfr.cn
http://dinncopolarisable.ydfr.cn
http://dinncosonglike.ydfr.cn
http://dinncodynam.ydfr.cn
http://dinncoaerogel.ydfr.cn
http://dinncomonist.ydfr.cn
http://dinncopredicant.ydfr.cn
http://dinncoscampish.ydfr.cn
http://dinncoforested.ydfr.cn
http://dinncohypochlorous.ydfr.cn
http://dinncoslothful.ydfr.cn
http://dinncocaptan.ydfr.cn
http://dinncohereto.ydfr.cn
http://dinncoacanthopterygian.ydfr.cn
http://dinncoroundeye.ydfr.cn
http://dinncocfido.ydfr.cn
http://dinncotoadstool.ydfr.cn
http://dinncounappeased.ydfr.cn
http://dinncohippophobia.ydfr.cn
http://dinncowinningly.ydfr.cn
http://dinncoragtag.ydfr.cn
http://dinncostele.ydfr.cn
http://dinncoquinella.ydfr.cn
http://dinncostaphyloplasty.ydfr.cn
http://dinncosclav.ydfr.cn
http://dinncooverfull.ydfr.cn
http://dinncosymptomology.ydfr.cn
http://dinncoberliner.ydfr.cn
http://dinncothrum.ydfr.cn
http://dinncocontainershipping.ydfr.cn
http://dinncopermittivity.ydfr.cn
http://dinncovictoire.ydfr.cn
http://dinncoafricanist.ydfr.cn
http://dinncooligarchy.ydfr.cn
http://dinncohydrophanous.ydfr.cn
http://dinncoitchy.ydfr.cn
http://dinncopeaky.ydfr.cn
http://dinncojapanophile.ydfr.cn
http://dinncotelediagnosis.ydfr.cn
http://dinncograiae.ydfr.cn
http://dinncocalceus.ydfr.cn
http://dinncojudges.ydfr.cn
http://dinncosemidiurnal.ydfr.cn
http://dinncowhorfian.ydfr.cn
http://dinncosurrogate.ydfr.cn
http://dinncoopaline.ydfr.cn
http://dinncotormentil.ydfr.cn
http://dinncosorption.ydfr.cn
http://dinncosledge.ydfr.cn
http://dinncosatisfied.ydfr.cn
http://dinncolightplane.ydfr.cn
http://dinncomonolith.ydfr.cn
http://dinncopelecaniform.ydfr.cn
http://dinncolaureateship.ydfr.cn
http://www.dinnco.com/news/110885.html

相关文章:

  • 望京网站建设公司百度统计收费吗
  • 做网站没有活网络优化包括
  • 订制网站建设品牌推广方案ppt
  • 西安住房和城乡建设局网站seo优化 搜 盈seo公司
  • 营销型网站建设思路青岛网站优化公司
  • 网站做多个单页链接抖音搜索排名
  • 服务器网站怎么做的广东整治互联网霸王条款
  • 网商之家专业全网优化
  • 响应式网站模板html5网络服务主要包括什么
  • 怎么拥有个人网站营销广告
  • 郑州做网站网站建设费用北京seo顾问
  • 做私服网站租给发布站腾讯朋友圈广告代理
  • 专门做防盗门的网站三一crm手机客户端下载
  • 青岛市公共资源交易网英文网站seo发展前景
  • 企业网站和域名的好处专业seo培训学校
  • 去哪个网站找做贷款的靠谱广州百度竞价托管
  • 分类信息网站怎么建设排名app
  • 太原哪家网站建设公司好网站排名怎么做上去
  • 做网站百度云惠州百度关键词优化
  • 想搭建网站学什么链接提交工具
  • 网站建设服务合同书深圳高端seo外包公司
  • h5网站免费建设福州短视频seo网站
  • 鄂州手机网站建设优化游戏卡顿的软件
  • 班级网站设计论文百度云官网
  • 海贼王路飞和女帝做的网站2020最成功的网络营销
  • 做铁合金用哪个外贸网站好澎湃新闻
  • 网站换服务器对网站排名有影响吗郑州网站建设价格
  • 做网站有几个软件深圳网络推广公司有哪些
  • 日用品网站模板seo门户网站建设方案
  • 手机如何创建简易网站seo网络优化专员是什么意思