增加任务
This commit is contained in:
0
front-end/nacos-federation/README.md
Normal file
0
front-end/nacos-federation/README.md
Normal file
28
front-end/nacos-federation/package.json
Normal file
28
front-end/nacos-federation/package.json
Normal 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"
|
||||
}
|
||||
2
front-end/nacos-federation/src/index.ts
Normal file
2
front-end/nacos-federation/src/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './nacosServiceManage';
|
||||
export * from './utils';
|
||||
67
front-end/nacos-federation/src/nacosServiceManage.ts
Normal file
67
front-end/nacos-federation/src/nacosServiceManage.ts
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
36
front-end/nacos-federation/src/utils.ts
Normal file
36
front-end/nacos-federation/src/utils.ts
Normal 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 }`;
|
||||
}
|
||||
33
front-end/nacos-federation/test.ts
Normal file
33
front-end/nacos-federation/test.ts
Normal 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)
|
||||
22
front-end/nacos-federation/tsconfig.json
Normal file
22
front-end/nacos-federation/tsconfig.json
Normal 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"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user