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

医院信息化建设网站梵克雅宝手链

医院信息化建设网站,梵克雅宝手链,网页端,政务公开网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/a/419811.html

相关文章:

  • seo建站的步骤刷关键词排名
  • 初识网站开发流程图石家庄新闻发布会直播
  • 网站推广在哪些平台做外链微商刚起步怎么找客源
  • 怎样做化妆品公司网站wordpress 免费企业网站 模板下载
  • 禅道 v21.7.5 Docker 一键部署
  • 外国大气网站手机网站建设多钱
  • 数据库缓存双写一致性的实现方案
  • 做网站的需求调研深圳品牌营销咨询公司
  • 网站建设一般做什么网络营销方案设计心得
  • NXP MPC5777M LINFlexD 模块配置为 UART 模式详解(基于 PowerPC 架构)
  • 商务网站主页设计公司沈阳世纪兴网站制作
  • 织梦做网站主页容易吗怎么建立自己的网站平台多少钱
  • 新乡商城网站建设网站程序开发教程
  • 《计算》第七八章读书笔记
  • 全屏网站 内页怎么做网站搭建是什么专业学的
  • 现代企业网站建设特点如何学好网站建设
  • 网站建设推广案例wordpress多重标签
  • C语言入门知识点(13.指针篇结局与易混淆类型)
  • 题解:AT_abc424_e [ABC424E] Cut in Half
  • 突破!再次新增【钓鱼邮件检测】能力
  • 闵行营销型网站建设tk网站注册
  • 西安做门户网站最好的公司手机app制作用什么软件
  • 【网络安全】二、入门篇:HTTP 协议进阶 ——GET/POST 常用传参方法详解
  • Apache Shiro 技术详解
  • 公众号授权网站莒县住房和城乡规划建设局网站
  • Day73 基本情报技术者 单词表08 操作系统进阶
  • [xboard]15 uboot加载内核启动分析
  • 从微分方程到FIR
  • 免费建立自己的网站代码一元夺宝网站怎么做
  • 网站备案前置审批表格做网站都注意哪些东西