如何在 Nest App 中使用 RabbitMQ 和 REST-API?

how to use RabbitMQ and REST-API in Nest App?

美好的一天!

我正在尝试实施 2 microservices that communicate with each other through a message brokerBut one of them should accept Http requests via REST-Api。不幸的是,我不明白如何让微服务同时监听消息队列和传入的 HTTP 请求。也许我对消息代理通信的范式有些不理解,但是如何接收来自客户端的请求并将它们转发给微服务架构?

Main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import {Transport, MicroserviceOptions} from '@nestjs/microservices'

async function bootstrap() {
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
    transport: Transport.RMQ,
    options: {
      urls: ['amqp://rabbitmq:5672'],
      queue: 'hello_world',
      queueOptions: {
        durable: false
      },
    },
  });
  await app.listen();
}
bootstrap();

如您所见,现在应用程序没有像标准方法那样侦听端口 3000。应该怎么办?

原来答案很简单。 NEST js 具有混合应用程序。你可以在这里了解他们 https://docs.nestjs.com/faq/hybrid-application#hybrid-application。谢谢大家

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import {Transport, MicroserviceOptions} from '@nestjs/microservices'

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  const microservice = app.connectMicroservice({
    transport: Transport.RMQ,
    options: {
      urls: ['amqp://rabbitmq:5672'],
      queue: 'hello_world',
      queueOptions: {
        durable: false
      },
    },
  });

  await app.startAllMicroservices();
  await app.listen(3000);
}
bootstrap();