1
votes

I am using docker compose with a .net core service and RabbitMQ. I try to connect to rabbitMQ from a different service. I've seen many questions but none of them worked or had an answer.

I know you should not use 'localhost' to connect with another service but the name you gave in the docker-compose file.

I am really curious about the answer!

What I tried

  1. Add depends on in the docker compose file
  2. Add links in the docker compose file
  3. Add restart: always in the docker compose file
  4. Changed the connection link to : amqp://guest:guest@rabbitmq:5672
  5. Changed the connection link to : amqp://guest:guest@rabbitmq/
  6. Cleaned and rebuilt the solution many times
  7. Changed the order in the docker compose file. RabbitMQ is now Nr.1, after that the other services.
  8. Added container_name
  9. Added hostname

Here is my docker compose file

version: '3.0'

services:
  rabbitmq:
    container_name: rabbitmq
    hostname: rabbitmq
    image: rabbitmq:3-management
    ports:
      - "7100:15672"
      - "7101:5672"
    volumes:
      - rabbitmq:/rabbitmq
  dbPosts:
    image: mysql:latest
    ports:
        - 3307:3306
    environment:
        MYSQL_ROOT_PASSWORD: rootpassword
        MYSQL_DATABASE: postsdb
    volumes:
        - dbpostdata:/var/lib/mysql
  post-service:
    depends_on:
        - dbPosts
        - rabbitmq
    build:
      context: .
      dockerfile: post-service/Dockerfile
    ports:
      - "8081:80"
  
volumes:
    dbpostdata:
    rabbitmq:

How the connection is made :


if (_connection == null)
{
   ConnectionFactory factory = new ConnectionFactory()
      {
         Uri = new Uri("amqp://guest:guest@rabbitmq:5672"),
         AutomaticRecoveryEnabled = true
      };
   _connection = factory.CreateConnection();
}

But when I try to run docker-compose up, I receive the following error message:

Unhandled Exception: RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable ---> RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No such device or address
1
To double-check: the code fragment you show is getting run as part of the post-service, and not from a development environment on your host?David Maze
The code with the connection is not from the post-service. It is a different "project", a class library called 'Shared'. I added a reference from post-service to the class library. In the post-service I have this piece of code in the start-up: services.AddMessagePublishing("PostService");. And from that it will make the connection I thinkJay

1 Answers

0
votes

I fixed the issue. I got the wrong package version...