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
74 lines • 3.53 kB
JavaScript
;
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.TierNacosClient = void 0;
const BaseNacosConfigClient_1 = __importDefault(require("./common/BaseNacosConfigClient"));
const constant_1 = require("../constant");
class TierNacosClient {
constructor(options) {
if (!TierNacosClient.instance) {
TierNacosClient.configClient = new BaseNacosConfigClient_1.default({
serverAddr: options.serverAddress,
namespace: options.namespace,
requestTimeout: constant_1.REQUESTTIMEOUT,
responseTimeout: constant_1.RESPONSETIMEOUT,
connectTimeout: constant_1.CONNECTTIMEOUT,
endpoint: options.endpoint
});
TierNacosClient.instance = this;
}
return TierNacosClient.instance;
}
ready() {
return __awaiter(this, void 0, void 0, function* () {
yield TierNacosClient.configClient.ready();
});
}
getTierConfig() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
// await TierNacosClient.configClient.ready()
this.subscribe();
const tierConfig = yield ((_a = TierNacosClient.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 = TierNacosClient.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);
}
else {
throw new Error(`应用 ${process.env.SERVICE_NAME} '${constant_1.TIER_VERSION}' '${constant_1.TIER_ENVIRON}' 配置不存在!`);
}
});
});
}
}
exports.TierNacosClient = TierNacosClient;
//# sourceMappingURL=TierNacosClient.js.map