Nestjs中如何将ClientsModuleOptions配置的IP端口替换为环境变量?
Nestjs 微服务客户端配置从环境变量读取实现方案
方案一:官方推荐规范实现(基于@nestjs/config)
步骤1:安装依赖
npm install @nestjs/config
步骤2:根模块注册全局配置模块
修改AppModule.ts,注册ConfigModule为全局可用:
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; @Module({ imports: [ ConfigModule.forRoot({ isGlobal: true, envFilePath: '.env', // 可根据环境切换为.env.development、.env.production等 }), // 原有其他模块配置 ], }) export class AppModule {}
步骤3:修改配置文件为动态工厂
修改原src/utils/gateway/gateway.ts,将静态配置改为可注入ConfigService的工厂函数:
import { ClientsModuleOptions, Transport } from "@nestjs/microservices" import { ConfigService } from '@nestjs/config'; export const getGatewayOptions = (configService: ConfigService): ClientsModuleOptions => [ { name: 'MICRO-ADMIN', transport: Transport.TCP, options: { host: configService.get<string>('MICRO_ADMIN_HOST', '127.0.0.1'), // 第二个参数为默认兜底值 port: configService.get<number>('MICRO_ADMIN_PORT', 4000), }, }, { name: 'MICRO-DEV', transport: Transport.TCP, options: { host: configService.get<string>('MICRO_DEV_HOST', '127.0.0.1'), port: configService.get<number>('MICRO_DEV_PORT', 5000), }, }, ];
步骤4:修改业务模块的注册方式
修改GameModule.ts,将ClientsModule的同步注册改为异步注册,注入ConfigService读取配置:
import { Module } from '@nestjs/common'; import { ClientsModule } from '@nestjs/microservices'; import { ConfigService } from '@nestjs/config'; import { getGatewayOptions } from 'src/utils/gateway/gateway'; import { AuthModule } from './../auth/auth.module'; import { CategoryModule } from './../category/category.module'; import { GameController } from './game.controller'; import { GameService } from './game.service'; @Module({ imports: [ AuthModule, CategoryModule, ClientsModule.registerAsync({ useFactory: (configService: ConfigService) => getGatewayOptions(configService), inject: [ConfigService], }) ], controllers: [GameController], providers: [GameService], exports: [GameService], }) export class GameModule {}
步骤5:新增环境变量配置文件
在项目根目录新建.env文件,填写对应配置:
# 管理端微服务配置 MICRO_ADMIN_HOST=127.20.20.2 MICRO_ADMIN_PORT=4000 # 开发端微服务配置 MICRO_DEV_HOST=127.30.30.3 MICRO_DEV_PORT=5000
方案二:快速实现(直接读取process.env)
如果不需要复杂的配置校验、多环境切换,可以直接读取全局process.env变量,改动更小:
步骤1:提前加载环境变量
在项目入口文件main.ts最顶部引入dotenv加载.env文件:
import * as dotenv from 'dotenv'; dotenv.config(); // 原有main.ts其他代码
步骤2:修改静态配置读取环境变量
直接修改src/utils/gateway/gateway.ts的静态配置即可,无需改动GameModule代码:
import { ClientsModuleOptions, Transport } from "@nestjs/microservices" export const GatewayOptions: ClientsModuleOptions = [ { name: 'MICRO-ADMIN', transport: Transport.TCP, options: { host: process.env.MICRO_ADMIN_HOST || '127.20.20.2', port: parseInt(process.env.MICRO_ADMIN_PORT || '4000', 10), }, }, { name: 'MICRO-DEV', transport: Transport.TCP, options: { host: process.env.MICRO_DEV_HOST || '127.30.30.3', port: parseInt(process.env.MICRO_DEV_PORT || '5000', 10), }, }, ];
注意事项
- 生产环境可直接在部署平台配置系统环境变量,优先级高于.env文件,无需修改代码
- .env文件属于敏感配置,需要加入.gitignore避免提交到代码仓库
- 端口读取后需要转为数字类型,否则TCP传输配置会报错
内容的提问来源于stack exchange,提问作者Jorvi Nogales
相关产品推荐
相关产品推荐

