Maple's Blog.

RabbitMQ 客户端(nodejs)

字数统计: 815阅读时长: 4 min
2022/05/27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
class RabbitMQChannel {
constructor (options) {
const {
type,
connect,
channel,
queue = '',
exchange = '',
routingKey = '',
queueOptions = {},
exchangeOptions = {}
} = options;

this.type = type;
this.connect = connect;
this.channel = channel;
this.queue = queue;
this.options = options;
this.exchange = exchange;
this.queueOptions = queueOptions;
this.exchangeOptions = exchangeOptions;
this.routingKey = routingKey;

// const {
// queueOptions = {/* durable: true */},
// exchangeOptions = {/* exclusive: false */}
// } = options;

if (!this.type || !this.connect || !this.channel || !this.queue) {
throw new Error('illegal params');
}

/**
* 设置 prefetch 参数,避免大量消息堆集
*/
if (this.channelOptions.prefetch) {
this.prefetch(this.channelOptions.prefetch);
}
}

/**
* 将任意类型的参数转换为 buffer
* @param {Object|String} buffer msg 参数
* @returns {Buffer} buffer
*/
_handleMsgToBuffer (buffer) {
if (typeof buffer === 'object' && !Buffer.isBuffer(buffer)) {
const hasToString = buffer.toString && buffer.toString !== Object.prototype.toString && !Array.isArray(buffer);
if (hasToString) {
buffer = buffer.toString();
} else {
buffer = JSON.stringify(buffer);
}
}
if (typeof buffer === 'string') {
buffer = Buffer.from(buffer);
}
return buffer;
}

// async _handleAssertChannel () {
// if (!this.assertStatus) {
// await this.channel.assertQueue(this.queue, this.type, this.assertOptions);
// this.assertStatus = true;
// }
// }
/**
* send to queue
*
* 发送数据到队列
* @param {Buffer} buffer 数据
* @param {Object} sendOptions send to queue 参数
*/
sendToQueue (buffer, sendOptions = {/* persistent: false 持久 */}) {
const { queue } = sendOptions;
this.channel.sendToQueue(queue || this.queue, this._handleMsgToBuffer(buffer), sendOptions);
}

publish (buffer, publishOptions = {}) {
let {
routingKey,
exchange
} = publishOptions;

if (Array.isArray(this.routingKey) && this.routingKey.length !== 1 && !routingKey) {
throw new Error('需要额外指定 options.routingKey');
} else {
routingKey = routingKey || this.routingKey[0] || this.routingKey;
}

exchange = exchange || this.exchange;

this.channel.publish(exchange || this.exchange, routingKey, this._handleMsgToBuffer(buffer), publishOptions);
}

async consume (consumer, consumeOptions = { /* noAck: false */}) {
if (!this.assertStatus) {
await this.channel.assertQueue(this.queue, this.type, this.assertOptions);
this.assertStatus = true;
}

this.channel.consume(this.queue, async (msg) => {
if (!msg) {
return;
}

// buffer msg
const msgContent = msg.content;

// consumer handle
await consumer(msgContent, msg);

// 自动 ack
if (!consumeOptions.noAck) {
// ack msg
this.channel.ack(msg);
}
}, consumeOptions);
}

async ackAll () {
// await this._handleAssertChannel();
await this.channel.ackAll();
}

async nack (message, allUpto, requeue) {
// await this._handleAssertChannel();
await this.channel.nack(message, allUpto, requeue);
}

async nackAll (requeue) {
// await this._handleAssertChannel();
await this.channel.nackAll(requeue);
}

prefetch (number) {
if (typeof number === 'number' && number >= 1) {
this.channel.prefetch(parseInt(number));
}
}
}

class RabbitMQ {
constructor (link = 'amqp://localhost') {
const defaultLinkOptions =
{
protocol: 'amqp',
hostname: 'localhost',
port: 5672,
username: 'guest',
password: 'guest',
locale: 'en_US',
frameMax: 0,
heartbeat: 0,
vhost: '/'
};

this.link = typeof link === 'string'
? link
: Object.assign(defaultLinkOptions, link || {});
}

async connet (link) {
const _link = link || this._link;
this.connet = await amqplib.connect(_link);
// return this;
return this.connect();
}

// fanout 绑定 queue 广播
// direct 绑定 queue 发送
// headers
// match
// topic 绑定 queue 和 topic

// 'direct' | 'topic' | 'headers' | 'fanout' | 'match'
async createChannel (
queue = '',
exchange,
type = 'direct',
options = {
queueOptions: {/* durable: true, exclusive: false */},
exchangeOptions: {/* durable: false */}
}
) {
if (typeof exchange === 'string') {
if (['direct', 'topic', 'headers', 'fanout', 'match'].indexOf(exchange) > -1) {
// 替换 options 为 type
if (typeof type === 'object' && !options) {
options = type;
}

type = exchange;
exchange = null;
}
}

const {
queueOptions,
exchangeOptions,
routingKey = ''
} = options;

// 创建 channel
const channel = await this.connet.createChannel();

if (exchange) {
// 如果 queue 默认是 '' 就设置为独占,链接断开后会自动删除
if (queue === '' && queueOptions.exclusive === undefined) {
queueOptions.exclusive = true;
}

// 申明 exchange
await channel.assertExchange(exchange, type, exchangeOptions);

// 申明 queue
const q = await channel.assertQueue(queue, type, queueOptions);

let routingKeys = routingKey;

if (!Array.isArray(routingKeys)) {
routingKeys = [routingKeys || ''];
}

routingKeys.forEach(routingKey => {
// 绑定 queue 和 exchange
channel.bindQueue(q.queue, exchange, routingKey);
});
} else {
await channel.assertQueue(queue, type, queueOptions);
}

// 返回 RabbitMQChannel 参数
return new RabbitMQChannel(type, this.connet, channel, queue, exchange, options);
}

async close () {
if (this.connet) {
this.connet.close();
}
}
}
CATALOG