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

山东城市建设职业学院官方网站合肥瑶海区房价

山东城市建设职业学院官方网站,合肥瑶海区房价,网页游戏排行榜奇迹,汉阳网站推广公司exhcange交换机类型fanout和direct对比总结 特性Fanout 交换机Direct 交换机(默认类型)消息路由方式广播,忽略路由键精确匹配路由键绑定关系所有绑定到该交换机的队列都接收消息只有路由键匹配的队列接收消息使用场景日志系统、事件通知任务…

exhcange交换机类型fanout和direct对比总结

特性Fanout 交换机Direct 交换机(默认类型)
消息路由方式广播,忽略路由键精确匹配路由键
绑定关系所有绑定到该交换机的队列都接收消息只有路由键匹配的队列接收消息
使用场景日志系统、事件通知任务分配、错误处理
示例所有服务都需要接收相同的日志或通知不同类型的日志或任务被路由到不同队列

示例一 

使用Fanout 交换机实现广播,它不考虑任何路由键(routing key),而是将接收到的消息广播到所有绑定到该交换机的队列中。

producer.ts 发送消息,exchange类型fanout,队列和消息没有设置持久化,如果有消费者会接收到数据,没有消费者数据会丢失,重启rabbitmq数据会丢失。

import amqp from 'amqplib/callback_api';amqp.connect('amqp://admin:admin1234@localhost:5672', function (error0, connection) {if (error0) {throw error0;}connection.createChannel(function (error1, channel) {if (error1) {throw error1;}var exchange = 'exchange3';var msg = 'Hello World!';channel.assertExchange(exchange, 'fanout', {durable: false});channel.publish(exchange, '', Buffer.from(msg));console.log(" [x] Sent %s", msg);});setTimeout(function () {connection.close();process.exit(0);}, 500);
});

consumer.ts

import amqp from 'amqplib/callback_api';amqp.connect('amqp://admin:admin1234@localhost:5672', function (error0, connection) {if (error0) {throw error0;}connection.createChannel(function (error1, channel) {if (error1) {throw error1;}var exchange = 'exchange3';channel.assertExchange(exchange, 'fanout', {durable: false});channel.assertQueue('', {exclusive: true}, function (error2, q) {if (error2) {throw error2;}console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q.queue);channel.bindQueue(q.queue, exchange, '');channel.consume(q.queue, function (msg) {if (msg?.content) {console.log(" [x] %s", msg.content.toString());}}, {noAck: true});});});
});

 示例二

使用direct类型交换机,只有路由键匹配的队列会接收到消息(精确匹配),适合需要根据特定条件(如路由键)将消息路由到特定队列的场景,如任务分配、错误处理等。

producer.ts 发送消息routeKey指定为了route.key,设置了队列和消息持久化,publish发送时指定了route.key。

import RabbitMQ from 'amqplib/callback_api';function start() {RabbitMQ.connect("amqp://admin:admin1234@localhost:5672?heartbeat=60", function (err0, conn) {if (err0) {console.error("[AMQP]", err0.message);return setTimeout(start, 1000);}conn.on("error", function (err1) {if (err1.message !== "Connection closing") {console.error("[AMQP] conn error", err1.message);}});conn.on("close", function () {console.error("[AMQP] reconnecting");return setTimeout(start, 1000);});console.log("[AMQP] connected");conn.createChannel(async (err2, channel) => {if (err2) {console.error("[AMQP]", err2.message);return setTimeout(start, 1000);}const exchangeName = 'exchange1';channel.assertExchange(exchangeName,'direct',{durable: true},(err, ok) => {if (err) {console.log('exchange路由转发创建失败', err);} else {console.log('exchange路由转发创建成功', ok);for (let i = 0; i < 10; ++i) {// 给单个队列发送消息// console.log('message send!', channel.sendToQueue(//   queueName,//   Buffer.from(`发送消息,${i}${Math.ceil(Math.random() * 100000)}`),//   { persistent: true, correlationId: 'ooooooooooooooo' },// 消息持久化,重启后存在。//   // (err: any, ok: Replies.Empty)=>{}// ));console.log('消息发送是否成功', channel.publish(exchangeName,'route.key',Buffer.from(`发送消息,${i}${Math.ceil(Math.random() * 100000)}`),{ persistent: true },));}}});});setTimeout(() => {conn.close();process.exit(0);}, 1000);});
}start();

consumer.ts 绑定了exchange并指定了route.key,可以接收到生产者发送的消息。

import RabbitMQ, { type Replies } from 'amqplib/callback_api';RabbitMQ.connect('amqp://admin:admin1234@localhost:5672', (err0, conn) => {if (err0) {console.error(err0);return;}conn.createChannel(function (err1, channel) {const queueName = 'queue1';channel.assertQueue(queueName, { durable: true }, (err2, ok) => {if (err2) {console.log('队列创建失败', err2);return;}console.log('[*] waiting...');// 一次只有一个未确认消息,防止消费者过载channel.prefetch(1);channel.bindQueue(queueName, 'exchange1', 'route.key', {}, (err3, ok) => {console.log(queueName, '队列绑定结果', err3, ok);});channel.consume(queueName, function (msg) {console.log('接收到的消息', msg?.content.toString());// 手动确认取消channel.ack(msg);设置noAck:false,// 自动确认消息noAck:true,不需要channel.ack(msg);try {if (msg) {channel.ack(msg);}} catch (err) {if (msg) {// 第二个参数,false拒绝当前消息// 第二个参数,true拒绝小于等于当前消息// 第三个参数,3false从队列中清除// 第三个参数,4true从新在队列中排队channel.nack(msg, false, false);}console.log(err);}}, {// noAck: true, // 是否自动确认消息,为true不需要调用channel.ack(msg);noAck: false});});});conn.on("error", function (err1) {if (err1.message !== "Connection closing") {console.error("[AMQP] conn error", err1.message);}});conn.on("close", function () {console.error("[AMQP] reconnecting");});
});

 consumer2.ts

import RabbitMQ, { type Replies } from 'amqplib/callback_api';RabbitMQ.connect('amqp://admin:admin1234@localhost:5672', (err0, conn) => {if (err0) {console.error(err0);return;}conn.createChannel(function (err1, channel) {const queueName = 'queue2';channel.assertQueue(queueName, { durable: true }, (err2, ok) => {if (err2) {console.log('队列创建失败', err2);return;}console.log('[*] waiting...');// 一次只有一个未确认消息,防止消费者过载channel.prefetch(1);channel.bindQueue(ok.queue, 'exchange1', 'route.key', {}, (err3, ok) => {console.log(queueName, '队列绑定结果', err3, ok);});channel.consume(ok.queue, function (msg) {console.log('接收到的消息', msg?.content.toString());// 手动确认取消channel.ack(msg);设置noAck:false,// 自动确认消息noAck:true,不需要channel.ack(msg);try {if (msg) {channel.ack(msg);}} catch (err) {if (msg) {// 第二个参数,false拒绝当前消息// 第二个参数,true拒绝小于等于当前消息// 第三个参数,3false从队列中清除// 第三个参数,4true从新在队列中排队channel.nack(msg, false, false);}console.log(err);}}, {// noAck: true, // 是否自动确认消息,为true不需要调用channel.ack(msg);noAck: false});});});conn.on("error", function (err1) {if (err1.message !== "Connection closing") {console.error("[AMQP] conn error", err1.message);}});conn.on("close", function () {console.error("[AMQP] reconnecting");});
});

consumer3.ts

import RabbitMQ, { type Replies } from 'amqplib/callback_api';RabbitMQ.connect('amqp://admin:admin1234@localhost:5672', (err0, conn) => {if (err0) {console.error(err0);return;}conn.createChannel(function (err1, channel) {const queueName = 'queue3';channel.assertQueue(queueName, { durable: true }, (err2, ok) => {if (err2) {console.log('队列创建失败', err2);return;}console.log('[*] waiting...');// 一次只有一个未确认消息,防止消费者过载channel.prefetch(1);// 绑定的routeKey不同不会接收到route.key发送的消息channel.bindQueue(queueName, 'exchange1', 'routeKey', {}, (err3, ok) => {console.log(queueName, '队列绑定结果', err3, ok);});channel.consume(queueName, function (msg) {console.log('接收到的消息', msg?.content.toString());// 手动确认取消channel.ack(msg);设置noAck:false,// 自动确认消息noAck:true,不需要channel.ack(msg);try {if (msg) {channel.ack(msg);}} catch (err) {if (msg) {// 第二个参数,false拒绝当前消息// 第二个参数,true拒绝小于等于当前消息// 第三个参数,3false从队列中清除// 第三个参数,4true从新在队列中排队channel.nack(msg, false, false);}console.log(err);}}, {// noAck: true, // 是否自动确认消息,为true不需要调用channel.ack(msg);noAck: false});});});conn.on("error", function (err1) {if (err1.message !== "Connection closing") {console.error("[AMQP] conn error", err1.message);}});conn.on("close", function () {console.error("[AMQP] reconnecting");});
});

 

http://www.dtcms.com/wzjs/155903.html

相关文章:

  • 百度关键词热度排名百度竞价优化软件
  • 新乐网站建设seo与sem的关系
  • 武汉 网站建设公司中山网站建设
  • 企业做网站有什么好处免费推广的网站有哪些
  • 市场营销策略范文手机流畅优化软件
  • 厦门网站定制开发长沙关键词优化方法
  • 犀牛做网站的公司网络推广赚钱
  • 网站如何做国外推广福州seo按天收费
  • 香港服务器做违规网站有必要买优化大师会员吗
  • 淘宝建站服务最能打动顾客的十句话
  • 网站做排名靠前个人接外包的网站
  • 城厢区住房和城乡建设局网站黑龙江今日新闻
  • 主流网站开发采用整合营销方案怎么写
  • 经营性质的网站广州今日刚刚发生的新闻
  • 网站建设宽度一般都是多少钱十大互联网广告公司
  • 网站浏览器兼容性问题吗业务推广公司
  • 网站建设技术大全最近热点新闻事件2023
  • 城乡建设厅网站首页百度推广登陆平台登录
  • 做查询新生寝室的网站足球世界排名
  • ip库网站源码免费seo工具汇总
  • 高埗做网站公司怎么做一个网站页面
  • 温岭公司做网站自媒体怎么入门
  • 在线股票交易网站开发洛阳seo外包公司费用
  • 上海专业做网站公司东莞seo外包公司哪家好
  • 做政府网站预算网站优化的主要内容
  • 株洲关键词优化优化大师官方
  • 文网站建设视频外链工具
  • foxtable网站开发网络营销做得好的企业有哪些
  • 信誉好的顺德网站建设网上打广告有哪些软件
  • PHP套模板做网站怎么在百度上做广告