-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti-pool-example.js
More file actions
271 lines (223 loc) · 7.49 KB
/
multi-pool-example.js
File metadata and controls
271 lines (223 loc) · 7.49 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* 多连接池使用示例
*
* 演示如何配置和使用多连接池功能
*
* @since v1.0.8
*/
const MonSQLize = require('../lib/index');
async function example1_basicMultiPool() {
console.log('\n========== 示例1: 基础多连接池配置 ==========\n');
const msq = new MonSQLize({
type: 'mongodb',
databaseName: 'myapp',
// 多连接池配置
pools: [
{
name: 'primary',
uri: 'mongodb://primary.example.com:27017/myapp',
role: 'primary',
options: { maxPoolSize: 50 },
weight: 1,
default: true // 默认连接池
},
{
name: 'secondary-1',
uri: 'mongodb://secondary1.example.com:27017/myapp',
role: 'secondary',
options: { maxPoolSize: 100 },
weight: 2 // 权重2,接收更多请求
},
{
name: 'secondary-2',
uri: 'mongodb://secondary2.example.com:27017/myapp',
role: 'secondary',
options: { maxPoolSize: 100 },
weight: 1 // 权重1
},
{
name: 'analytics',
uri: 'mongodb://analytics.example.com:27017/myapp',
role: 'analytics',
options: { maxPoolSize: 20 },
tags: ['reporting', 'batch']
}
],
// 连接池选择策略
poolStrategy: 'auto', // 自动(读写分离 + 负载均衡)
// 故障转移配置
poolFallback: {
enabled: true,
retryDelay: 1000,
maxRetries: 3,
fallbackStrategy: 'readonly' // 只读模式降级
},
// 连接池数量上限
maxPoolsCount: 10
});
await msq.connect();
console.log('✅ 多连接池已初始化');
console.log('连接池列表:', msq.getPoolNames());
await msq.close();
}
async function example2_dynamicPoolManagement() {
console.log('\n========== 示例2: 动态连接池管理 ==========\n');
const msq = new MonSQLize({
type: 'mongodb',
databaseName: 'myapp',
pools: [], // 初始为空,动态添加
poolStrategy: 'auto'
});
await msq.connect();
// 动态添加主连接池
await msq.addPool({
name: 'primary',
uri: 'mongodb://primary.example.com:27017/myapp',
role: 'primary',
default: true
});
console.log('✅ 添加主连接池');
// 动态添加副本连接池
await msq.addPool({
name: 'secondary-1',
uri: 'mongodb://secondary1.example.com:27017/myapp',
role: 'secondary',
weight: 2
});
console.log('✅ 添加副本连接池');
// 获取连接池列表
console.log('当前连接池:', msq.getPoolNames());
// 获取统计信息
const stats = msq.getPoolStats();
console.log('\n连接池统计:', JSON.stringify(stats, null, 2));
// 获取健康状态
const health = msq.getPoolHealth();
console.log('\n健康状态:');
for (const [poolName, status] of health.entries()) {
console.log(` ${poolName}: ${status.status}`);
}
// 移除连接池
await msq.removePool('secondary-1');
console.log('\n✅ 移除副本连接池');
console.log('剩余连接池:', msq.getPoolNames());
await msq.close();
}
async function example3_transactionWithPool() {
console.log('\n========== 示例3: 事务锁定到指定连接池 ==========\n');
const msq = new MonSQLize({
type: 'mongodb',
databaseName: 'myapp',
pools: [
{
name: 'primary',
uri: 'mongodb://primary.example.com:27017/myapp',
role: 'primary',
default: true
}
],
poolStrategy: 'auto'
});
await msq.connect();
// 事务锁定到 primary 连接池
await msq.withTransaction(async (tx) => {
const users = tx.collection('users');
const orders = tx.collection('orders');
// 插入用户
await users.insertOne({
name: 'Alice',
email: 'alice@example.com'
});
// 插入订单
await orders.insertOne({
userId: 'user123',
amount: 100
});
console.log('✅ 事务操作完成');
}, { pool: 'primary' }); // 🔴 指定连接池
await msq.close();
}
async function example4_singlePoolBackwardCompatibility() {
console.log('\n========== 示例4: 单连接池模式(向后兼容)==========\n');
// 现有代码无需修改
const msq = new MonSQLize({
type: 'mongodb',
databaseName: 'myapp',
config: {
uri: 'mongodb://localhost:27017/myapp'
}
});
await msq.connect();
console.log('✅ 单连接池模式正常工作');
console.log('poolManager:', msq._poolManager === null ? 'null (单连接池)' : '已初始化');
// 所有现有 API 照常使用
const { collection } = msq.dbInstance;
const users = collection('users');
// 正常执行查询
// await users.find({ status: 'active' });
await msq.close();
}
async function example5_fallbackStrategies() {
console.log('\n========== 示例5: 故障转移策略 ==========\n');
// 策略1: error - 抛出错误(默认)
const msq1 = new MonSQLize({
type: 'mongodb',
databaseName: 'myapp',
pools: [...],
poolFallback: {
enabled: true,
fallbackStrategy: 'error' // 所有连接池故障时抛出错误
}
});
// 策略2: readonly - 只读模式降级
const msq2 = new MonSQLize({
type: 'mongodb',
databaseName: 'myapp',
pools: [...],
poolFallback: {
enabled: true,
fallbackStrategy: 'readonly' // 只允许读操作
}
});
// 策略3: secondary - 尝试使用 down 状态的 secondary
const msq3 = new MonSQLize({
type: 'mongodb',
databaseName: 'myapp',
pools: [...],
poolFallback: {
enabled: true,
fallbackStrategy: 'secondary' // 尝试使用 down 的 secondary
}
});
console.log('✅ 故障转移策略配置示例');
}
// 运行所有示例
async function main() {
console.log('=====================================');
console.log(' 多连接池功能使用示例 (v1.0.8+) ');
console.log('=====================================');
try {
// 注意:这些示例需要实际的 MongoDB 服务器
// 如果没有,请使用 Memory Server 进行测试
await example4_singlePoolBackwardCompatibility();
// 其他示例需要真实的 MongoDB 副本集
console.log('\n💡 提示:其他示例需要真实的 MongoDB 副本集环境');
console.log(' - example1: 基础多连接池配置');
console.log(' - example2: 动态连接池管理');
console.log(' - example3: 事务锁定');
console.log(' - example5: 故障转移策略');
} catch (error) {
console.error('❌ 错误:', error.message);
process.exit(1);
}
}
// 如果直接运行此文件
if (require.main === module) {
main().catch(console.error);
}
module.exports = {
example1_basicMultiPool,
example2_dynamicPoolManagement,
example3_transactionWithPool,
example4_singlePoolBackwardCompatibility,
example5_fallbackStrategies
};