0

I have 3 containers that connect to RabbitMQ: 1 container (Payment service) as the publisher, and 2 containers (Cart and Catalog services) as consumers. They are all in the same network and run using Docker Compose. Weird thing is Payment and Catalog containers have no problems connecting to RabbitMQ container while Cart service connection is always refused. Not sure what's causing this, before containerizing my services all 3 local services had no issue connecting to RabbitMQ container.

Is it possible the issue is because Cart container tried to connect before RabbitMQ in its container finished starting up?

Happened in Cart container only:

amqp.Dial dial tcp 192.168.96.3:5672: connect: connection refused

Docker compose:

cart-service:
    build: 
      context: .
      dockerfile: ./build/cart/Dockerfile
    networks:
      - shopnetwork
    ports:
      - 50052:50052
    env_file:
      - .env
    depends_on:
      - catalog-service
      - payment-service
      - redis
      - rabbitmq
  catalog-service:
    build: 
      context: .
      dockerfile: ./build/catalog/Dockerfile
    networks:
      - shopnetwork
    ports:
      - 50051:50051
    env_file:
      - .env
    depends_on:
      - mongo1
      - mongo2
      - mongo3
      - rabbitmq
  payment-service:
    build: 
      context: .
      dockerfile: ./build/payment/Dockerfile
    networks:
      - shopnetwork
    ports:
      - 50053:50053
    env_file:
      - .env
    depends_on:
      - mongo1
      - mongo2
      - mongo3
      - rabbitmq
  rabbitmq:
    image: rabbitmq:3.9.11-management
    networks:
      - shopnetwork
    ports:
      - 5672:5672
      - 15672:15672

  networks:
    shopnetwork:

My rabbitmq connection initialization code. Payment, Cart, and Catalog all use the same codes for establishing the connection (failed when dialing on line 9):

// NewRabbitMQ instantiates the RabbitMQ instances using configuration defined in environment variables.
func NewRabbitMQ() (*Rabbitmq, error) {
    username := os.Getenv("RABBITMQ_USERNAME")
    password := os.Getenv("RABBITMQ_PASSWORD")
    hostname := os.Getenv("RABBITMQ_HOST")
    port := os.Getenv("RABBITMQ_PORT")

    url := fmt.Sprintf("amqp://%s:%s@%s:%s/", username, password, hostname, port)
    conn, err := amqp.Dial(url) // <----------------- FAILED HERE
    if err != nil {
        return nil, fmt.Errorf("amqp.Dial %w", err)
    }

    ch, err := conn.Channel()
    if err != nil {
        return nil, fmt.Errorf("conn.Channel %w", err)
    }

    err = ch.ExchangeDeclare(
        "tasks", // name
        "topic", // type
        true,    // durable
        false,   // auto-deleted
        false,   // internal
        false,   // no-wait
        nil,     // arguments
    )
    if err != nil {
        return nil, fmt.Errorf("ch.ExchangeDeclare %w", err)
    }

    if err := ch.Qos(
        1,     // prefetch count
        0,     // prefetch size
        false, // global
    ); err != nil {
        return nil, fmt.Errorf("ch.Qos %w", err)
    }

    return &Rabbitmq{
        Conn: conn,
        Channel: ch,
    }, nil
}

My .env for RABBITMQ:

RABBITMQ_USERNAME=guest
RABBITMQ_PASSWORD=guest
RABBITMQ_HOST=rabbitmq
RABBITMQ_PORT=5672
RABBITMQ_MANAGEMENT_PORT=15672
prototype26
  • 121
  • 11
  • 1
    Yes, it's possible you need to wait for RabbitMQ to become available; see [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y). Note that you're getting a "connection refused" error with an IP address, so the `rabbitmq` hostname resolved correctly, which implies the Docker-level wiring is correct. – David Maze Dec 29 '21 at 14:33
  • @DavidMaze Thanks for the reference link. I just added `restart: on-failure` and it's working now. – prototype26 Dec 30 '21 at 03:03

1 Answers1

1

"guest" user can only connect from localhost.

So we need to add a config file to enable the guest user access from outside rabbitmq container.

The contents of the rabbitmq.config:

[
 {rabbit,
  [
   {loopback_users, []},
  ]}
].

and pull the config to rabbitmq container:

  rabbitmq:
    image: rabbitmq:3.9.11-management
    networks:
      - shopnetwork
    ports:
      - 5672:5672
      - 15672:15672
    volumes:
      - ./rabbitmq.config:/etc/rabbitmq/rabbitmq.config

s4eedb
  • 46
  • 4
  • Hi, as I mentioned I have 3 services which are all in their own containers separate from RabbitMQ's. They all use the same RabbitMQ connection codes and credential (guest), it's just one of the three services that is not able to connect. The other 2 can connect just fine even when they use guest credentials. – prototype26 Dec 29 '21 at 17:37