UNPKG

coordinator-node-client

Version:

分布式协调服务node客户端,用于获取微服务的可用地址,参照eureka-js-client.

233 lines (227 loc) 6.69 kB
/** * Copyright (c) 2010-2015 EEFUNG Software Co.Ltd. All rights reserved. * 版权所有 (c) 2010-2015 湖南蚁坊软件有限公司。保留所有权利。 * Created by liwenjun on 2016/4/13. */ var EurekaWrapper = require('./EurekaWrapper'); var RequestWrapper = require('./RequestWrapper'); var util = require("./utils"); function noop() { } /* CoordinatorClient对象 @param config CoordinatorClient参数 */ function CoordinatorClient(config) { this.count = 0; this.wrapper = new EurekaWrapper(config); this.logger = this.wrapper.getLogger(); } /* 设置token。 */ CoordinatorClient.prototype.setToken = function (token) { if (this.wrapper) { this.wrapper.setToken(token); } } /* 启动。 */ CoordinatorClient.prototype.start = function (fn) { if (this.wrapper) { this.wrapper.start(fn); } } /* 停止。 */ CoordinatorClient.prototype.stop = function (fn) { if (this.wrapper) { this.wrapper.stop(fn); } } /** * 根据appid获取某个IP形式服务地址 * @param appId * @returns {*} 服务地址, 例如:http://192.168.1.100:8080,没有地址时,返回空字符串""。 */ CoordinatorClient.prototype.getServerByAppId = function (appId) { if (this.wrapper) { //获取全部实例 var appInstances = this.wrapper.getInstancesByAppId(appId); //获取其中一个实例 if (appInstances) { var oneInstance = getOneInstanceFromAll(this, appInstances); //组织成url地址 return util.getServerPath(oneInstance); } else { return ""; } } else { return ""; } }; /** * 根据appid获取IP形式的服务实例地址列表 * @param appId * @returns {*} 服务实例地址列表[],没有地址时返回空数组[]。 */ CoordinatorClient.prototype.getServerListByAppId = function (appId) { var appUrls = []; if (this.wrapper) { //获取全部实例 var appInstances = this.wrapper.getInstancesByAppId(appId); if (appInstances && appInstances instanceof Array) { //组织成url地址 appUrls = appInstances.map(function (instance) { return util.getServerPath(instance); }); } } return appUrls; }; /** * 根据vipaddress获取某个IP形式的服务实例地址 * @param vipAddress * @returns {*} 单个服务地址;没有地址时,返回空字符串""。 */ CoordinatorClient.prototype.getServerByVipAddress = function (vipAddress) { if (this.wrapper) { //获取全部实例 var appInstances = this.wrapper.getInstancesByVipAddress(vipAddress); if (appInstances && appInstances instanceof Array) { //获取其中一个实例 var oneInstance = getOneInstanceFromAll(this, appInstances); //组织成url地址 return util.getServerPath(oneInstance); } else { return ""; } } else { return ""; } }; /** * 根据vipaddress获取IP形式的服务实例列表 * @param vipAddress * @returns {*} 服务地址数组["http://192.168.1.100:8080","http://192.168.1.100:8080"],没有地址时返回空数组[]. */ CoordinatorClient.prototype.getServerListByVipAddress = function (vipAddress) { var appUrls = []; if (this.wrapper) { //获取全部实例 var appInstances = this.wrapper.getInstancesByVipAddress(vipAddress); if (appInstances && appInstances instanceof Array) { //组织成url地址 appUrls = appInstances.map(function (instance) { return util.getServerPath(instance); }); } } return appUrls; }; /** * 根据过滤规则,获取服务 * @param filter 是appId,如user;或者是函数过滤appId,例如:function(appId){return appId.indexOf("user")!=-1;} * @returns {{}} 对象,如{"user":["http://"]} */ CoordinatorClient.prototype.getServerListByFilter = function (filter) { if (this.wrapper) { //获取全部实例 return this.wrapper.getServerListByFilter(filter); } else { return null; } } /** * 获取某个可用服务。每次调用按顺序获取下一个服务实例。 * @param client CoordinatorClient * @param instances 所有实例 * @returns {*} */ function getOneInstanceFromAll(client, instances) { if (instances != null) { var upInstances = []; for (var i = 0; i < instances.length; i++) { if (instances[i].status.toUpperCase() === "UP") { upInstances.push(instances[i]); } } if (upInstances.length > 0) { //取余数 var instanceIndex = client.count++ % upInstances.length; //当大于总数时,重新计数 if (client.count >= upInstances.length) { client.count = 0; } return upInstances[instanceIndex]; } else { return ""; } } else { return ""; } }; /** * 获取http请求方法。 * @returns {RequestWrapper|exports|module.exports} */ CoordinatorClient.prototype.getRequest = function getRequest() { return new RequestWrapper(this); }; /** * 监听服务的增加 * @type {CoordinatorClient} */ CoordinatorClient.prototype.onAdded = function (fn) { if (this.wrapper) { this.wrapper.onAdded(fn); } } /** * 取消服务增加的监听 * @param fn */ CoordinatorClient.prototype.unbindAddedListener = function (fn) { if (this.wrapper) { this.wrapper.unbindAddedListener(fn); } } /** * 监听服务的移除 * @type {CoordinatorClient} */ CoordinatorClient.prototype.onRemoved = function (fn) { if (this.wrapper) { this.wrapper.onRemoved(fn); } } /** * 取消服务移除的监听 * @param fn */ CoordinatorClient.prototype.unbindRemovedListener = function (fn) { if (this.wrapper) { this.wrapper.unbindRemovedListener(fn); } } /** * 监听服务的变化 * @type {CoordinatorClient} */ CoordinatorClient.prototype.onChanged = function (fn) { if (this.wrapper) { this.wrapper.onChanged(fn); } } /** * 取消服务变化的监听 * @param fn */ CoordinatorClient.prototype.unbindChangedListener = function (fn) { if (this.wrapper) { this.wrapper.unbindChangedListener(fn); } } module.exports = CoordinatorClient;