Comparing version 1.1.4 to 1.1.5
@@ -175,3 +175,4 @@ "use strict"; | ||
// require consul get hosts. | ||
return consul_1.consul.get(this.getConsulAddress(), this.service, cb, this.env); | ||
return consul_1.consul.get(this.getConsulAddress(), this.service, cb, this.env, null, // idc | ||
this.consul.action || 'lookup', { timeout: this.consul.timeout }); | ||
}; | ||
@@ -178,0 +179,0 @@ /** |
@@ -6,2 +6,4 @@ "use strict"; | ||
var NConsul = require("consul"); | ||
var lookup_1 = require("./consul/lookup"); | ||
var util_1 = require("./util"); | ||
var debug = Debug('consul'); | ||
@@ -12,3 +14,3 @@ var Consul = (function () { | ||
} | ||
Consul.prototype.get = function (serAddress, service, callback, env, idc) { | ||
Consul.prototype.get = function (serAddress, service, callback, env, idc, action, requestOptions) { | ||
if (env === void 0) { env = 'prod'; } | ||
@@ -22,9 +24,27 @@ if (!service) { | ||
debug("start_request host: " + serAddress.getHost + " port: " + serAddress.getPort()); | ||
this.initOptions(serAddress, service, env, idc); | ||
if (action === 'catelog') { | ||
this.requestWithCatelog(callback); | ||
} | ||
else { | ||
this.requestWithLookup(requestOptions, callback); | ||
} | ||
}; | ||
Consul.prototype.initOptions = function (serAddress, service, env, idc) { | ||
if (env === void 0) { env = 'prod'; } | ||
this.options = { | ||
address: serAddress, | ||
service: service, | ||
env: env, | ||
idc: idc | ||
}; | ||
}; | ||
Consul.prototype.requestWithCatelog = function (callback) { | ||
var consul = NConsul({ | ||
host: serAddress.getHost(), | ||
port: serAddress.getPort() | ||
host: this.options.address.getHost(), | ||
port: this.options.address.getPort() | ||
}); | ||
var __this = this; | ||
consul.catalog.service.nodes({ | ||
service: service | ||
service: __this.options.service | ||
}, function (err, nodes) { | ||
@@ -35,3 +55,3 @@ if (err) | ||
nodes.forEach(function (node) { | ||
if (__this.useCheckEnv && !__this.checkEnv(node.ServiceTags, env)) { | ||
if (__this.useCheckEnv && !__this.checkEnv(node.ServiceTags, __this.options.env)) { | ||
return; | ||
@@ -44,2 +64,27 @@ } | ||
}; | ||
Consul.prototype.requestWithLookup = function (requestOptions, callback) { | ||
var debug = this.options.env !== 'prod'; | ||
var lookup = new lookup_1.Lookup({ debug: debug }); | ||
var __this = this; | ||
lookup.request(util_1.assign({ | ||
service: this.options.service, | ||
host: this.options.address.host, | ||
port: this.options.address.port | ||
}, requestOptions || {}), function (err, nodes) { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
var hosts = []; | ||
nodes.forEach(function (node) { | ||
if (__this.useCheckEnv && node.Tags && node.Tags.env) { | ||
if (node.Tags.env !== __this.options.env) { | ||
return; | ||
} | ||
} | ||
hosts.push(new protocol_1.Address(node.Host, node.Port)); | ||
}); | ||
callback(null, hosts); | ||
}); | ||
}; | ||
/** | ||
@@ -46,0 +91,0 @@ * tags maybe is null, @TODO |
{ | ||
"name": "kitejs", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"description": "the rpc framework Kite for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -34,3 +34,3 @@ /** | ||
consul: { host: string, port: number }; | ||
consul: { host: string, port: number, action ?: string, timeout ?: number}; | ||
@@ -209,3 +209,11 @@ /** | ||
// require consul get hosts. | ||
return consul.get(this.getConsulAddress(), <string>this.service, cb, this.env); | ||
return consul.get( | ||
this.getConsulAddress(), | ||
<string>this.service, | ||
cb, | ||
this.env, | ||
null, // idc | ||
this.consul.action || 'lookup', | ||
{timeout: this.consul.timeout} | ||
); | ||
} | ||
@@ -212,0 +220,0 @@ |
@@ -1,4 +0,7 @@ | ||
import {Address} from './protocol/protocol'; | ||
import { Address } from './protocol/protocol'; | ||
import * as Debug from 'debug'; | ||
import * as NConsul from 'consul'; | ||
import { Lookup } from './consul/lookup'; | ||
import { Map } from './types/lang'; | ||
import { assign } from './util'; | ||
@@ -9,4 +12,5 @@ const debug = Debug('consul'); | ||
useCheckEnv: boolean = true; | ||
options: Map<any>; | ||
public get(serAddress: Address, service: string, callback, env: string = 'prod', idc ?: string) { | ||
public get(serAddress: Address, service: string, callback, env: string = 'prod', idc ?: string, action?: string, requestOptions ?: Map<any>) { | ||
if (!service) { | ||
@@ -22,5 +26,24 @@ return callback(new Error('unknown service name.'), null); | ||
this.initOptions(serAddress, service, env, idc); | ||
if (action === 'catelog') { | ||
this.requestWithCatelog(callback); | ||
} else { | ||
this.requestWithLookup(requestOptions, callback); | ||
} | ||
} | ||
private initOptions(serAddress: Address, service: string, env: string = 'prod', idc ?: string) { | ||
this.options = { | ||
address: serAddress, | ||
service: service, | ||
env: env, | ||
idc: idc | ||
}; | ||
} | ||
private requestWithCatelog(callback) { | ||
const consul = NConsul({ | ||
host: serAddress.getHost(), | ||
port: serAddress.getPort() | ||
host: this.options.address.getHost(), | ||
port: this.options.address.getPort() | ||
}); | ||
@@ -32,3 +55,3 @@ | ||
{ | ||
service: service | ||
service: __this.options.service | ||
}, function (err, nodes) { | ||
@@ -39,3 +62,3 @@ if (err) return callback(err); | ||
nodes.forEach(node => { | ||
if (__this.useCheckEnv && !__this.checkEnv(node.ServiceTags, env)) { | ||
if (__this.useCheckEnv && !__this.checkEnv(node.ServiceTags, __this.options.env)) { | ||
return; | ||
@@ -52,5 +75,38 @@ } | ||
} | ||
); | ||
); | ||
} | ||
private requestWithLookup(requestOptions: Map<any>, callback) { | ||
let debug = this.options.env !== 'prod'; | ||
let lookup = new Lookup({debug: debug}); | ||
let __this = this; | ||
lookup.request(assign({ | ||
service: this.options.service, | ||
host: this.options.address.host, | ||
port: this.options.address.port | ||
}, requestOptions || {}), function (err, nodes) { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
var hosts: Array<Address> = []; | ||
nodes.forEach(node => { | ||
if (__this.useCheckEnv && node.Tags && node.Tags.env) { | ||
if (node.Tags.env !== __this.options.env) { | ||
return; | ||
} | ||
} | ||
hosts.push(new Address( | ||
node.Host, | ||
node.Port | ||
)); | ||
}); | ||
callback(null, hosts); | ||
}); | ||
} | ||
/** | ||
@@ -57,0 +113,0 @@ * tags maybe is null, @TODO |
Sorry, the diff of this file is not supported yet
70646
38
1474