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
51 lines • 2.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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerInstance = void 0;
const WhaleNacosClient_1 = require("../baseNacos/WhaleNacosClient");
const constant_1 = require("../constant");
const log_1 = require("../nacosLogger/log");
const os = require('os');
const dns = require('dns');
const hostName = os.hostname();
// 注册实例到nacos上
function registerInstance(options) {
const nacosClientInstance = new WhaleNacosClient_1.WhaleNacosClient({
serverAddress: constant_1.serverAddress,
namespace: constant_1.namespace,
endpoint: '7777',
logger: log_1.logger
});
let instanceOptions = Object.assign(constant_1.DEFAULT_INSTANCE_OPTIONS, options);
const { serviceName, groupName } = instanceOptions;
dns.lookup(hostName, (err, address, family) => __awaiter(this, void 0, void 0, function* () {
console.log('DNS 解析:', err, address, family);
if (err) {
console.error('获取Pod ip 失败:', err);
}
else {
instanceOptions = Object.assign({ ip: address }, instanceOptions);
if (constant_1.TIER_VERSION) {
instanceOptions = Object.assign(instanceOptions, { metadata: { TIER_VERSION: constant_1.TIER_VERSION } });
}
try {
yield nacosClientInstance.ready();
yield nacosClientInstance.namingClient.registerInstance(serviceName, instanceOptions, groupName);
console.log(`服务${process.env.SERVICE_NAME}注册成功!Pod IP:${address}`);
}
catch (error) {
console.error(`服务${process.env.SERVICE_NAME}注册失败!Pod IP:${address}`);
}
}
}));
}
exports.registerInstance = registerInstance;
//# sourceMappingURL=index.js.map