Step By Step
1、AMQP控制台创建实例
2、创建用户名和密码
3、Node JS SDK 安装
npm install amqplib
4、连接URL配置说明
amqp://{用户名}:{密码}@{endpoint}/{vhost}
4、发送端代码
#!/usr/bin/env node
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://Mj******:Nk******@am******.mq-amqp.cn-beijing-******.aliyuncs.com/demo_vhost', function(error0, connection) {
if (error0) {
throw error0;
}
connection.createChannel(function(error1, channel) {
if (error1) {
throw error1;
}
var queue = 'hello';
var msg = 'Hello World!';
channel.assertQueue(queue, {
durable: false
});
channel.sendToQueue(queue, Buffer.from(msg));
console.log(" [x] Sent %s", msg);
});
setTimeout(function() {
connection.close();
process.exit(0);
}, 500);
});
5、消费端代码
#!/usr/bin/env node
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://Mj******:Nk******@am******.mq-amqp.cn-beijing-******.aliyuncs.com/demo_vhost', function(error0, connection) {
if (error0) {
throw error0;
}
connection.createChannel(function(error1, channel) {
if (error1) {
throw error1;
}
var queue = 'hello';
channel.assertQueue(queue, {
durable: false
});
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", queue);
channel.consume(queue, function(msg) {
console.log(" [x] Received %s", msg.content.toString());
}, {
noAck: true
});
});
});
6、测试效果
7、控制台部分监控