1.1.1 • Published 9 months ago
kigo-rpc v1.1.1
安装
$ cnpm i kigo-rpc --save-dev
用法
- 直接引用需要使用的service文件,调用其中的方法即可
- 只会加载已经引用的Service
- Service不用再写在特定目录下
const friendService=require('./service/FriendService')
co(function *()
{
yield friendService.isUserInGuestList(userId, 222);
})
配置文件
- 默认加载项目执行目录(process.cwd())同级目录rpc下的配置文件,默认配置目录结构如下
rpc
├──app.conf
├──module.json
├──module-xxx.json
- 也可以设置环境变量,将配置文件放在一个固定的系统目录 这样多个进程可以共享配置
process.env.moduleDir='/data/home/rpc';
Service编写
- 模块导出Service对象 编写自己的Service时 需要继承Service
const Service = require('kigo-rpc').Service;
function DemoService() {
Service.call(this,'demoService','com.melot.test');
}
util.inherits(DemoService, Service);
module.exports=new DemoService();
- demoService 和配置文件中的key对应起来即可 com.melot.test 为interfancename
API方式创建Service
- 除了通过引用文件方式设置service,也可以直接通过API方式new Service
- 参考 example/createService.js
const InvokeConfig = require('kigo-rpc').InvokeConfig;
const interfanceName='com.melot.test';
var config = InvokeConfig.fromJson({
"moduleName": "friendservice",
"moduleVersion": "1.0.9",
"filters": ["cache", "degrade", "circuitbreaker"],
"methods": []
})
var service =new Service(InvokeConfig,interfanceName);
或者
var service =new Service('demoService',interfanceName) //这里需要配置文件中有 demoService 的配置
//自定义方法
service.xxxMethod=function()
{
}
多租户相关
- 通过@melot/Context模块来模拟线程变量 传递租户信息 Cat的transaction对象等
- rpc模块会取出Context中的租户对象传递到java端
- 具体用法参考example/demo.js 和 @melot/Context模块