使用aws Copilot在ECS上部署Nest Js微服务



嘿,大家好,我正在尝试部署到生产一个基本的nestjs微服务堆栈:一个应用程序是一个基本的nestjs应用程序,它将被用作Api网关,并将通过TCP传输与服务通信第二个应用是一个nestjs微服务

//Gateway/src/main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(8000);
}
bootstrap();

和服务

//Restaurant/src/main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Transport, TcpOptions } from '@nestjs/microservices';
async function bootstrap() {
const app = await NestFactory.createMicroservice(AppModule, {
transport: Transport.TCP,
options: {
host: '0.0.0.0',
port: 8001,
},
} as TcpOptions);
await app.listen();
}
bootstrap();

然后在我的网关中,我在模块中注册微服务,像这样

// Gateway/src/app.module.ts
@Module({
imports: [
ConfigModule.forRoot({
envFilePath: [`.env.stage.${process.env.STAGE}`],
}),
],
controllers: [RestaurantController, AppController],
providers: [
ConfigService,
{
provide: 'RESTAURANT_SERVICE',
useFactory: (configService: ConfigService) => {
return ClientProxyFactory.create({
options: {
host: '0.0.0.0',
port: 8001,
},
});
},
inject: [ConfigService],
},
],
})
export class AppModule {}

当我在本地机器上启动每个应用程序时,所有工作都很完美。现在我使用aws的副驾驶来部署我的api-gw和我的服务到同一个副驾驶应用程序对于api-gw,我选择了负载均衡Web服务对于服务,我选择了后端服务

api-gw manifest file

name: api-gw
type: Load Balanced Web Service

http:
path: '/'

image:
build: Dockerfile
port: 8000

cpu: 256       # Number of CPU units for the task.
memory: 512    # Amount of memory in MiB used by the task.
platform: linux/x86_64  # See https://aws.github.io/copilot-cli/docs/manifest/lb-web-service/#platform
count: 1       # Number of tasks that should be running in your service.
exec: true     # Enable running commands in your container.
network:
connect: true # Enable Service Connect for intra-environment traffic between services.

餐厅服务清单文件

name: restaurant
type: Backend Service
image:
build: Dockerfile
port: 8001
cpu: 256       # Number of CPU units for the task.
memory: 512    # Amount of memory in MiB used by the task.
platform: linux/x86_64     # See https://aws.github.io/copilot-cli/docs/manifest/backend-service/#platform
count: 1       # Number of tasks that should be running in your service.
exec: true     # Enable running commands in your container.
network:
connect: true # Enable Service Connect for intra-environment traffic between services.

两个服务的部署工作正常,但是当我向api-gw发送请求时,api-gw试图连接到餐厅服务,我得到错误

错误:connect ECONNREFUSED 0.0.0.0:8001

就像你看到的,我在manifest文件中为两个服务启用了network true属性

谢谢你的帮助

network字段启用AWS Service Connect。

要使用TCP,请使用NLB(负载均衡Web服务的默认值是ALB)。有关指定NLB的说明,请参阅副驾驶文档页面。

最新更新