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

109 lines 5.25 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 }); const LoadBalanceService_1 = __importDefault(require("./LoadBalanceService")); const TierNacosClient_1 = require("../baseNacos/TierNacosClient"); const constant_1 = require("../constant"); const log_1 = require("../nacosLogger/log"); class TierService extends LoadBalanceService_1.default { constructor(options) { super(options); } ready() { const _super = Object.create(null, { ready: { get: () => super.ready } }); return __awaiter(this, void 0, void 0, function* () { yield _super.ready.call(this); this.tierClientInstance = new TierNacosClient_1.TierNacosClient({ serverAddress: constant_1.serverAddress, namespace: constant_1.TIER_NAMESPACE, endpoint: '8888', logger: log_1.logger }); yield this.tierClientInstance.ready(); yield this.tierClientInstance.getTierConfig(); }); } buildConnectioPool(serviceName, packageName, protoPath) { const _super = Object.create(null, { batchCreateClientGrpc: { get: () => super.batchCreateClientGrpc } }); return __awaiter(this, void 0, void 0, function* () { const instanceList = yield this.selectHealthyInstances(serviceName, this.group); if (this.hasInstance) { _super.batchCreateClientGrpc.call(this, instanceList, packageName, protoPath); } }); } selectHealthyInstances(serviceName, group) { const _super = Object.create(null, { setServiceClusterName: { get: () => super.setServiceClusterName }, formatInstanceList: { get: () => super.formatInstanceList } }); return __awaiter(this, void 0, void 0, function* () { let healthyInstances = yield this.nacosNamingClient.selectInstances(serviceName, group); (0, log_1.log)('服务Pods:', { serviceName: serviceName, pods: healthyInstances }); _super.setServiceClusterName.call(this, healthyInstances); return this.getTierInstances(serviceName, _super.formatInstanceList.call(this, healthyInstances)); }); } // 订阅实例变更 subscribe(serviceName, packageName, protoPath) { const params = { serviceName, groupName: this.group, clusters: constant_1.WHALE_CLUSTER_INFO // 默认订阅BFF层所在集群 }; if (this.serviceClusterName) { // 更改为实例所在集群 params.clusters = this.serviceClusterName; } this.nacosNamingClient.subscribe(params, (instanceList) => { super.setServiceClusterName(instanceList); let _instanceList = super.formatInstanceList(instanceList); _instanceList = this.getTierInstances(serviceName, _instanceList); super.batchCreateClientGrpc(_instanceList, packageName, protoPath); (0, log_1.log)('服务Pods订阅:', { serviceName: serviceName, pods: instanceList }); }); } getTierInstances(serviceName, healthyInstances) { let tierServiceList = []; const tierService = this.isTierService(serviceName); if (tierService) { tierServiceList = healthyInstances.filter(({ metadata }) => metadata.TIER_VERSION == tierService.tier_version); if (!tierServiceList || !tierServiceList.length) { console.warn(`未找到服务 ${serviceName} TIER_VERSION=${tierService.tier_version}的实例,请确认该 tier 是否健康/是否可用/已部署/已上线`); } } (0, log_1.log)('tier Pods:', { serviceName: serviceName, tierPods: tierServiceList }); // 未找到tier,则走全量实例建立连接 return tierServiceList.length ? tierServiceList : healthyInstances; } isTierService(serviceName) { var _a; const tierService = (_a = this.tierClientInstance.tierConfig) === null || _a === void 0 ? void 0 : _a.find(({ service_name }) => serviceName === service_name); return tierService ? tierService : false; } } exports.default = TierService; //# sourceMappingURL=TierService.js.map