amqplib-init 1.1.9 → 1.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +25 -3
- package/package.json +2 -1
package/index.js
CHANGED
|
@@ -2,10 +2,11 @@ const amqp = require('amqplib');
|
|
|
2
2
|
const shelljs = require('shelljs');
|
|
3
3
|
const log = require('chalk-style')
|
|
4
4
|
const happy = require('happy-help');
|
|
5
|
+
const axios = require('axios'); // 新增 axios 依赖
|
|
5
6
|
|
|
6
7
|
module.exports = {
|
|
7
8
|
// 初始化函数
|
|
8
|
-
init(option) {
|
|
9
|
+
async init(option) { // 改为 async 函数
|
|
9
10
|
const {
|
|
10
11
|
channelName = 'node-test-channel', // 频道名称,默认为'node-test-channel'
|
|
11
12
|
prefetch = 1, // 预取计数,默认为1
|
|
@@ -13,6 +14,7 @@ module.exports = {
|
|
|
13
14
|
callback = () => {}, // 消息处理回调,默认为空函数
|
|
14
15
|
finish = () => {}, // 初始化完成回调,默认为空函数
|
|
15
16
|
amqpLink = '', // RabbitMQ连接地址
|
|
17
|
+
amqpAutoLink = '', // 自动获取连接信息的HTTPS链接
|
|
16
18
|
heartbeat = 5, // 心跳间隔,单位为秒,默认为2秒
|
|
17
19
|
timeout = 2000, // 连接超时时间,单位为毫秒,默认为10000毫秒
|
|
18
20
|
delay = 0, // 消息处理完成后延迟ack的时间,单位为毫秒,默认为0毫秒
|
|
@@ -24,11 +26,31 @@ module.exports = {
|
|
|
24
26
|
let connection = null; // RabbitMQ连接
|
|
25
27
|
let channel = null; // RabbitMQ频道
|
|
26
28
|
|
|
29
|
+
// 处理 amqpAutoLink 自动获取连接信息
|
|
30
|
+
let finalAmqpLink = amqpLink; // 最终使用的连接地址
|
|
31
|
+
if (amqpAutoLink) {
|
|
32
|
+
try {
|
|
33
|
+
log.log(`正在从 ${amqpAutoLink} 获取连接信息...`);
|
|
34
|
+
const response = await axios.post(amqpAutoLink);
|
|
35
|
+
const { info } = response.data;
|
|
36
|
+
|
|
37
|
+
if (info && info.AMQPLIB_USER && info.AMQPLIB_PWD && info.AMQPLIB_IP && info.AMQPLIB_PORT) {
|
|
38
|
+
finalAmqpLink = `amqp://${info.AMQPLIB_USER}:${info.AMQPLIB_PWD}@${info.AMQPLIB_IP}:${info.AMQPLIB_PORT}`;
|
|
39
|
+
log.log(`✅ 自动获取连接信息成功: ${finalAmqpLink}`);
|
|
40
|
+
} else {
|
|
41
|
+
log.error('❌ 获取的连接信息不完整,使用默认连接地址');
|
|
42
|
+
}
|
|
43
|
+
} catch (error) {
|
|
44
|
+
log.error('❌ 获取连接信息失败:', error.message);
|
|
45
|
+
log.log('使用默认连接地址继续...');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
27
49
|
// 重连函数
|
|
28
50
|
const reconnect = async () => {
|
|
29
51
|
log.error('连接丢失,正在尝试重连...');
|
|
30
52
|
await happy.sleep(2); // 重连前等待2秒
|
|
31
|
-
connection = await amqp.connect(
|
|
53
|
+
connection = await amqp.connect(finalAmqpLink, { heartbeat, timeout });
|
|
32
54
|
channel = await connection.createChannel();
|
|
33
55
|
await channel.assertQueue(channelName, { durable });
|
|
34
56
|
await channel.prefetch(prefetch);
|
|
@@ -74,7 +96,7 @@ module.exports = {
|
|
|
74
96
|
// 启动函数
|
|
75
97
|
const start = async () => {
|
|
76
98
|
try {
|
|
77
|
-
connection = await amqp.connect(
|
|
99
|
+
connection = await amqp.connect(finalAmqpLink, { heartbeat, timeout });
|
|
78
100
|
channel = await connection.createChannel();
|
|
79
101
|
await channel.assertQueue(channelName, { durable });
|
|
80
102
|
await channel.prefetch(prefetch);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "amqplib-init",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.10",
|
|
4
4
|
"description": "消息队列初始化",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"license": "ISC",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"amqplib": "^0.10.4",
|
|
13
|
+
"axios": "^1.6.0",
|
|
13
14
|
"chalk-style": "^1.0.3",
|
|
14
15
|
"happy-help": "^1.0.1",
|
|
15
16
|
"js-base64": "^3.7.7",
|