0.0.6 • Published 4 years ago
service-modules v0.0.6
service-modules
用于服务公共中间件(RabbitMQ、Redis、Mongoose等)使用consul配置一键初始化,
Usage
install
npm install service-modules --save
init with consul
- 从 consul 获取各中间件的基础配置,并初始化连接
- 仅 MQ 需要传入部分队列信息参数初始化
const { Middleware } = require('service-modules');
let middleware = new Middleware({
mq: { exchange: 'exc', queueMap: new Map(), funcMap: new Map() },
});
const consul = middleware.initWithConsul({
// consul ip
host: '127.0.0.1',
// consul port
port: 8500,
// The params of the service to register in consul.
regService: regParam,
// mq config in consul
mqKey: 'abc/mq',
// redis config in consul
redisKey: 'abc/redis',
// mongo config in consul
mongoKey: 'abc/mongo'
});
- 初始化
Middleware
类后,middleware
包含四个基本属性:
mq
: MQ 模块,包含连接到 consul 中 mq 配置的connection
,以及发送/接收消息接口mongo
:mongoose
对象,redis
: Redis 模块,包含连接到 consul 中 redis 配置 的ioredis
对象,以及部分 redis 操作接口log
: Log 模块,log4js
的基本配置初始化, 默认输出日志级别为debug
Middleware(options)
options:
mq
: mq 监听消息队列所需的参数, 必选exchange
: 交换机queueMap
: 队列表funcMap
: 消息处理函数表
redis
: redis 订阅参数, 默认不打开订阅服务openSub
: 开启标志, 默认为false
subChannel
: 订阅通道onMessage
: 消息处理回调
log
: 日志参数level
: 日志等级, 默认为debug
function onMessage(pattern, channel, message) { console.log(pattern, channel, message); // handling messages } let middleware = new Middleware({ mq: mqInfo, redis: { openSub: true, subChannel: ['test'], onMessage: onMessage }, log: { level: 'debug' } });
middleware.initWithConsul(initParams)
返回consul对象,封装有三个api
- initParams:
host
: consul 服务器ipport
: consul 服务器端口,默认 8500regService
: 服务注册到consul的信息, 使用官方数据结构, 具体参数详见官方文档,这里不做赘述mqKey
: mq 的 ip、port等配置信息, 默认为''
, 不会初始化redisKey
: redis 的 ip、port等配置信息, 默认为''
, 不会初始化mongoKey
: mongodb 的 ip、port等配置信息, 默认为''
, 不会初始化
error events
使用事件监听机制抛出回调函数中不能捕捉的错误:
mq
: amqp 建立连接过程中的错误redis
: redis 建立连接过程中的错误mongo
: mongodb 建立连接过程中的错误consul
: consul 监听键变化时的错误
// listen on err of middleware
middleware.on('mq', (err) => {
console.debug('init mq err', err);
// do something
});
middleware.on('redis', (err) => {
console.debug('init redis err', err);
});
middleware.on('mongo', (err) => {
console.debug('init mongo err', err);
});
middleware.on('consul', (err) => {
console.debug('consul err', err);
});
api
mq api
- 监听队列
需要初始化 MQ 队列 Map queueMap
和 消息处理 Map funcMap
// listening
mq.receive()
const queueMap = new Map<string, string>([
// [routeKey, queueName]
['a.hello', 'hello']
// 队列为空为匿名队列
['#.world', '']
]);
const funcMap = new Map<string, Function>([
// [routeKey, msgFunc]
['a.hello', getHello]
['c.world', fixWorld]
]);
/** async notice */
function getHello(context) {
// do something
}
/* rpc function */
function fixWorld(context, replyTo) {
// do something
mq.rpcReply(replyTo, msg);
}
- 发送异步消息
mq.sender(exchange, routingKey, message);
- 发送同步消息
mq.rpcSender(exchange, routingKey, reqMsg);
- 回复同步消息到指定队列
mq.rpcReply(queue, msg);
consul api
- 设置键内容
await consul.setKey('abc/123', { hello: 'world' });
- 查询键值
const res = await consul.getKey('abc/123');
console.log(res); // { hello: 'world' }
- 监听键变化
consul.watchKey('abc/123', getConf);
function getConf(conf) {
console.log(conf);
}
redis api
采用 scan 命令扫描所有 key
- 查询所有键列表
const keys = await redis.getKeys('test:*');
- 删除所有键
const keys = await redis.delKeys('test:*');