golevelup / nestjs

A collection of badass modules and utilities to help you level up your NestJS applications 🚀
MIT License
2.16k stars 244 forks source link

Problem to connect RabbitMQ in Docker #744

Closed Nikita-Ma closed 1 month ago

Nikita-Ma commented 1 month ago

Hello. I would be glad if you could help me with the question of connecting RabbitMQ and Node js (nest js). Everything works locally, but when using docker-compose. It turns out that authorization is successful, and then TCP connections are closed. Here are the logs and my docker-compose

logs rmq container

2024-06-09 20:04:27 2024-06-09 17:04:27.641575+00:00 [info] <0.991.0> connection <0.991.0> (192.168.128.4:36792 -> 192.168.128.2:5672): user 'your_rabbitmq_username' authenticated and granted access to vhost '/'
2024-06-09 20:04:28 2024-06-09 17:04:28.250978+00:00 [warning] <0.978.0> closing AMQP connection <0.978.0> (192.168.128.4:36780 -> 192.168.128.2:5672, vhost: '/', user: 'your_rabbitmq_username'):
2024-06-09 20:04:28 2024-06-09 17:04:28.250978+00:00 [warning] <0.978.0> client unexpectedly closed TCP connection
2024-06-09 20:04:32 2024-06-09 17:04:32.587656+00:00 [warning] <0.991.0> closing AMQP connection <0.991.0> (192.168.128.4:36792 -> 192.168.128.2:5672, vhost: '/', user: 'your_rabbitmq_username'):
2024-06-09 20:04:32 2024-06-09 17:04:32.587656+00:00 [warning] <0.991.0> client unexpectedly closed TCP connection

logs nesjs

[Nest] 163  - 06/09/2024, 5:08:27 PM   ERROR [ExceptionHandler] Failed to connect to a RabbitMQ broker within a timeout of 5000ms
Error: Failed to connect to a RabbitMQ broker within a timeout of 5000ms

uri connect

uri:  'amqp://your_rabbitmq_username:your_rabbitmq_password@rabbitmq-container:5672',

docker-compose

version: '3.3'

services:
  auth-service:

    ports:
      - "3000:3000" 
    env_file:
      - ./auth-service/.env.production
    depends_on:
      - rabbitmq

  rabbitmq:
    image: rabbitmq:3.13.3-management
    container_name: rabbitmq-container
    ports:
      - "5672:5672"
      - "15672:15672"
    environment:
      RABBITMQ_DEFAULT_USER: your_rabbitmq_username
      RABBITMQ_DEFAULT_PASS: your_rabbitmq_password

app.module.ts

 RabbitMQModule.forRoot(RabbitMQModule, {
            exchanges: [
                {
                    name: 'AUTH',
                    type: 'topic',
                },
            ],
            uri:  'amqp://your_rabbitmq_username:your_rabbitmq_password@rabbitmq-container:5672',
        }),
Nikita-Ma commented 1 month ago

It was the problem with .env config DB. I hope it's safe your time! (This problem ate up 5 hours of my life)

Nikita-Ma commented 1 month ago

This bug happening when I use TypeOrm package. When without him It's work. 🫠

Nikita-Ma commented 1 month ago

I think this could happen due to TypeOrm I used entities and migrates together.