也许我在开始使用Nestjs时遗漏了一个基本的东西,但不知何故,我无法使用以下命令使Nest.js以Pub/Sub模式接收消息:https://github.com/golevelup/nestjs。我这样创建消息服务:
@Module({
imports: [
RabbitMQModule.forRoot(RabbitMQModule, {
exchanges: [
{
name: 'tx-nextor',
type: 'topic',
},
],
uri: RABBIT_URI,
channels: {
'channel-1': {
prefetchCount: 15,
default: true,
},
},
}),
MessagingModule,
],
providers: [],
controllers: [],
})
export class MessagingModule {}
一切都连接正确,我可以在RabbitMQ管理中看到连接和通道。在我创建了一个订户后:
@Injectable()
export class ResourceService {
@RabbitSubscribe({
exchange: 'tx-nextor',
routingKey: 'resource.*',
queue: 'resource-history-resource.*.updated',
})
public async updatedHandler(msg: {}, amqpMsg: ConsumeMessage) {
console.log('Subscribe handler ran');
console.log(JSON.stringify(msg));
console.log(`Correlation id: ${amqpMsg.properties.correlationId}`);
return 'test';
}
}
这也连接:
[resource-history] [Nest] 220 - 07/09/2022, 4:30:47 PM LOG [RoutesResolver] AppController {/}: +8ms
[resource-history] [Nest] 220 - 07/09/2022, 4:30:47 PM LOG [RouterExplorer] Mapped {/, GET} route +3ms
[resource-history] [Nest] 220 - 07/09/2022, 4:30:47 PM LOG [RoutesResolver] HistoryController {/history}: +1ms
[resource-history] [Nest] 220 - 07/09/2022, 4:30:47 PM LOG [RouterExplorer] Mapped {/history/appointment, GET} route +1ms
[resource-history] [Nest] 220 - 07/09/2022, 4:30:47 PM LOG [RouterExplorer] Mapped {/history/test, POST} route +1ms
[resource-history] [Nest] 220 - 07/09/2022, 4:30:47 PM LOG [RabbitMQModule] Initializing RabbitMQ Handlers
[resource-history] [Nest] 220 - 07/09/2022, 4:30:47 PM LOG [RabbitMQModule] Registering rabbitmq handlers from ResourceService
[resource-history] [Nest] 220 - 07/09/2022, 4:30:47 PM LOG [RabbitMQModule] ResourceService.updatedHandler {subscribe} -> tx-nextor::resource.*::resource-history-resource.*.updated
[resource-history] [Nest] 220 - 07/09/2022, 4:30:48 PM LOG [NestApplication] Nest application successfully started +11ms
但执行此操作后,没有记录任何内容。x1c 0d1x
尽管使用者已连接,但所有测试消息仍停留在队列中:
1条答案
按热度按时间nle07wnf1#
将RabbitMQModule添加到Module定义中的导出中为我解决了这个问题。
参考:Problems with RabbitMQ and NestJS. I can't publish a message with nestjs-rabbitmq and NestJS
此外,在RabbitMQ Admin中,确保有效负载的值中有“”,因此它将是“test”与test(发送JSON也应该可以工作)
希望这对你有帮助!