实现 nacos 的分布式集群部署配置

This commit is contained in:
编码猿
2025-09-29 05:07:42 +08:00
parent ced7662d98
commit 04754a1f77
82 changed files with 991 additions and 62852 deletions

View File

@@ -0,0 +1,23 @@
功能分析:
1 注册服务
子模块直接通过nacos 注册,给 内网 的 使用
主模块main-app -> Traefik -> ConfigServernacos-federation -> Nacos
2 获取配置
子模块:直接通过 nacos 获取配置
主模块: main-app -> Traefik -> ConfigServernacos-federation -> Nacos
增加功能:
认证授权机制
请求限流保护
操作日志记录
服务治理功能(如权重调整、元数据管理等)

View File

@@ -42,8 +42,8 @@ export class nacosServiceManage {
}
const instance = new nacosServiceManage(option);
await instance.initClient();
await instance.initConfig();
// await instance.initClient();
// await instance.initConfig();
this.instanceMap.set(configKey, instance);
return instance;
@@ -57,6 +57,53 @@ export class nacosServiceManage {
return `${ option.serverList }-${ namespace }-${ option.serviceName }`;
}
// 服务注册
public async initClient() {
this.nacosClient = new NacosNamingClient({
logger,
serverList: this.initOptions.serverList,
namespace: this.initOptions.namespace || 'public',
});
await this.nacosClient.ready();
this.reg()
}
/**
* 获取配置
* @param dataId
* @param group
*/
async getConfig(dataId: string, group: string = "DEFAULT_GROUP") {
const res = await this.configClient.getConfig(dataId, group)
console.log()
console.log(`${ chalk.blue('[nacos-cluster-federation] 配置发现:') }\n ${ chalk.green('➜') } ${ chalk.green(dataId) } 的配置为: ${ chalk.green(res) }`)
return res;
}
/**
* 订阅配置变化
* @param dataId
* @param group
*/
async subscribe(dataId: string, group: string = "DEFAULT_GROUP") {
return new Promise(async (resolve, reject) => {
this.configClient.subscribe({ dataId, group }, (content: any) => {
resolve(content)
});
})
}
/**
* 初始化配置服务
* @private
*/
private async initConfig() {
this.configClient = new NacosConfigClient({
serverAddr: this.initOptions.serverList,
});
}
/**
* 从Nacos发现服务地址
* @param serviceName 发现选项
@@ -87,31 +134,6 @@ export class nacosServiceManage {
nacosServiceManage.instanceMap.delete(configKey);
}
// 初始化服务发现
private async initClient() {
this.nacosClient = new NacosNamingClient({
logger,
serverList: this.initOptions.serverList,
namespace: this.initOptions.namespace || 'public',
});
await this.nacosClient.ready();
this.reg()
}
// 初始化配置服务
private async initConfig() {
this.configClient = new NacosConfigClient({
serverAddr: this.initOptions.serverList,
});
}
async getConfig(dataId: string, group: string) {
const res = await this.configClient.getConfig(dataId, group)
console.log()
console.log(`${ chalk.blue('[nacos-federation] 配置发现:') }\n ${ chalk.green('➜') } ${ chalk.green(dataId) } 的配置为: ${ chalk.green(res) }`)
return res;
}
/**
* 注册服务到Nacos并返回客户端实例
*/
@@ -121,7 +143,7 @@ export class nacosServiceManage {
// @ts-ignore
await this.nacosClient.registerInstance(serviceName, { port, ip })
logger.log()
logger.log(chalk.blue("[nacos-federation] 服务注册:"))
logger.log(chalk.blue("[nacos-cluster-federation] 服务注册:"))
logger.log(` ${ chalk.green('➜') } ${ chalk.green(serviceName) } (${ formatServiceUrl(ip, port) }) 已注册到配置中心!`);
}

View File

@@ -1,5 +1,5 @@
import { loadEnv, Plugin, UserConfig } from 'vite';
import { nacosServiceManage, RegisterOptions } from './index';
import { getLocalIP, nacosServiceManage, RegisterOptions } from './index';
import chalk from 'chalk';
const log = console.log;
@@ -19,7 +19,8 @@ export function nacosRegVitePlugin(options?: Partial<RegisterOptions>): Plugin {
baseUrl = `${ env.VITE_SERVER_NAME }/`
}
return {
base: `${ env.VITE_PREFIX }${ env.VITE_TRAEFIK_ADDRESS }/${ baseUrl }`,
// http(https)://前端运行的服务器所在ip/前端模块名
base: `${ env.VITE_PREFIX }${ getLocalIP() }/${ baseUrl }`,
}
},
async configResolved(config) {
@@ -34,20 +35,19 @@ export function nacosRegVitePlugin(options?: Partial<RegisterOptions>): Plugin {
// 验证必要配置
if (!registerOptions.serverList) {
throw new Error('[nacos-federation] 缺少Nacos服务列表配置请设置VITE_NACOS_ADDRESS环境变量或插件选项');
throw new Error('[nacos-cluster-federation] 缺少Nacos服务列表配置请设置VITE_NACOS_ADDRESS环境变量或插件选项');
}
if (!registerOptions.serviceName) {
throw new Error('[nacos-federation] 缺少服务名称配置请设置VITE_SERVER_NAME环境变量或插件选项');
throw new Error('[nacos-cluster-federation] 缺少服务名称配置请设置VITE_SERVER_NAME环境变量或插件选项');
}
if (!registerOptions.port) {
throw new Error('[nacos-federation] 缺少服务端口配置请设置VITE_SERVER_PORT环境变量或插件选项');
throw new Error('[nacos-cluster-federation] 缺少服务端口配置请设置VITE_SERVER_PORT环境变量或插件选项');
}
// 初始化Nacos实例
nacosInstance = await nacosServiceManage.create(registerOptions);
const baseUrl = await nacosInstance.getConfig("baseUrl", "DEFAULT_GROUP")
const baseUrlConfig = baseUrl ? JSON.parse(baseUrl) : {};
@@ -91,7 +91,7 @@ function Logs(env: Record<string, any>) {
${ chalk.green('➜') } Prometheus 监控: ${ env.VITE_PREFIX }${ webUI }:9090
${ chalk.green('➜') } Grafana 面板: ${ env.VITE_PREFIX }${ webUI }:3000
${ chalk.green('➜') } Traefik 网关: ${ env.VITE_PREFIX }${ env.VITE_TRAEFIK_ADDRESS }
${ chalk.green('➜') } WebUi ${ env.VITE_PREFIX }${ env.VITE_TRAEFIK_ADDRESS }:8080
${ chalk.green('➜') } WebUi ${ env.VITE_PREFIX }${ env.VITE_TRAEFIK_ADDRESS }:8880
${ chalk.blue.bold('微前端信息:') }
${ chalk.green('➜') } 主模块:${ chalk.green(env.VITE_SERVER_NAME) }

View File

@@ -13,7 +13,8 @@
"declarationDir": "./dist"
},
"include": [
"src/**/*"
"src/**/*",
"src/types/*"
],
"exclude": [
"node_modules",