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
58 lines • 2.67 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 });
const { NacosNamingClient } = require('nacos');
const BaseNacosConfigClient_1 = __importDefault(require("./BaseNacosConfigClient"));
const constant_1 = require("../../constant");
class BaseNacosClient {
constructor({ serverAddress, namespace, endpoint, logger }) {
// nacos服务地址
this.serverAddress = serverAddress;
// namespace: 名称空间必须在服务器上存在
this.namespace = namespace;
this.logger = logger;
// 注册服务到Nacos服务器
this.namingClient = new NacosNamingClient({
logger: this.logger,
serverList: this.serverAddress,
namespace: this.namespace
});
this.configClient = new BaseNacosConfigClient_1.default({
serverAddr: this.serverAddress,
namespace: this.namespace,
requestTimeout: constant_1.REQUESTTIMEOUT,
responseTimeout: constant_1.RESPONSETIMEOUT,
connectTimeout: constant_1.CONNECTTIMEOUT,
endpoint
});
}
ready() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
if (!BaseNacosClient.isReady) {
yield this.namingClient.ready();
// namingClient ready 后,NacosConfigClient才能正常创建连接,否则会报连接超时
yield ((_a = this.configClient) === null || _a === void 0 ? void 0 : _a.ready());
BaseNacosClient.isReady = true;
}
return {
nacosNamingClient: this.namingClient,
nacosConfigClient: this.configClient
};
});
}
}
exports.default = BaseNacosClient;
BaseNacosClient.isReady = false;
//# sourceMappingURL=BaseNacosClient.js.map