kitejs
Version:
the rpc framework Kite for Node.js
155 lines (118 loc) • 3.24 kB
Markdown
# kitejs
## 说明
kitejs 提供统一访问服务的 client,支持 Http、RPC(thrift) 协议,提供多种编解码方式(正在支持)。
kitejs 集成寻址、支持负载均衡策略
## 使用
首先进行安装
```bash
npm install kitejs --save
```
使用方法
```js
// for HTTP
const kite = require('kitejs');
var client = new kite.Client(
{
host: '127.0.0.1',
port: 8080,
protocol: 'HTTP',
log: {
logFile: __dirname + '/client.log',
logId: Date.now()
}
}
);
// use request component
client.request({ path: '/' }, function (err, data) {
if (err) throw err;
console.log(data);
});
```
```js
// for THRIFT
const kite = require('kitejs');
var client = new kite.Client(
{
host: '127.0.0.1',
port: 8080,
protocol: 'THRIFT',
log: {
logFile: __dirname + '/client.log',
logId: Date.now()
}
}
);
client
.loadService(__dirname + '/thrift/gen-nodejs/Calculator.js')
.request(function (err, cal, conn) {
if (err) throw err;
cal.ping(function (err, response) {
console.log('ping()');
});
cal.add(1, 1, function (err, response) {
console.log(response);
});
});
```
```js
// for CONSUL
const kite = require('kitejs');
var client = new kite.Client({
service: 'ies.fe.mis',
searchHostType: 'CONSUL',
log: {
logFile: __dirname + '/client.log',
logId: Date.now()
}
});
client.request({path: '/'}, function (err, data) {
console.log(data);
});
```
## 接口
### Client(options) 客户端类
可以方便的创建一个 Client 用于请求。。。
**Options**
- protocol 访问协议,HTTP \ THRIFT,默认**HTTP**
- searchHostType 寻址方式 LOCAL 或者 CONSUL,默认**LOCAL**
- service consul 时提供 PSM 信息
- host 服务端域名信息
- port 服务端端口信息
- timeout 访问超时时间 `1s` `100ms` or `1` 默认为**秒**
- address 更人性化的地址设置,比如 `127.0.0.1:10220`
- consul 提供 consul 服务地址信息
- retry 重试次数,当网络不稳定时,需要重试 `2`
- transport 选择的thrift协议。FRAMED | BUFFERED
```js
{
consul: {
host: '127.0.0.1',
port: 2280
}
}
```
### Client.`request` 发起请求,获取服务端数据
```
.request(options?, cb);
```
参数
- options 可选,主要用于 HTTP 填写 PATH、HEADERS 等信息
- `request` 的参数
- cb
```js
function (err, data, conn?) {}
```
- err 如果发生错误,err 为 Error 对象实例,如果未发生错误为 `null`
- data
- protocol=THRIFT 时,返回 Service 对象
- protocol=HTTP 时,直接返回请求 response 的内容
- conn 可选参数,如果 THRIFT 时,返回请求 connection ,可以方便 Service 获取到数据后关闭连接。
### Client.`loadService` 加载 Thrift Service
参数
```js
client.loadService('xxService.js')
// return client Object instance.
```
- Thrift Service 的 path,框架会 load `xxService.js` 并注册入 Thrift 请求框架
返回
- Client 对象实例