阿里云AMQP NodeJS SDK使用Demo

Step By Step

1、AMQP控制台创建实例
阿里云AMQP NodeJS SDK使用Demo
阿里云AMQP NodeJS SDK使用Demo
阿里云AMQP NodeJS SDK使用Demo

2、创建用户名和密码
阿里云AMQP NodeJS SDK使用Demo
阿里云AMQP NodeJS SDK使用Demo
阿里云AMQP NodeJS SDK使用Demo

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、测试效果
阿里云AMQP NodeJS SDK使用Demo

7、控制台部分监控
阿里云AMQP NodeJS SDK使用Demo
阿里云AMQP NodeJS SDK使用Demo

更多参考

阿里云常见参数获取位置
rabbitmq/rabbitmq-tutorials
创建静态用户名密码

上一篇:Module切换,如何实现loading效果


下一篇:在Web上登录Domino后直接打开用户的邮件