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

网站底部放置备案号wordpress 同步

网站底部放置备案号,wordpress 同步,网站建设制作fash,杭州网站推广服务在 RabbitMQ 中,交换机(Exchange)是一个核心组件,负责接收来自生产者的消息,并根据特定的路由规则将消息分发到相应的队列。交换机的存在改变了消息发送的模式,使得消息的路由更加灵活和高效。 交换机的类…

在 RabbitMQ 中,交换机(Exchange)是一个核心组件,负责接收来自生产者的消息,并根据特定的路由规则将消息分发到相应的队列。交换机的存在改变了消息发送的模式,使得消息的路由更加灵活和高效。

交换机的类型

RabbitMQ 提供了四种主要类型的交换机,每种交换机的路由规则不同:

  1. Direct Exchange(直连交换机)

    • 功能:基于路由键(Routing Key)将消息发送到与该路由键完全匹配的队列。
    • 应用场景:适用于需要精确匹配路由键的场景。
    • 示例:假设有两个队列 A 和 B,A 绑定了路由键 key1,B 绑定了路由键 key2。当生产者发送一条路由键为 key1 的消息时,只有队列 A 会接收到这条消息。
  2. Fanout Exchange(扇出交换机)

    • 功能:将消息广播到所有绑定到该交换机的队列,不考虑路由键。
    • 应用场景:适用于需要将消息广播到多个队列的场景。
    • 示例:假设有两个队列 A 和 B 都绑定到了一个 Fanout 交换机上。当生产者发送一条消息到该交换机时,A 和 B 都会接收到这条消息。
  3. Topic Exchange(主题交换机)

    • 功能:基于路由键的模式匹配(使用通配符)将消息发送到匹配的队列。
    • 应用场景:适用于需要基于模式匹配路由键的场景。
    • 示例:假设有两个队列 A 和 B,A 绑定了路由键模式 key.*,B 绑定了路由键模式 key.#。当生产者发送一条路由键为 key.test 的消息时,A 和 B 都会接收到这条消息。
  4. Headers Exchange(头交换机)

    • 功能:基于消息的头部属性进行匹配,将消息发送到匹配的队列。
    • 应用场景:适用于需要基于消息头部属性进行路由的场景。
    • 示例:这种交换机使用较少,通常在特定情况下才会使用。

交换机的作用

  • 消息路由:交换机根据路由规则将消息分发到相应的队列。
  • 解耦生产者和消费者:生产者只需将消息发送到交换机,不需要知道消息的最终目的地队列。
  • 灵活性和扩展性:通过不同类型的交换机,可以实现复杂的消息路由逻辑,满足各种业务需求。

示例代码

以下是如何使用 Direct Exchange 和 Fanout Exchange 的示例代码:

Direct Exchange 示例
const amqp = require('amqplib/callback_api');amqp.connect('amqp://localhost', function(error0, connection) {if (error0) {throw error0;}connection.createChannel(function(error1, channel) {if (error1) {throw error1;}const exchange = 'direct_logs';const msg = 'Hello World!';const routingKey = 'key1';channel.assertExchange(exchange, 'direct', { durable: true });channel.publish(exchange, routingKey, Buffer.from(msg));console.log(" [x] Sent %s: '%s'", routingKey, msg);});setTimeout(function() {connection.close();process.exit(0);}, 500);
});
Fanout Exchange 示例
const amqp = require('amqplib/callback_api');amqp.connect('amqp://localhost', function(error0, connection) {if (error0) {throw error0;}connection.createChannel(function(error1, channel) {if (error1) {throw error1;}const exchange = 'logs';const msg = 'Hello World!';channel.assertExchange(exchange, 'fanout', { durable: true });channel.publish(exchange, '', Buffer.from(msg));console.log(" [x] Sent %s", msg);});setTimeout(function() {connection.close();process.exit(0);}, 500);
});
Topic Exchange 示例

Topic Exchange 允许使用通配符进行路由,支持更复杂的路由规则。

发布者代码
const amqp = require('amqplib/callback_api');amqp.connect('amqp://localhost', function(error0, connection) {if (error0) {throw error0;}connection.createChannel(function(error1, channel) {if (error1) {throw error1;}const exchange = 'topic_logs';const msg = 'Hello World!';const routingKey = 'quick.orange.rabbit';channel.assertExchange(exchange, 'topic', { durable: true });channel.publish(exchange, routingKey, Buffer.from(msg));console.log(" [x] Sent %s: '%s'", routingKey, msg);});setTimeout(function() {connection.close();process.exit(0);}, 500);
});
消费者代码
const amqp = require('amqplib/callback_api');amqp.connect('amqp://localhost', function(error0, connection) {if (error0) {throw error0;}connection.createChannel(function(error1, channel) {if (error1) {throw error1;}const exchange = 'topic_logs';const queue = 'topic_queue';channel.assertExchange(exchange, 'topic', { durable: true });channel.assertQueue(queue, { durable: true });// 绑定队列到交换机,使用通配符channel.bindQueue(queue, exchange, '*.orange.*');channel.consume(queue, function(msg) {if (msg.content) {console.log(" [x] Received %s: '%s'", msg.fields.routingKey, msg.content.toString());}}, { noAck: true });});
});

在这个示例中,发布者将消息发送到 topic_logs 交换机,使用路由键 quick.orange.rabbit。消费者绑定到 topic_logs 交换机,使用通配符 *.orange.*,因此会接收到所有包含 orange 的消息。

Headers Exchange 示例

Headers Exchange 基于消息头部属性进行路由,适用于需要复杂路由规则的场景。

发布者代码
const amqp = require('amqplib/callback_api');amqp.connect('amqp://localhost', function(error0, connection) {if (error0) {throw error0;}connection.createChannel(function(error1, channel) {if (error1) {throw error1;}const exchange = 'headers_logs';const msg = 'Hello World!';channel.assertExchange(exchange, 'headers', { durable: true });channel.publish(exchange, '', Buffer.from(msg), {headers: {'format': 'pdf','type': 'report'}});console.log(" [x] Sent %s", msg);});setTimeout(function() {connection.close();process.exit(0);}, 500);
});
消费者代码
const amqp = require('amqplib/callback_api');amqp.connect('amqp://localhost', function(error0, connection) {if (error0) {throw error0;}connection.createChannel(function(error1, channel) {if (error1) {throw error1;}const exchange = 'headers_logs';const queue = 'headers_queue';channel.assertExchange(exchange, 'headers', { durable: true });channel.assertQueue(queue, { durable: true });// 绑定队列到交换机,使用头部属性channel.bindQueue(queue, exchange, '', {'x-match': 'all','format': 'pdf','type': 'report'});channel.consume(queue, function(msg) {if (msg.content) {console.log(" [x] Received %s", msg.content.toString());}}, { noAck: true });});
});

在这个示例中,发布者将消息发送到 headers_logs 交换机,并设置消息头部属性 format: pdftype: report。消费者绑定到 headers_logs 交换机,使用头部属性匹配 format: pdftype: report,因此会接收到符合这些头部属性的消息。

http://www.yayakq.cn/news/903944/

相关文章:

  • 网站修改影响做百度竞价吗wordpress首页热门排行显示
  • 中山h5模板建站企业oa系统手机版下载
  • 济南市住房和城乡建设局官方网站google浏览器网页版
  • 营销型的物流网站模板简体中文wordpress5.2下载
  • 怎么查询网站开发时间北京工商注册代理公司
  • 上海市建设质量协会网站腾讯网站建设费用
  • 诛仙3官方网站时竹任务荧灵怎么做教育网站制作多少费用
  • 合肥网页制作公司推荐专业的网站优化公司排名
  • html5 网站推荐桂林网站
  • 海宁营销型网站设计wordpress 部署报错
  • 移动网站源码wordpress模板文件是那个文件
  • 服装网站建设中期目标wordpress导航怎么设置主页
  • 贸易网站建设方案网站权重如何提高
  • 国学大师网站是哪里做的分析网站建设到运营需要多少钱
  • asp网站空间申请网站开发用什么语言开发的
  • 东莞市工程建设安监站网站网站访客qq获取原理
  • 怎么做网站手机版简单的个人网站制作流程
  • 手机网站建设优势有谁做彩票网站吗
  • 做打折网站如何长沙网站seo技巧
  • 专业外贸网站建设前端开发语言有哪几种
  • 养生类网站源码安徽网站建设seo优化
  • 做兼职的网站策划书网络工程师官网
  • 温州营销网站公司注册一家公司需要多少钱
  • 大网站前端怎么做的中国纪检监察报数字报
  • 类似千图网的素材网站wordpress po mo
  • 四川省肿瘤医院河北百度seo关键词排名
  • 怎么选择赣州网站建设宁波做网站seo的
  • 百度收录排名好的网站淘宝网店运营
  • 扬州市城市建设监察支队网站网站怎么伪静态网站
  • 网站建设对教育解决方案建设工程考试官方网站