Nestjs RabbitMQ 无法发送到队列

问题描述 投票:0回答:2

我知道有类似的问题,但到目前为止还没有对我有用。 我有一个 NestJS 项目,该项目应该获取 HTTP 请求并将其发送到 RabbitMQ。 RabbitMQ 已设置,并且侦听 C# 脚本可以连接到它。我在 RabbitMQ 的 Web 界面中看到了连接。但是当我在 AppService 中调用函数

sendIntoQueue()
时,什么也没有发生。不,那么我在哪里错过了什么?

这个应用程序没有收到任何东西,这是对的。它应该只发送到队列。

main()
看起来应该正确吗?

这些是我的文件:

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

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

  app.listen(3000);
}
bootstrap();
import { Module } from "@nestjs/common";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";
import { ClientsModule, Transport } from "@nestjs/microservices";


const user = 'quest';
const password = 'quest';
const host = '127.0.0.1:5672';
const queueName = 'dev';
@Module({
  imports: [
    ClientsModule.registerAsync([
      {
        name: "RMQ_CLIENT",
        imports: [],
        useFactory: () => ({
          transport: Transport.RMQ,
          options: {
            urls: [`amqp://${user}:${password}@${host}`],
            queue: queueName,
            queueOptions: {
              durable: false,
            },
          },
        }),
      }
    ])
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
import { Inject, Injectable } from "@nestjs/common";
import { PdfRequest } from "./model/pdf-request";
import { ClientProxy } from "@nestjs/microservices";
import { PdfEvent } from "./events/pdf-event";
import { PdfEventPattern } from "./events/pdf-event-pattern";

@Injectable()
export class AppService {
  private readonly queue: any[] = [];

  constructor(@Inject("RMQ_CLIENT") private readonly pdfClient: ClientProxy) {
  }

  async sendIntoQueue(pdfRequest: PdfRequest) {
    try {
      this.queue.push(pdfRequest);
      let result = this.pdfClient.send("test", {"test": "test"});
    } catch (e){
      console.log(e);
    }
  }
}

angular typescript rabbitmq nestjs
2个回答
0
投票

您可以尝试修改以下行吗?

import { Inject, Injectable } from "@nestjs/common";
import { PdfRequest } from "./model/pdf-request";
import { ClientProxy } from "@nestjs/microservices";
import { PdfEvent } from "./events/pdf-event";
import { PdfEventPattern } from "./events/pdf-event-pattern";

@Injectable()
export class AppService {
  private readonly queue: any[] = [];

  constructor(@Inject("RMQ_CLIENT") private readonly pdfClient: ClientProxy) {
  }

  async sendIntoQueue(pdfRequest: PdfRequest) {
    try {
      this.queue.push(pdfRequest);
      let result = await this.pdfClient.send("test", {"test": "test"});
    } catch (e){
      console.log(e);
    }
  }
}

0
投票

您的问题可能是

send
返回一个 rxjs
Observable
,但您正在丢弃它并且它永远不会被订阅。使用的模式可能是

this.pdfClient
   .send("test", {"test": "test"})
   .subscribe(response => {
       console.log('Response:', JSON.stringify(response));
  });
© www.soinside.com 2019 - 2024. All rights reserved.