egg-framework-wlxh
v1.0.3
Published
往来翕忽nodejs 服务专门定制的egg 框架,包含了:amqplib、consul、redis、mysql、sequelize、middleware(httpAuth)
Downloads
3
Readme
egg-framework-wlxh
往来翕忽nodejs 服务专门定制的egg 框架,包含了:amqplib、consul、redis、mysql、sequelize、middleware(httpAuth)
QuickStart
$ npm install
$ npm test
publish your framework to npm, then change app's framework config:
// {app_root}/index.js
{
"name": "egg-framework-wlxh",
"egg": {
"framework": "egg-framework-wlxh"
}
}
包含插件:egg-sequelize、egg-validate、egg-path-matching、amqplib、consul、egg-redis
插件开关
// config.*.js
connect: {
sequelize: true,
redis: true,
rabbitmq: true,
},
consul 配置
consul 默认关闭
如果不实用consul:
// config.*.js
// consul 服务发现配置
consul: {
connect: true, // 是否初始化连接Consul, 默认true
fetchConfig: true, // 是否获取consul的配置 true
configKey: 'nodejs/parking-server', // 配置中心key
server: {
host: '127.0.0.1', // 注册中心ip地址
port: 8500, // 注册中心端口号
},
register: true, // 是否注册当前模块,默认为true
id: `${dockerId}:nodejs-parking-server`, // id (String, optional): check ID
// serviceid (String, optional): service ID, associate check with existing service
// http (String): url to test, 2xx passes, 429 warns, and all others fail
name: 'nodejs-parking-server', // name (String): check name
address: ip, // address (String, optional): service IP address
port: localPort, // port (Integer, optional): service port
// meta (Object, optional): metadata linked to the service instance
tags: [ 'development', 'nodejs' ], // tags (String[], optional): service tags
check: {
http: `http://${ip}:${localPort}/consul/health`, // 健康检测地址
interval: '5s', // 健康检测间隔
notes: 'http service check',
status: 'critical', // status (String, optional): initial service status
},
services: [
// // 服务发现列表
// {
// serviceName: 'merchant-admin-aggregator', // 服务Name
// referName: 'merchant-admin-aggregator', // 引用名,后续可用 app.services.referName 访问服务
// comment: '实时收费列表', // 备注
// // address: '', // 服务的地址
// },
{
serviceName: 'common-service', // 服务Name
referName: 'common-service', // 引用名,后续可用 app.services.referName 访问服务
comment: '操作员相关', // 备注
// address: '', // 服务的地址
},
],
},
‼️健康检测地址 consul/health, router、controller 需要自己在项目中添加
// app/controller/consul.js
'use strict';
const { Controller } = require('egg');
class ConsulController extends Controller {
async health() {
const { ctx } = this;
ctx.body = {
code: 200,
msg: 'success',
timestamp: Date.now(),
};
}
}
module.exports = ConsulController;
// app/router.js
router.get('/consul/health', controller.consul.health);
amqplib 配置
// config.*.js
{
rabbitmq: {
protocol: 'amqp',
host: '127.0.0.1',
port: 5672,
username: 'rabbit',
password: '**********',
heartbeat: 2000,
}
}
// config/mq.js
// 配置rabbitmq consumer,producers此处配置暂时没有用到
'use strict';
module.exports = {
producers: [
{
exchange: 'eggmqproducer.exchange.message',
exchangeType: 'topic',
},
],
consumers: [
{
exchange: 'eggmqproducer.exchange.message',
exchangeType: 'topic',
queue: 'eggmqconsumer.queue.textMessage',
topic: 'text.*',
consumer: 'foo.bar',
},
],
};
// 业务代码中使用producer
const msg = JSON.stringify({
name: 'lucy',
age: 12,
});
const exchangeName = 'abc_dbe'; // 交换机名
const exchangeType = 'direct'; // exchangeType
const routingKey = 'people'; // 路由密钥
app.messenger.send('@@egg-mq/producer', {
exchange: exchangeName,
routingKey,
exchangeType, // 可以不传,默认:direct
payload: msg,
});
redis 配置
// config.*.js
{
redis: {
client: {
host: '127.0.0.1',
port: 6379,
password: '*******',
db: 0,
},
},
}
egg-sequelize 配置
详细配置请查看egg-sequelize
// config.*.js
{
sequelize: {
// delegate: 'model', // load all models to app.model and ctx.model
// baseDir: 'model', // load models from `app/model/*.js`
dialect: 'mysql',
host: '127.0.0.1',
port: 13306,
database: 'user',
username: 'root',
password: '**********',
timezone: '+08:00',
},
}
中间件
httpAuth
httpAuth 配置
使用 egg-path-matching 匹配路由
// config.*.js
const pathMatching = require('egg-path-matching');
{
middleware: [ 'httpAuth' ],
httpAuth: {
tokenKey: 'session.token', // 必需的
match: pathMatching({
match: [ /^\/nodejs\/parking/ ],
}),
},
}
const user = ctx.user // 获取用户信息
扩展
helper
helper
parseRedisRes
转化redis 获得的内容,json 字符串转化为对象
ctx.helper.parseRedisRes(await app.redis.get(content))
stringifyRedisRes
转化js 对象为字符串,存入redis
await app.redis.setex(key, 60 * 60 * 24 * 7, ctx.helper.stringifyRedisRes(content));
插件
egg-validate
自定义校验规则
- jsonString:json string
- string|number: string or number
Questions & Suggestions
Please open an issue here.
change log
- 1.0.2 发送mq 消息 durable 参数可配置,默认值true
- 1.0.3 修复getServiceFromConsul 获取可用服务失败