talen
talen

Reputation: 11

Problem with connection to RabbitMQ on container

UPDATE: As it turns out, the port that rabbitmq uses is not 15672. I changed the port number from 15672 to 5672 in ConnectionFactory block and connected successfully.

I've been trying to design a simple microservices architecture for trying and learning docker & rabbitmq. So I've written these docker-compose.yml file as seen below:

version: '3.4'

networks:
 customqueue:

services:
  feed.api:
    image: feed.api:${TAG:-latest}
    build:
      context: .
      dockerfile: src/Services/Feed/Feed.Api/Dockerfile
    depends_on:
     - sqldata
     - rabbitmq
    ports:
     - "8000:80"
    networks:
     - customqueue

  like.api:
    image: like.api:${TAG:-latest}
    build:
      context: .
      dockerfile: src/Services/Like/Like.Api/Dockerfile
    depends_on:
     - rabbitmq
    ports:
     - "7000:70"
    networks:
     - customqueue

  rabbitmq:
    image: rabbitmq:3-management-alpine
    environment:
      RABBITMQ_DEFAULT_USER: "admin"
      RABBITMQ_DEFAULT_PASS: "password"
    ports:
      - "15672:15672"
      - "5672:5672" 
    networks:
     - customqueue 

feed.api is designed to be a subscriber, like.api is designed to be a publisher. However, when i'm trying to run .net core code of feed.api, i'm getting this "None of the endpoints were reachable" error with RabbitMQ. RabbitMQ on container works fine. I'm trying to define a ConnectionFactory as below on Startup.cs in Feed.Api project.

var factory = new ConnectionFactory()
{
    HostName = "rabbitmq",
    UserName = "admin",
    Password = "password",
    Port = 15672,
    Protocol = Protocols.DefaultProtocol,
    RequestedConnectionTimeout = 2000,
    VirtualHost = "/",
};

Note:

EDIT:rabbimq:3-managament-alpine is appearently is an old image. Updating this to latest version might help, but i'm not sure. Has anyone have an idea about it?

Upvotes: 1

Views: 1113

Answers (1)

frsechet
frsechet

Reputation: 790

add a links: section to the rabbitmq container from the apis, otherwise they have no idea about the "rabbitmq" hostname.

Links are getting deprecated in docker commands, but not in docker-compose.

  feed.api:
    image: feed.api:${TAG:-latest}
    build:
      context: .
      dockerfile: src/Services/Feed/Feed.Api/Dockerfile
    depends_on:
     - sqldata
     - rabbitmq
    links:
     - rabbitmq
    ports:
     - "8000:80"
    networks:
     - customqueue

  like.api:
    image: like.api:${TAG:-latest}
    build:
      context: .
      dockerfile: src/Services/Like/Like.Api/Dockerfile
    depends_on:
     - rabbitmq
    ports:
     - "7000:70"
    links:
     - rabbitmq
    networks:
     - customqueue

  rabbitmq:
    image: rabbitmq:3-management-alpine
    environment:
      RABBITMQ_DEFAULT_USER: "admin"
      RABBITMQ_DEFAULT_PASS: "password"
    ports:
      - "15672:15672"
      - "5672:5672" 
    networks:
     - customqueue 

Upvotes: 1

Related Questions