I'm building a NestJS app with the @golevelup/nestjs-rabbitmq
library to publish messages to a rabbitmq exchange.
我正在使用@goevelup/nestjs-rabbitmq库构建一个NestJS应用程序,以便将消息发布到rabbitmq交易所。
I'm importing and configuring the RabbitMQ module in the AppModule (this part appears to work correctly).
我正在AppModule中导入和配置RabbitMQ模块(这部分似乎工作正常)。
I've created a module called MessagingModule and a service MessagingService. However, when I attempt to create the MessagingService and inject the AmqpConnection object into the constructor, there is an error.
我已经创建了一个名为MessagingModule的模块和一个服务MessagingService。但是,当我尝试创建MessagingService并将AmqpConnection对象注入构造函数时,出现错误。
Error:
错误:
error: Nest can't resolve dependencies of the RabbitMQModule (DiscoveryService, ExternalContextCreator, ?, AmqpConnectionManager). Please make sure that the argument RabbitRpcParamsFactory at index [2] is available in the RabbitMQModule context.
Potential solutions:
- Is RabbitMQModule a valid NestJS module?
- If RabbitRpcParamsFactory is a provider, is it part of the current RabbitMQModule?
- If RabbitRpcParamsFactory is exported from a separate @Module, is that module imported within RabbitMQModule?
App module
应用程序模块
import { ConfigModule } from '@nestjs/config';
import { configuration } from './config/configuration';
import { MessagingModule } from './messaging/messaging.module';
import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq';
import { ConfigService } from '@nestjs/config';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
cache: true,
load: [configuration],
validationSchema,
validationOptions: {
allowUnkown: false,
abortEarly: true,
},
}),
RabbitMQModule.forRootAsync(RabbitMQModule, {
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
exchanges: [
{
name: configService.get<string>('RABBITMQ_EXCHANGE'),
type: configService.get<string>('RABBITMQ_EXCHANGE_TYPE'),
},
],
uri: configService.get<string>('RABBITMQ_URL'),
channels: {
'channel-1': {
prefetchCount: 15,
default: true,
},
'channel-2': {
prefetchCount: 2,
},
},
connectionInitOptions: { wait: false },
enableControllerDiscovery: true,
}),
}),
MessagingModule,
],
controllers: [],
providers: [],
})
export class AppModule {}
Messaging module
消息模块
import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq';
import { Module } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { MessagingService } from './messaging.service';
import { MessagingController } from './messaging.controller';
@Module({
imports: [
RabbitMQModule,
MessagingModule,
],
providers: [ConfigService, MessagingService],
controllers: [MessagingController],
exports: [MessagingService],
})
export class MessagingModule {}
Messaging Service
消息传送服务
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
@Injectable()
export class MessagingService {
constructor(private readonly amqpConnection: AmqpConnection) {}
// Publish the event to the exchange
public async publish<T>(exchange: string, event: T): Promise<void> {
await this.amqpConnection.publish(exchange, '', event);
}
}
更多回答
优秀答案推荐
Every time you want to use the RabbitMQModule
, you need to use the forRoot
/forRootAsync
method to ensure all of the providers the module needs are set up. If you want to only need to call this once, you can set up a wrapper module for it like so:
每次你想使用RabbitMQModule时,你都需要使用forRoot/forRootAsync方法来确保模块所需的所有提供程序都已设置。如果你想只调用一次,你可以为它设置一个包装器模块,如下所示:
@Module({
imports: [
RabbitMQModule.forRootAsync(RabbitMQModule, {
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
exchanges: [
{
name: configService.get<string>('RABBITMQ_EXCHANGE'),
type: configService.get<string>('RABBITMQ_EXCHANGE_TYPE'),
},
],
uri: configService.get<string>('RABBITMQ_URL'),
channels: {
'channel-1': {
prefetchCount: 15,
default: true,
},
'channel-2': {
prefetchCount: 2,
},
},
connectionInitOptions: { wait: false },
enableControllerDiscovery: true,
}),
}),
],
exports: [RabbitMQModule],
})
export class RabbitModule {}
Now you can add the RabbitModule
instead of RabbitMQModule
and you'll get the same RabbitMQModule
that's pre-configured each time.
现在,您可以添加RabbitModule而不是RabbitMQModule,您将得到每次都预先配置的RabbitMQModule。
更多回答
我是一名优秀的程序员,十分优秀!