实现 nacos + traefik,改了很多东西!!!
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { NacosNamingClient } from 'nacos';
|
||||
// @ts-ignore
|
||||
import { NacosConfigClient, NacosNamingClient } from 'nacos';
|
||||
import { formatServiceUrl, getLocalIP } from "./utils";
|
||||
|
||||
const logger = console;
|
||||
@@ -15,20 +14,81 @@ export interface RegisterOptions {
|
||||
namespace?: string;
|
||||
}
|
||||
|
||||
|
||||
export class nacosServiceManage {
|
||||
|
||||
// 静态变量:缓存单例实例(key为配置唯一标识,value为实例)
|
||||
private static instanceMap: Map<string, nacosServiceManage> = new Map();
|
||||
|
||||
public nacosClient!: NacosNamingClient;
|
||||
public configClient!: NacosConfigClient;
|
||||
public initOptions!: RegisterOptions;
|
||||
|
||||
|
||||
constructor(option: RegisterOptions) {
|
||||
private constructor(option: RegisterOptions) {
|
||||
this.initOptions = option;
|
||||
this.init()
|
||||
}
|
||||
|
||||
/**
|
||||
* 静态 create 方法:单例模式入口,同一配置只返回一个实例
|
||||
* @param option 初始化配置
|
||||
* @returns nacosServiceManage 实例(单例)
|
||||
*/
|
||||
public static async create(option: RegisterOptions): Promise<nacosServiceManage> {
|
||||
const configKey = this.getConfigKey(option);
|
||||
|
||||
async init() {
|
||||
if (this.instanceMap.has(configKey)) {
|
||||
logger.log(`[nacos-federation] 已存在 ${ option.serviceName } 的Nacos实例,直接复用`);
|
||||
return this.instanceMap.get(configKey)!;
|
||||
}
|
||||
|
||||
const instance = new nacosServiceManage(option);
|
||||
await instance.initClient();
|
||||
await instance.initConfig();
|
||||
|
||||
this.instanceMap.set(configKey, instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成配置唯一标识:基于 serverList + namespace + serviceName(确保不同配置对应不同实例)
|
||||
*/
|
||||
private static getConfigKey(option: RegisterOptions): string {
|
||||
const namespace = option.namespace || 'public';
|
||||
return `${ option.serverList }-${ namespace }-${ option.serviceName }`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Nacos发现服务地址
|
||||
* @param serviceName 发现选项
|
||||
* @returns 服务地址(如 'http://192.168.1.101:1301')
|
||||
*/
|
||||
public async find(serviceName: string) {
|
||||
const instances = await this.nacosClient.getAllInstances(serviceName);
|
||||
// console.log("instances: ", instances);
|
||||
if (instances.length === 0) {
|
||||
throw new Error(`[nacos-federation] 未发现服务 ${ serviceName } 的实例`);
|
||||
}
|
||||
return formatServiceUrl(instances[0].ip, instances[0].port);
|
||||
}
|
||||
|
||||
/**
|
||||
* 手动销毁实例(如服务下线时)
|
||||
*/
|
||||
public async destroy() {
|
||||
const configKey = nacosServiceManage.getConfigKey(this.initOptions);
|
||||
// 注销Nacos服务实例
|
||||
const { serviceName, port } = this.initOptions;
|
||||
const ip = getLocalIP();
|
||||
// @ts-ignore
|
||||
await this.nacosClient.deregisterInstance(serviceName, { ip, port });
|
||||
logger.log(`[nacos-federation] 服务 ${ serviceName } 已从Nacos注销`);
|
||||
|
||||
// 从缓存中移除实例
|
||||
nacosServiceManage.instanceMap.delete(configKey);
|
||||
}
|
||||
|
||||
// 初始化服务发现
|
||||
private async initClient() {
|
||||
this.nacosClient = new NacosNamingClient({
|
||||
logger,
|
||||
serverList: this.initOptions.serverList,
|
||||
@@ -38,30 +98,23 @@ export class nacosServiceManage {
|
||||
this.reg()
|
||||
}
|
||||
|
||||
// 初始化配置服务
|
||||
private async initConfig() {
|
||||
this.configClient = new NacosConfigClient({
|
||||
serverAddr: this.initOptions.serverList,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册服务到Nacos并返回客户端实例
|
||||
*/
|
||||
async reg() {
|
||||
private async reg() {
|
||||
const { serviceName, port } = this.initOptions;
|
||||
const ip = getLocalIP();
|
||||
// @ts-ignore
|
||||
await this.nacosClient.registerInstance(serviceName, {
|
||||
port, ip
|
||||
})
|
||||
console.log(`[nacos] 服务 ${ serviceName } 已注册:${ formatServiceUrl(ip, port) }`);
|
||||
await this.nacosClient.registerInstance(serviceName, { port, ip })
|
||||
logger.log(`[nacos-federation] 服务 ${ serviceName } 已注册:${ formatServiceUrl(ip, port) }`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Nacos发现服务地址
|
||||
* @param serviceName 发现选项
|
||||
* @returns 服务地址(如 'http://192.168.1.101:1301')
|
||||
*/
|
||||
async find(serviceName: string) {
|
||||
const instances = await this.nacosClient.getAllInstances(serviceName);
|
||||
if (instances.length === 0) {
|
||||
throw new Error(`[nacos] 未发现服务 ${ serviceName } 的实例`);
|
||||
}
|
||||
return formatServiceUrl(instances[0].ip, instances[0].port);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
// import {NacosNamingClient} from 'nacos';
|
||||
//
|
||||
// const logger = console;
|
||||
//
|
||||
// const client = new NacosNamingClient({
|
||||
// logger,
|
||||
// serverList: '192.168.31.100:8848', // replace to real nacos serverList
|
||||
// namespace: 'public',
|
||||
// });
|
||||
//
|
||||
// await client.ready();
|
||||
//
|
||||
// // registry instance
|
||||
// await client.registerInstance('main-app', {
|
||||
// ip: '192.168.31.101',
|
||||
// port: 1300,
|
||||
// });
|
||||
|
||||
// @ts-ignore
|
||||
import { nacosServiceManage } from "./src/nacosServiceManage.ts";
|
||||
|
||||
const client = new nacosServiceManage({
|
||||
serverList: '192.168.31.100:8848',
|
||||
serviceName: 'main-app',
|
||||
port: 1300
|
||||
})
|
||||
|
||||
|
||||
setTimeout(() => {
|
||||
client.find("main-app").then((client) => {
|
||||
console.log("client ---- : ", client);
|
||||
})
|
||||
}, 3000)
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2020",
|
||||
"target": "ESNext",
|
||||
"module": "CommonJS",
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
|
||||
Reference in New Issue
Block a user