使用 RabbitMq 从 Nodejs 微服务与 NestJs 微服务通信

时间:2021-04-02 23:00:56

标签: node.js rabbitmq microservices nestjs

我正在尝试使用 RabbitMq 从 NestJs 微服务调用特定函数,但我无法使用 nodejs 调用特定的消息模式。

我试图从我的控制器文件访问的消息模式

@MessagePattern('signin')
  async signin(
    @Payload(ValidationPipe) signInCredentialsDto: SignInCredentialsDto,
    @Ctx() context: RmqContext,
  ): Promise<{ accessToken: string }> {
    this.logger.verbose('Signing in ' + JSON.stringify(signInCredentialsDto));
    // const result = await this.authService.userSignin(signInCredentialsDto);
    // const channel = context.getChannelRef();
    // const originalMsg = context.getMessage();

    // channel.ack(originalMsg);
    return {
      accessToken: 'works',
    };
  }

我的 NestJs rabbitmq 连接详情

async function bootstrap() {
  const logger = new Logger('Main');
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(
    AppModule,
    {
      transport: Transport.RMQ,
      options: {
        urls: ['amqp://localhost:5672'],
        queue: 'rebus_queue',
        queueOptions: {
          durable: false,
        },
      },
    },
  );
  app.listen(() => {
    logger.log('Auth Microservice Started Successfully');
  });
}
bootstrap();

我的 nodejs 客户端代码:

amqp.connect("amqp://localhost:5672", (error0, connection) => {
  if (error0) throw error0;

  connection.createChannel((error1, channel) => {
    if (error1) throw error1;

    channel.assertQueue("rebus_queue", {
      durable: false,
    });

    app.get("/", (req: express.Request, res: express.Response) => {});

    app.post(
      "/auth/signin",
      async (req: express.Request, res: express.Response) => {
        channel.sendToQueue(
          "rebus_queue",
          Buffer.from(JSON.stringify(req.body))
        );
      }
    );
  });
});

当我访问 /auth/signin 路由时,我成功访问了 nestjs 微服务,但是我不知道如何访问登录消息模式。

就目前的情况而言,命中 nodejs http 端点会导致 nestjs 终端出现以下错误

[Server] There is no matching event handler defined in the remote service. Event pattern: undefined. +50026ms

0 个答案:

没有答案