coordinator-node-client
Version:
分布式协调服务node客户端,用于获取微服务的可用地址,参照eureka-js-client.
42 lines (39 loc) • 1.44 kB
JavaScript
/**
* Copyright (c) 2010-2015 EEFUNG Software Co.Ltd. All rights reserved.
* 版权所有 (c) 2010-2015 湖南蚁坊软件有限公司。保留所有权利。
* Created by liwenjun on 2016/4/13.
*/
var aysnc = require("./async");
/**
* request请求封装
* @param client CoordinatorClient
* @constructor
*/
function RequestWrapper(client) {
this.client = client;
}
/*
根据appid获取服务实例,循环请求服务,直到能够获取数据或全部实例请求完毕时结束。
@appId 服务的名称或id
@requestFn 发送rest请求的函数,接收两个参数(url,callback),url是服务地址,callback为request请求的回调函数
@callback requestFn函数的回调函数
*/
RequestWrapper.prototype.rest = function (appId, requestFn, callback) {
//获取所有实例
var appUrls = this.client.getServerListByAppId(appId);
if (appUrls && appUrls instanceof Array) {
if (appUrls.length > 0) {
aysnc.setLogger(this.client.logger);
//fn,没有url时回调callback
aysnc.eachOfSeries(appUrls, requestFn, callback);
} else {
callback(new Error("协调中心没有可用的" + appId + "服务!"));
}
} else {
callback(new Error("协调中心服务" + appId + "不存在!"));
}
}
/**
* 重新包装好后,再次Exports出去
*/
module.exports = RequestWrapper;