增加任务

This commit is contained in:
编码猿
2025-09-20 02:26:42 +08:00
parent 32d40e1fa3
commit e038fdb965
14 changed files with 356 additions and 129 deletions

View File

View File

@@ -0,0 +1,28 @@
{
"name": "nacos-federation",
"version": "1.0.2",
"description": "Nacos服务注册与发现插件用于模块联邦",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "tsc",
"prepublishOnly": "npm run build"
},
"dependencies": {
"nacos": "^2.6.0"
},
"devDependencies": {
"@types/node": "^20.19.17",
"typescript": "~5.8.3"
},
"keywords": [
"nacos",
"module-federation",
"service-discovery"
],
"author": "",
"license": "MIT"
}

View File

@@ -0,0 +1,2 @@
export * from './nacosServiceManage';
export * from './utils';

View File

@@ -0,0 +1,67 @@
import { NacosNamingClient } from 'nacos';
// @ts-ignore
import { formatServiceUrl, getLocalIP } from "./utils";
const logger = console;
export interface RegisterOptions {
serverList: string;
/** 服务名称(如 'main-app' */
serviceName: string;
/** 服务端口号 */
port: number;
/** 命名空间(用于环境隔离,默认 'public' */
namespace?: string;
}
export class nacosServiceManage {
public nacosClient!: NacosNamingClient;
public initOptions!: RegisterOptions;
constructor(option: RegisterOptions) {
this.initOptions = option;
this.init()
}
async init() {
this.nacosClient = new NacosNamingClient({
logger,
serverList: this.initOptions.serverList,
namespace: this.initOptions.namespace || 'public',
});
await this.nacosClient.ready();
this.reg()
}
/**
* 注册服务到Nacos并返回客户端实例
*/
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) }`);
}
/**
* 从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);
}
}

View File

@@ -0,0 +1,36 @@
import { networkInterfaces } from "os";
/**
* 获取本机局域网IPv4地址
* @returns 局域网IP地址如 '192.168.1.100'
*/
export function getLocalIP(): string {
const interfaces = networkInterfaces();
for (const devName in interfaces) {
const iface = interfaces[devName];
if (!iface) continue;
for (const alias of iface) {
if (
alias.family === 'IPv4' &&
alias.address !== '127.0.0.1' &&
!alias.internal
) {
return alias.address;
}
}
}
return '127.0.0.1';
}
/**
* 格式化服务地址
* @param ip IP地址
* @param port 端口号
* @returns 完整服务地址(如 'http://192.168.1.100:1300'
*/
export function formatServiceUrl(ip: string, port: number): string {
return `http://${ ip }:${ port }`;
}

View File

@@ -0,0 +1,33 @@
// 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)

View File

@@ -0,0 +1,22 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"declaration": true,
// 生成类型声明文件
"declarationDir": "./dist"
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"dist"
]
}