nanoservices
比微服务更小的纳米服务框架
安装
$ npm install nanoservices --save
要求 Node.js v4.0.0 或更高版本
设计目标
- 将项目代码服务化,每一个「纳米服务」完成一个小功能
- 通过
requestId
来跟踪记录完整的调用链 - 自动记录日志,结合相应的调试信息方便开发调试
- 考虑接驳
clouds
系统,可以使得不同主机/进程间的调用也适用
使用方法
callback 接口
'use strict'; const Manager = ; // 创建管理器const services = ; // 注册服务services; // 调用服务services; // 支持 Promiseservices ;
promise 接口
'use strict'; const Manager = ; // 创建管理器const services = ; // 注册服务services; // 调用服务services ;
Context对象
服务的处理函数只接收一个参数,该参数为一个Context
对象,通过该对象完成读取参数、返回结果等所有操作。
Context
对象结构如下:
调用链
各个服务之间的调用会通过传递requestId
来记录调用来源以及整个调用链结构(请求参数、返回结果等),
还可以通过debug()
方法来打印调试信息,这些信息会根据需要记录到日志文件中,
只要通过requestId
即可查询到完整的调用信息。
在服务外部调用多个服务
默认情况下使用services.call()
会自动生成一个requestId
并调用服务,但调用方无法获得这个requestId
,
我们可以通过services.newContext()
来获得一个新的Context
对象:
// 创建Contextconst ctx = services;// 如果要自定义requestId,可以这样:// const ctx = services.newContext(requestId); // 调用服务ctx;
顺序调用多个服务
有时候某个服务实际上是通过顺序调用一系列服务来完成操作的,可以使用ctx.series()
方法:
ctx;
日志
默认情况下,服务调用产生以及服务执行期间所产生的日志调试信息是不会被记录的。可以在初始化Manager
时可以传入一个logRecorder
参数,
以便将这些日志信息记录到指定的位置。目前支持stream
和logger
两种方式。
1、stream
方式如下:
const Manager StreamRecorder = ; // 将日志记录到标准输出接口const stream = processstdout; // 创建LogRecorderconst logRecorder: new StreamRecorderstream newLine: '\n' format: '$date $time $type $id $content'; // 创建Managerconst services = logRecorder ;
在创建StreamRecorder
时,第一个参数stream
为一个标准的Writable Stream
,可以通过fs.writeWriteStream()
或TCP
网络的可写流;
第二个参数为一些选项,比如:
newLine
表示换行符,即每条日志都会自动在末尾加上这个换行符,如果不指定则表示不加换行符format
表示日志格式,其中有以下变量可选:$id
- 当前requestId
$service
- 当前服务名称,如果没有则为null
$uptime
- 当前context
已启动的时间(毫秒)$date
- 日期,如2016/08/02
$time
- 时间,如14:01:37
$datetime
- 日期时间,如2016/08/02 14:01:37
isotime
- ISO格式的时间字符串,如2016-08-12T13:20:27.599Z
$timestamp
- 毫秒级的Unix时间戳,如1470980387892
$timestamps
- 秒级的Unix时间戳,如1470980387
$type
- 日志类型,目前有以下几个:debug, log, error, call, result
$content
- 内容字符串$pid
- 当前进程PID$hostname
- 当前主机名
2、logger
方式如下:
const Manager LoggerRecorder = ; // 一个日志记录器const logger = console;// 由于console没有debug方法,需要模拟一个loggerdebug = consolelog; // 创建LogRecorderconst logRecorder: new LoggerRecorderlogger format: '$date $time $type $id $content'; // 创建Managerconst services = logRecorder ;
在创建LoggerRecorder
时,第一个参数logger
为一个包含了info, log, debug, error
这四个方法的日志记录器;
第二个参数为一些选项,比如format
,其使用方法与上文的StreamRecorder
相同,但默认值与前者不同。
ctx.log()
会使用logger.log()
来记录,ctx.debug()
使用logger.debug()
,
ctx.error()
使用logger.error()
,其他的均使用logger.info()
来记录。
通过记录服务调用日志等信息,再结合相应的日志分析系统即可实现调试跟踪等功能。
License
The MIT License (MIT)
Copyright (c) 2016 SuperID | 免费极速身份验证服务
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.