In this post we will create a RabbitMQ instance inside a docker container and run each a consumer and a producer application in .Net to work with this instance.

When you do not know my recipes take a look at the introduction, or check out all recipes there are.

The accompanying git repository can be found here . But as always you can simply follow along the recipe and create everything from scratch.

As usual in the recipe section we will look at the following:

The problem to solve

  1. RabbitMQ in general is a message broker and an awesome one too. If you do not know the concept of RabbitMQ or message brokers in general have a look at the documentation.

    But for starters they help you to separate the communication of distributed applications not only spatial (like with Http or webservices in general) but also temporal with helps for all kinds of things. But to mention at least one you can use it to create event driven systems.

  2. Running RabbitMQ in a container lets you easily set up a testing environment and tearing it down at will

  3. You can simulate your production queue on your local machine to do some exploration testing or simply have it easier to develop (and not stealing messages from other developers or such)

  4. So as in general: Easy to set up local tests or even automated integration tests for CI/CD.

  5. Also to test some new software, in this case a message broker.

This post will mostly concern itself with setting up this scenario. We will not look into internals of RabbitMQ (actually I took the two console apps directly from the documentation) in the future we will explore this kind of scenario in a recipe with MassTransit and a local RabbitMQ.

What

Now lets the see what we will be doing in this recipe

  1. We are going to create dotnet console producer and consumer applications that push and pull messages from the queue.
  2. Then we will create a container that runs a RabbitMQ instance by pulling the rabbitmq:3-management image
  3. Poll until the management web app from RabbitMQ is ready.
  4. Start the producer and consumer application
  5. Automate the process with a bash script

How

And finally the actual recipe (or the How):
You can either follow along with this recipe or pull/clone the github repository.

  1. Create dotnet console applications
  2. Pull rabbitmq image and start the container
  3. Poll for completion
  4. Start the applications

1. ) Create two console applications for the producer and consumer
You can do this with any IDE you are comfortable with, but I am using mostly VS Code on a Macbook so I will use the terminal and dotnet cli for this:

So we created the directory structure, added two new console applications, and added them to the solution.
After that we add the RabbitMQ client package to the project files.
Either by nuget manager or by dotnet cli (you know what I am going to show you by now)

Producer:

 

Consumer:

 

2.) Pull rabbitmq management image

We are getting using image rabbitmq:3-management so we can poll on the web application to have the queue ready for an automated setup scenario of our producer and consumer. There are different ways to go about this.

  • We can pull the container with

    So we can inspect the image
  • Or we can create our own rabbit_mq Dockerfile with the virtual host (not needed here but helpful advice in any case) already set up
  • Start rabbit mq with docker and let docker do the heavy lifting for you

-d lets the container run in the background
– -rm removes the container and all associated ones on stopping the container
-p maps the exposed ports of the container to those of the docker host machine
– -name tags the container for easier reference
rabbitmq:3-management the image used to run the container

3.) Poll for the completion

This is necessary or else the applications will fail with a Socketexception

RabbitMQ Managemt with .Net starting console outputSo start the container like described in step 3.) and then run

until it returns:
Rabbitmq with dotnet started successfully

4.) Start the actual applications

To start the applications when rabbitmq is running do:

  • Start new terminal with root of project
  • Start anpoter terminal with the root of the project

The resulting application in terminals look like:

5.) Automate with bash script

As a last step we can set up a script to run this automatically instead of manually.
The steps are again

  • Setting up the rabbitmq container
  • Polling for the container to be ready by curl the management applicationStart producer
  • Start consumer
  • Stopping (and thereby removing) the container again.

see the following bash file for this:

Caveats

  • Already running local rabbit then do different ports
  • If you start locally remember to add-host or links for your applications
  • Remember to create virtual host
  • If you want to not only do local set password and user

Advanced features

  • Use docker-compose to orchestrate apps and rabbitmq container
  • Masstransit instead of RabbitMQ client library
  • Producer with command line interface to push several messages
Tags: , ,

Leave a Reply