node-amqp 使用fanout发布订阅rabbitmq消息
publisher代码
const amqp = require('amqp');
let option = {
host: 'server-ip',
port: 5672,
login: 'guest',
password: 'guest',
connectionTimeout: 10000,
authMechanism: 'AMQPLAIN',
vhost: '/',
noDelay: true,
ssl: {
enabled: false
}
}
const connection = amqp.createConnection(option);
connection.on('error',function(e){
console.log("Error from amqp: ", e);
})
let default_exchange = {};
connection.on('ready', function(){
default_exchange = connection.exchange('fans',{type:'fanout'}); //创建 fanout 类型的交换机
let q = connection.queue('my-queue');
q.bind(default_exchange,'my-queue');
let qq = connection.queue('qqq');
qq.bind(default_exchange, 'qqq');
setInterval(publish_message, 2000);
})
let count= 0;
publish_message = function() {
let message = {
hello: 'world',
time: Date.now(),
count: count++
};
default_exchange.publish('', message);
return console.log("my-queue message published: " + (JSON.stringify(message)) + " to queue: my-queue");
};
receiver1 代码
const amqp = require('amqp');
let option = {
host: 'server-ip',
port: 5672,
login: 'guest',
password: 'guest',
connectionTimeout: 10000,
authMechanism: 'AMQPLAIN',
vhost: '/',
noDelay: true,
ssl: {
enabled: false
}
}
const connection = amqp.createConnection(option);
connection.on('error',function(e){
console.log("Error from amqp: ", e);
})
connection.on('ready', function(){
connection.queue('my-queue', function(q){
console.log('my-queue is already subscribing');
q.bind('logs','my-queue', function(){
q.subscribe(function(message){
console.log('----receiveMessage: ',message);
})
});
})
})
receiver2 代码
const amqp = require('amqp');
let option = {
host: 'server-ip',
port: 5672,
login: 'guest',
password: 'guest',
connectionTimeout: 10000,
authMechanism: 'AMQPLAIN',
vhost: '/',
noDelay: true,
ssl: {
enabled: false
}
}
const connection = amqp.createConnection(option);
connection.on('error',function(e){
console.log("Error from amqp: ", e);
})
connection.on('ready', function(){
connection.queue('qqq', function(q){
console.log('my-queue is already subscribing');
q.bind('fans','qqq',function(){
q.subscribe(function(message){
console.log('----receiveMessage: ',message);
})
});
})
})
初学,简单测试,理解不深,可能有潜在问题
node-amqp 使用fanout发布订阅rabbitmq消息的更多相关文章
- RabbitMQ学习系列三:.net 环境下 C#代码订阅 RabbitMQ 消息并处理
上一篇已经讲了Rabbitmq如何在Windows平台安装 不懂请移步: RabbitMQ学习系列二:.net 环境下 C#代码使用 RabbitMQ 消息队列 一.理论 .net环境下,C#代码订阅 ...
- Kafka 分布式的,基于发布/订阅的消息系统
Kafka是一种分布式的,基于发布/订阅的消息系统.主要设计目标如下: 通过O(1)的磁盘数据结构提供消息的持久化,这种结构对于即使数以TB的消息存储也能够保持长时间的稳定性能. 高吞吐量:即使是非常 ...
- kafka高吞吐量的分布式发布订阅的消息队列系统
一:kafka介绍kafka(官网地址:http://kafka.apache.org)是一种高吞吐量的分布式发布订阅的消息队列系统,具有高性能和高吞吐率. 1.1 术语介绍BrokerKafka集群 ...
- Publisher/Subscriber(发布/订阅者)消息模式开发流程
该模式的作用是发布者和订阅者 可以相互发送消息 发布者和订阅者都充当 生产者和消费者 发布者 package publisher.to.subscriber; import java.awt.font ...
- rabbitMq及安装、fanout交换机-分发(发布/订阅)
<dependency> <groupId>com.rabbitmq</groupId> <artifactId& ...
- redis实现消息队列&发布/订阅模式使用
在项目中用到了redis作为缓存,再学习了ActiveMq之后想着用redis实现简单的消息队列,下面做记录. Redis的列表类型键可以用来实现队列,并且支持阻塞式读取,可以很容易的实现一个高性 ...
- RabbitMQ 消息队列 应用
安装参考 详细介绍 学习参考 RabbitMQ 消息队列 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. M ...
- RabbitMQ 消息模式
消息模式实例 视频教程:https://ke.qq.com/course/304104 编写代码前,最好先添加好用户并设置virtual hosts 一.简单模式 1.导入jar包 <depen ...
- RabbitMQ消息队列1: Detailed Introduction 详细介绍
1. 历史 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需求,虽然在同步消息通讯的世界里有 ...
随机推荐
- 用CSS实现新闻轮播效果
CSS: /* Make it a marquee */ .marquee { width: 450px;margin: 0 auto;overflow: hidden;white-space: no ...
- UOJ_14_【UER #1】DZY Loves Graph_并查集
UOJ_14_[UER #1]DZY Loves Graph_并查集 题面:http://uoj.ac/problem/14 考虑只有前两个操作怎么做. 每次删除一定是从后往前删,并且被删的边如果不是 ...
- Godot-3D教程-02.3D性能和局限性
介绍 Introduction Godot遵循表现与性能平衡信条.在这个表现的世界中,它们总是有许多约定俗成的东西,主要是在用执行速度换取可用性与扩展性方面.下面是一些实际的例子: 高效地渲染对象是比 ...
- 《c# 实现p2p文件分享与传输系统》 二、 设计 - 续(NAT穿透)
c#实现P2P文件分享与传输系统 二.设计 - 续(NAT穿透) 首先要抱歉,因为这些日子较忙,没有写文章,这个系列拖了很久,现在开始继续. 上一篇文章介绍了p2p系统Tracker Server和 ...
- 解决CentOS 7安装zabbix 3.0 无法启动zabbix-server的问题[segfault at 18 ip 00007f78842b4bd0 sp 00007fff1995a818 error 4 in libpthread-2.17.so[7f78842ab000+16000]]
解决CentOS 7安装zabbix 3.0 无法启动zabbix-server的问题 [root@localhost sbin]# service zabbix-server start Redir ...
- Python3解leetcode Linked List Cycle
问题描述: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given link ...
- JS编写简易计算器
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/html"> <head lang=" ...
- jquery 插件 国外
http://www.jqueryrain.com/demo/jquery-portfolio-gallery-plugin/
- WPF TabControl SelectionChanged 重复执行的问题
很邪门的问题,我曾经都感觉是微软的bug了. 问题是这样的:在我的tabcontrol下的tabitem中有一个combobox控件,由于一些原因,需要执行tabcontrol的SelectionCh ...
- Javascript实现页面左边的菜单选中项高亮显示
在项目开发过程中,遇到一个问题 在一个模板页面中,Layout.cshtml,页面左边放了一个菜单项menu,每一项都是一个链接到一个新的页面.但所有页面都是用这个模板Layout.cshtml.需要 ...