2.3.2 • Published 6 years ago
shimo-rocketmq v2.3.2
shimo-rocketmq
Aliyun Open Notification Service Client (base on opensource project RocketMQ)
Sub module of ali-sdk.
Install
npm install shimo-rocketmq --save
Usage
consumer
'use strict';
const httpclient = require('urllib');
const Consumer = require('ali-ons').Consumer;
const consumer = new Consumer({
httpclient,
accessKey: 'your-accesskey',
secretKey: 'your-secretkey',
consumerGroup: 'your-consumer-group',
// isBroadcast: true,
});
consumer.subscribe(config.topic, '*', function*(msg) {
console.log(`receive message, msgId: ${msg.msgId}, body: ${msg.body.toString()}`)
});
consumer.on('error', err => console.log(err));
producer
'use strict';
const co = require('co');
const httpclient = require('urllib');
const Producer = require('ali-ons').Producer;
const Message = require('ali-ons').Message;
const producer = new Producer({
httpclient,
accessKey: 'your-accesskey',
secretKey: 'your-secretkey',
producerGroup: 'your-producer-group',
});
producer.ready(() => {
console.log('producer ready');
const msg = new Message('your-topic', // topic
'TagA', // tag
'Hello ONS !!! ' // body
);
// Might be an order id or other key you want
const shardingKey = 'your sharding key goes here'
co(function *() {
// Send normal message, message will delivery averagely into topic queues
let sendResult = yield * producer.send(msg);
// Send sharding message
// Messages will be delivered into queues which depends on sharding key,
// that means the messages with same sharding key will always be deliveried
// into same queue, but you should handle the situation when consumers scaling yourself.
sendResult = yield * producer.send(msg, shardingKey);
});
});
Rocketmq
implemention from yeliex/rocketmq
consumer
const consumer = new Consumer({
namesrvAddr: 'localhost:9876', // for rocket mq
accessKey: 'your-accesskey', // for aliyun-ons
secretKey: 'your-secretkey', // for aliyun-ons
onsAddr: '', // for aliyun-ons
consumerGroup: 'your-consumer-group', // for aliyun-ons
isBroadcast: false, // default is false, that mean messages will be pushed to consumer cluster only once.
});
producer
const producer = new Producer({
namesrvAddr: 'localhost:9876', // for rocket mq
accessKey: 'your-accesskey', // for aliyun-ons
secretKey: 'your-secretkey', // for aliyun-ons
producerGroup: 'your-producer-group', // for aliyun-ons
});