egg plugin for RabbitMQ
$ npm i egg-rmq --save
// {app_root}/config/plugin.js
exports.rmq = {
enable: true,
package: 'egg-rmq',
};
// {app_root}/config/config.default.js
exports.rmq = {
host: 'amqp://localhost',
};
// {app_root}/config/config.default.js
exports.rmq = {
servers: {
test: {
host: 'amqp://localhost',
},
},
};
// {app_root}/config/config.default.js
// If not set name then this config like Single Server, use app.rmq.conn
exports.rmq = {
servers: [
{
host: 'amqp://localhost',
},
{
name: 'test',
host: 'amqp://localhost2',
},
],
};
use rmq.route to set Single Server channel
use rmq[server name].route to set Multiple Server channel
// {app_root}/router.js
module.exports = app => {
const { router, controller, rmq } = app;
rmq.route('hello', rmq.controller.listener.test); // hello is queue
};
use ctx.req.ch to get Channel
use ctx.req.queue to get Queue
use app.rmq.conn to get Single Server connect
use app.rmq[server name].conn to get Multiple Server connect
// {app_root}/rmq/controller/hello.js
module.exports = app => {
return class TestController extends app.Controller {
async test() {
const { ctx } = this;
await ctx.req.ch.assertQueue(ctx.req.queue, { durable: false });
ctx.req.ch.consume(ctx.req.queue, msg => {
console.log(' [x] Received %s', msg.content.toString());
console.log(msg.properties.headers, '#hello headers');
}, { noAck: true });
ctx.req.ch.sendToQueue(ctx.req.queue, Buffer.from('Nodejs'), { headers: { user: 'user test' } });
}
};
};
see config/config.default.js for more detail.
Please open an issue here.