Comparing version 1.0.6 to 1.0.7
@@ -11,3 +11,3 @@ "use strict"; | ||
function Client(options, idc, ctx) { | ||
this.protocolArr = {}; | ||
this.provider = {}; | ||
this.config = new config_1.Config(options, idc, ctx); | ||
@@ -28,3 +28,3 @@ this.register('HTTP', http_1.Http); | ||
} | ||
var handle = this.protocolArr[this.config.getProtocol()]; | ||
var handle = this.provider[this.config.getProtocol()]; | ||
if (!handle) { | ||
@@ -56,3 +56,3 @@ return cb(new Error("protocol " + this.config.getProtocol() + " not support"), null); | ||
Client.prototype.register = function (protocol, handle) { | ||
this.protocolArr[protocol] = handle; | ||
this.provider[protocol] = handle; | ||
}; | ||
@@ -59,0 +59,0 @@ /** |
@@ -18,2 +18,3 @@ "use strict"; | ||
this.ctx = ctx; | ||
this.env = 'prod'; | ||
this.searchHostType = 'LOCAL'; // or 'CONSUL' | ||
@@ -151,3 +152,3 @@ /** | ||
// require consul get hosts. | ||
return consul_1.consul.get(this.getConsulAddress(), this.service, cb); | ||
return consul_1.consul.get(this.getConsulAddress(), this.service, cb, this.env); | ||
}; | ||
@@ -154,0 +155,0 @@ /** |
@@ -10,4 +10,4 @@ "use strict"; | ||
} | ||
Consul.prototype.get = function (serAddress, service, callback, idc) { | ||
if (idc === void 0) { idc = ''; } | ||
Consul.prototype.get = function (serAddress, service, callback, env, idc) { | ||
if (env === void 0) { env = 'prod'; } | ||
if (!service) { | ||
@@ -21,2 +21,3 @@ return callback(new Error('unknown service name.'), null); | ||
}); | ||
var that = this; | ||
consul.catalog.service.nodes({ | ||
@@ -29,2 +30,5 @@ service: service | ||
nodes.forEach(function (node) { | ||
if (!that.checkEnv(node.ServiceTags, env)) { | ||
return; | ||
} | ||
hosts.push(new protocol_1.Address(node.Address || node.ServiceAddress, node.ServicePort)); | ||
@@ -35,2 +39,10 @@ }); | ||
}; | ||
Consul.prototype.checkEnv = function (tags, env) { | ||
tags.forEach(function (tag) { | ||
if (tag.indexOf('env:') === 0 && tag === ('env:' + env)) { | ||
return true; | ||
} | ||
}); | ||
return false; | ||
}; | ||
return Consul; | ||
@@ -37,0 +49,0 @@ }()); |
{ | ||
"name": "kitejs", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "the rpc framework Kite for Node.js", | ||
@@ -8,3 +8,4 @@ "main": "index.js", | ||
"test": "mocha --compilers ts:ts-node/register,tsx:ts-node/register test/*", | ||
"build": "tsc -p ./src" | ||
"build": "tsc -p ./src", | ||
"prepublishOnly": "tsc -p ./src" | ||
}, | ||
@@ -26,4 +27,5 @@ "keywords": [ | ||
"mocha": "^3.4.2", | ||
"@types/node": "^8.0.13", | ||
"@types/mocha": "^2.2.41" | ||
} | ||
} |
@@ -79,1 +79,53 @@ # kitejs | ||
## 接口 | ||
### Client(options) 客户端类 | ||
可以方便的创建一个 Client 用于请求。。。 | ||
**Options** | ||
- protocol 访问协议,HTTP \ THRIFT | ||
- searchHostType 寻址方式 LOCAL 或者 CONSUL | ||
- service consul 时提供 PSM 信息 | ||
- host 服务端域名信息 | ||
- port 服务端端口信息 | ||
- timeout 访问超时时间 1s 100ms or 1 默认为秒 | ||
- address 更人性化的地址设置,比如 127.0.0.1:1010 | ||
- consul | ||
```js | ||
{ | ||
consul: { | ||
host: '127.0.0.1', | ||
port: 2280 | ||
} | ||
} | ||
``` | ||
### Client.`request` 发起请求,获取服务端数据 | ||
参数 | ||
```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 对象实例 |
@@ -19,3 +19,3 @@ /** | ||
connection: any; | ||
protocolArr: Map<any> = {}; | ||
provider: Map<any> = {}; | ||
@@ -41,3 +41,3 @@ constructor(options: Map<any>, idc?: string, ctx?: any) { | ||
var handle = this.protocolArr[this.config.getProtocol()] | ||
var handle = this.provider[this.config.getProtocol()]; | ||
if (!handle) { | ||
@@ -77,3 +77,3 @@ return cb(new Error(`protocol ${this.config.getProtocol()} not support`), null); | ||
public register(protocol: string, handle: any) { | ||
this.protocolArr[protocol] = handle; | ||
this.provider[protocol] = handle; | ||
} | ||
@@ -80,0 +80,0 @@ |
@@ -15,3 +15,3 @@ /** | ||
export class Config { | ||
env: string = 'prod'; | ||
encoding: string; | ||
@@ -183,3 +183,3 @@ | ||
// require consul get hosts. | ||
return consul.get(this.getConsulAddress(), <string>this.service, cb); | ||
return consul.get(this.getConsulAddress(), <string>this.service, cb, this.env); | ||
} | ||
@@ -186,0 +186,0 @@ |
@@ -8,3 +8,3 @@ import {Address} from './protocol/protocol'; | ||
export class Consul { | ||
public get(serAddress: Address, service: string, callback, idc: string = '') { | ||
public get(serAddress: Address, service: string, callback, env: string = 'prod', idc ?: string) { | ||
if (!service) { | ||
@@ -19,2 +19,5 @@ return callback(new Error('unknown service name.'), null); | ||
}); | ||
var that = this; | ||
consul.catalog.service.nodes( | ||
@@ -25,5 +28,9 @@ { | ||
if (err) return callback(err); | ||
var hosts: Array<Address> = []; | ||
nodes.forEach(node => { | ||
if (!that.checkEnv(node.ServiceTags, env)) { | ||
return; | ||
} | ||
hosts.push(new Address( | ||
@@ -39,4 +46,14 @@ node.Address || node.ServiceAddress, | ||
} | ||
private checkEnv(tags: Array<string>, env: string) { | ||
tags.forEach(tag => { | ||
if (tag.indexOf('env:') === 0 && tag === ('env:' + env)) { | ||
return true; | ||
} | ||
}); | ||
return false; | ||
} | ||
} | ||
export var consul = new Consul(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37985
34
967
130
4