UNPKG

whale-nest-nacos

Version:

If you are a nest micro service architecture, you can use the SDK, so as to realize service discovery based on nacos, distributed load balancing strategy

111 lines 5.41 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Discoverer = void 0; const BaseNacosClient_1 = __importDefault(require("../baseNacos/common/BaseNacosClient")); const WhaleNacosClient_1 = require("../baseNacos/WhaleNacosClient"); const constant_1 = require("../constant"); class Discoverer extends BaseNacosClient_1.default { constructor(options) { super(options); this.options = options; if (!Discoverer.instance) { Discoverer.instance = this; } this.nacosClientInstance = new WhaleNacosClient_1.WhaleNacosClient({ serverAddress: constant_1.serverAddress, namespace: constant_1.namespace, endpoint: '7777', logger: console }); return Discoverer.instance; } ready() { const _super = Object.create(null, { ready: { get: () => super.ready } }); return __awaiter(this, void 0, void 0, function* () { const { nacosNamingClient, nacosConfigClient } = yield _super.ready.call(this); this.nacosNamingClient = nacosNamingClient; this.nacosConfigClient = nacosConfigClient; return { nacosNamingClient, nacosConfigClient }; }); } selectHealthyInstances(serviceName, group, protocol) { var _a; return __awaiter(this, void 0, void 0, function* () { let tierConfig; if (this.tierConfig) { if (((_a = this.tierConfig) === null || _a === void 0 ? void 0 : _a.length) > 2) { console.warn(`应用 ${process.env.SERVICE_NAME} 配置了多个中间层,请确认配置是否正确:`, this.tierConfig); } tierConfig = this.tierConfig[0]; const { service_name, tier_version } = tierConfig; let final_serviceName = service_name || serviceName; let final_group = group || constant_1.DEFAULT_GROUP; let healthyInstances = yield this.nacosClientInstance.namingClient.selectInstances(final_serviceName, final_group); this.Pods = healthyInstances.filter(({}) => { return; }); const tierInstanceList = healthyInstances.filter(({ metadata }) => metadata.TIER_VERSION == tier_version); if (!tierInstanceList.length) { throw new Error(`未找到tier ${service_name},version:${tier_version} 的实例`); } let final_protocol = protocol || constant_1.DEFAULT_PROTOCOL; this.Pods = (tierInstanceList || []).map(({ ip, port }) => { return `${final_protocol}:${ip}:${port}`; }); } }); } getTierConfig() { var _a; return __awaiter(this, void 0, void 0, function* () { this.subscribe(); const tierConfig = yield ((_a = this.configClient) === null || _a === void 0 ? void 0 : _a.getConfig(constant_1.TIER_VERSION, constant_1.TIER_ENVIRON)); console.log(`tier ${constant_1.TIER_VERSION},${constant_1.TIER_ENVIRON} 配置获取成功!配置为:`, tierConfig); if (tierConfig) { this.tierConfig = JSON.parse(tierConfig); } else { this.tierConfig = []; throw new Error(`应用 ${process.env.SERVICE_NAME} 的tier配置不存在!`); } return this.tierConfig; }); } subscribe() { var _a; return __awaiter(this, void 0, void 0, function* () { (_a = this.configClient) === null || _a === void 0 ? void 0 : _a.subscribe({ dataId: constant_1.TIER_VERSION, group: constant_1.TIER_ENVIRON }, (content) => { if (content) { console.log(`tier ${constant_1.TIER_VERSION},${constant_1.TIER_ENVIRON} 配置获取成功!配置为:`, content); this.tierConfig = JSON.parse(content); const { serviceName, group, protocol } = this.options; this.selectHealthyInstances(serviceName, group, protocol); } else { throw new Error(`应用 ${process.env.SERVICE_NAME} '${constant_1.TIER_VERSION}' '${constant_1.TIER_ENVIRON}' 配置不存在!`); } }); }); } } exports.Discoverer = Discoverer; //# sourceMappingURL=Discoverer.js.map