Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose with rabbitmq

I'm trying to setup a docker-compose script - to start a dummy: website, API, Gateway and RabbitMQ. (micro service approach)

Request pipeline:

Web >> Gateway >> API >> RabbitMQ

My docker-compose looks like this:

version: "3.4"

services:
  web:
    image: webclient
    build:
      context: ./WebClient
      dockerfile: dockerfile
    ports:
      - "4000:4000"  
    depends_on:
     - gateway

  gateway:
    image: gatewayapi
    build:
      context: ./GateWayApi
      dockerfile: dockerfile
    ports:
      - "5000:5000"
    depends_on:
     - ordersapi

  ordersapi:
    image: ordersapi
    build:
      context: ./ExampleOrders
      dockerfile: dockerfile
    ports:
      - "6002:6002" 
    depends_on:
      - rabbitmq

  rabbitmq:
    image: rabbitmq:3.7-management
    container_name: rabbitmq
    hostname: rabbitmq
    volumes:
      - rabbitmqdata:/var/lib/rabbitmq
    ports:
      - "7000:15672"
      - "7001:5672"
    environment:
      - RABBITMQ_DEFAULT_USER=rabbitmquser
      - RABBITMQ_DEFAULT_PASS=some_password

This pipe works:

Web >> Gateway >> API

I get a response from the API on the website.

But when I try to push a message to rabbitmq from the API, I get the following error:

System.AggregateException: One or more errors occurred. (Connection failed) ---> RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: Connection refused 127.0.0.1:7001

The RabbitMQ managment GUI still works on the defined port 7000. Requests to port 7001 does not.

However, if I start the API and RabbitMQ manually, it works like a charm. The API I simply start with a debugger (.Net core + IIS - default settings hitting F5 in VS) and this is the command I use to start the docker image manually:

docker run -p 7001:5672 -p 7000:15672 --hostname localhost -e RABBITMQ_DEFAULT_USER=rabbitmquser -e RABBITMQ_DEFAULT_PASS=some_password rabbitmq:3.7-management

Update

This is how I inject the config in the .Net core pipe.

startup.cs

public void ConfigureServices(IServiceCollection services)
{
    // setup RabbitMQ
    var configSection = Configuration.GetSection("RabbitMQ");
    string host = configSection["Host"];
    int.TryParse(configSection["Port"], out int port);
    string userName = configSection["UserName"];
    string password = configSection["Password"];

    services.AddTransient<IConnectionFactory>(_ => new ConnectionFactory()
    {
        HostName = host,
        Port = port,
        UserName = userName,
        Password = password
    });

 services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

}

controller.cs

private readonly IConnectionFactory _rabbitFactory;

public ValuesController(IConnectionFactory rabbitFactory)
{
    _rabbitFactory = rabbitFactory;
}

public void PublishMessage()
{
    try
    {
        using (var connection = _rabbitFactory.CreateConnection())
        using (var channel = connection.CreateModel())
        {
            string exchangeName = "ExampleApiController";
            string routingKey = "MyCustomRoutingKey";
            channel.ExchangeDeclare(exchange: exchangeName, type: "direct", durable: true); 
            SendMessage("Payload to queue 1", channel, exchangeName, routingKey);
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.InnerException);
    }
}

private static void SendMessage(string message, IModel channel, string exchangeName, string routingKey)
{
    byte[] body = Encoding.UTF8.GetBytes(message);
    channel.BasicPublish(exchange: exchangeName,
                         routingKey: routingKey,
                         basicProperties: null,
                         body: body);
    Console.WriteLine($" Sending --> Exchange: { exchangeName }    Queue: { routingKey }    Message: {message}");
}
like image 942
Henkolicious Avatar asked Jan 11 '19 12:01

Henkolicious


People also ask

How do I run RabbitMQ with Docker compose?

Open a terminal, navigate to your rabbitmq-go folder and run docker-compose up . This command will pull the rabbitmq:3-management-alpine image, create the container rabbitmq and start the service and webUI. You should see something like this: Once you see this, open your browser and head over to http://localhost:15672.

How do I connect RabbitMQ to docker?

If you open your Docker engine, you will see the RbbitMQ container set and running. If you open http://localhost:15672/ on a browser, you will be able to access the management Ui, and now you can log in using the docker-compose set username and password. And now you can see the RabbitMQ instance is up and running.

Is RabbitMQ a docker?

The RabbitMQ container registry (link) includes a variety of images for different platforms. In this article, we will use the RabbitMQ image with a tag 3-management , which is a Docker image with the RabbitMQ management plugin installed and enabled by default.


1 Answers

I imagine in your caller you set rabbitmq_url to localhost:7001. However, the caller is in a container, which does have anything running on the port 7001. Rabbitmq is running on port 7001 on your host.

You need to change the url to rabbitmq:5672 to use the internal network or use host.docker.internal:7001 if you are using window or mac docker 18.03+

like image 151
Siyu Avatar answered Sep 21 '22 12:09

Siyu