How to configure RabbitMQ properly

In a project I’m involved in, we started using RabbitMQ to support internal communication between different microservices. At the beginning we have been using rabbitmqadmin to create all the queues at startup — this isn’t a production ready solution. Let’s read how you should configure RabbitMQ to fully control who changed what.

Photo by Sarah Brink on Unsplash

As I mentioned we started with a simple case, right now we are extending the usage of RabbitMQ and adding support for collecting all information and events happening in different parts of our system. We decided to use an exchange of type fanout and a few dedicated queues that receive events.

The whole configuration was done via a batch file using rabbitmqctl and rabbitmqadmin. And yes, we are using Docker to configure all our services. Below is our current Dockerfile before I have introduced configuration via files:

Old Dockerfile

There is a one huge drawback when you want to use those tools — a RabbitMQ server must be up & running, without this you won’t be able to connect to the server and configure anything.

To overcome this issue we simple used asleep command to wait a bit when RabbitMQ will be ready.

Configuring RabbitMQ via a batch file — run.sh

As you see this is a good idea if you want to play with RabbitMQ, but a totally bad idea when you want to run RabbitMQ in production.

Configuration files

RabbitMQ supports few options if you want to configure it via files. First there is a rabbitmq.conf file located in /etc/rabbitmq — it uses a new format available since RabbitMQ 3.7. If you want to use an old format, name your file rabbitmq.config and you can use an Erlang term configuration format instead.

A good idea is to start with an example config file published on Github. You can read throughout it and uncomment and change options you want.

If you need to configure some advanced feature, it’s a good idea to use another file — advanced.conf.

Also plugins allow you to use config files, e.g. if you want to use the Management Plugin (a Rest API along with a Web UI) and configure it via a file, you can define proper options in rabbitmq.conf, as specified on the plugin page.

You can also use the Management Plugin to configure queues, exchanges and policies based on a JSON file. This is the super easy option if you want to have a version controlled configuration. All you have to do is to tell the plugin to load the configuration from the JSON file:

management.load_definitions = /etc/rabbitmq/definitions.json

Add the above line to your rabbitmq.conf file as specified on the plugin page and you are done.

You will probably ask how to prepare the definitions.json file? The easies, and the most convenient way is to use the Management Plugin UI interface to setup everything you need and export the whole configuration into the file.

On the Overview page of the UI, at the bottom, you will find a button Download broker definitions that will export the whole configuration of the RabbitMQ into a JSON file. Just use the file with new options in your Dockerfile as presented below:

Final Dockerfile

Summary

Using cli tools is good if you want to setup RabbitMQ fast and your setup isn’t too complicated. If there are more options and you want to control what and when was changed by who, using configuration files is a far better solution.