以下是“详解Node.js amqplib 连接 Rabbit MQ最佳实践”的完整攻略,包含两条示例说明。
示例1:使用amqplib连接RabbitMQ
步骤1:安装amqplib
使用npm安装amqplib:
npm install amqplib
步骤2:连接RabbitMQ
const amqp = require('amqplib');
async function connect() {
try {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
console.log('Connected to RabbitMQ');
} catch (error) {
console.error(error);
}
}
connect();
步骤3:创建队列
const amqp = require('amqplib');
async function connect() {
try {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'hello';
await channel.assertQueue(queue);
console.log('Queue created: ', queue);
} catch (error) {
console.error(error);
}
}
connect();
步骤4:发送消息
const amqp = require('amqplib');
async function connect() {
try {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'hello';
const message = 'Hello World!';
await channel.assertQueue(queue);
channel.sendToQueue(queue, Buffer.from(message));
console.log('Message sent: ', message);
} catch (error) {
console.error(error);
}
}
connect();
步骤5:接收消息
const amqp = require('amqplib');
async function connect() {
try {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'hello';
await channel.assertQueue(queue);
console.log('Waiting for messages...');
channel.consume(queue, (message) => {
console.log('Message received: ', message.content.toString());
});
} catch (error) {
console.error(error);
}
}
connect();
示例2:使用amqplib实现工作队列
步骤1:安装amqplib
同示例1。
步骤2:连接RabbitMQ
同示例1。
步骤3:创建队列
const amqp = require('amqplib');
async function connect() {
try {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'task_queue';
await channel.assertQueue(queue, { durable: true });
console.log('Queue created: ', queue);
} catch (error) {
console.error(error);
}
}
connect();
步骤4:发送消息
const amqp = require('amqplib');
async function connect() {
try {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'task_queue';
const message = 'Hello World!';
await channel.assertQueue(queue, { durable: true });
channel.sendToQueue(queue, Buffer.from(message), { persistent: true });
console.log('Message sent: ', message);
} catch (error) {
console.error(error);
}
}
connect();
步骤5:接收消息
const amqp = require('amqplib');
async function connect() {
try {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'task_queue';
await channel.assertQueue(queue, { durable: true });
channel.prefetch(1);
console.log('Waiting for messages...');
channel.consume(queue, (message) => {
const content = message.content.toString();
console.log('Message received: ', content);
setTimeout(() => {
channel.ack(message);
console.log('Message processed: ', content);
}, 5000);
});
} catch (error) {
console.error(error);
}
}
connect();
以上就是使用amqplib连接RabbitMQ的完整攻略,包含两个示例说明。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:详解Node.js amqplib 连接 Rabbit MQ最佳实践 - Python技术站