Microservice connection rabbitMQ

63 views Asked by At

I have two services gateway and users that are supposed to communicate with each other through rabbitMQ:

gateway:

app.module.ts:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ClientsModule } from '@nestjs/microservices';
import { UsersController } from './users/users.controller';

@Module({
  imports: [
    ClientsModule.register([
      {
        name: 'USERS_SERVICE',
        options: {
          urls: ['amqp://localhost:5672/hello'],
          queue: 'users_queue',
          queueOptions: { durable: false },
        },
      },
    ]),
  ],
  controllers: [AppController, UsersController],
  providers: [AppService],
})
export class AppModule {}



users.controller.ts:

@Controller('users')
export class UsersController {
  constructor(@Inject('USERS_SERVICE') private usersClient: ClientProxy) {}

  @Post()
  create(@Body() createUserDto: CreateUserDto): Observable<object> {
    return this.usersClient.send('createUser', createUserDto);
  }

  @Get()
  findAll(): Observable<object> {
    return this.usersClient.send('findAllUsers', {});
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.usersClient.send('findOneUser', id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.usersClient.send('updateUser', { id, updateUserDto });
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.usersClient.send('removeUser', id);
  }
}

users:


main.ts:

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

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



users.controller.ts:

@MessagePattern('findAllUsers')
  findAll() {
    return this.usersService.findAll();
  }



users.service.ts:

findAll() {
    return `This action returns all users`;
  }

Now, my problem is when I send this GET request http://localhost:3000/users, I have this error in gateway service:

[Nest] 21343 - 11/01/2023, 10:24:17 PM ERROR [ExceptionsHandler] Connection closed Error: Connection closed at ClientTCP.handleClose (/home/arta/project/vsCode/nestjs/microHub/gateway/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_amqp-connection-manage_a4katzriqmc2wl6sp3kq4c2z64/node_modules/@nestjs/microservices/client/client-tcp.js:102:25) at Socket. (/home/arta/project/vsCode/nestjs/microHub/gateway/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_amqp-connection-manage_a4katzriqmc2wl6sp3kq4c2z64/node_modules/@nestjs/microservices/client/client-tcp.js:92:55) at Socket.emit (node:events:526:35) at TCP. (node:net:337:12) ^C ELIFECYCLE  Command failed.

(I have rabbitMQ running on the system).

But I expect return "This action returns all users".

Why am I facing this problem?

0

There are 0 answers