Published in


Add RabbitMQ and gocron to your DigitalOcean droplet [Part 2]

Configuring RabbitMQ

(This is part 2 of a 3 part series. You can read the first part here)

Photo by Daryan Shamkhali on Unsplash

Before we start

For working with your DigitalOcean droplet, you need to SSH into your server. I advise you to create a new admin user while doing this. Using the root user is fine if you know what you are doing. If you are unsure, you will find some useful links in the references section.

Configuring RabbitMQ

Let’s start configuring RabbitMQ on our server. RabbitMQ usually comes installed with your DO depending on how up-to-date your Linux distribution is.

You can confirm this by running the status command and checking for the output:

$ sudo rabbitmqctl status | grep rabbit

If RabbitMQ is not installed, simply run:

$ sudo apt-get install rabbitmq-server

Now that RabbitMQ is installed, we need to create a user before establishing a connection. We will use RabbitMQ’s command line management tool rabbitmqctl for this:

rabbitmqctl add_user <USERNAME> <PASSWORD>

Using the same tool, give admin rights to your user:

rabbitmqctl set_user_tags <USERNAME> administrator

In this last step, we will give permissions to our admin user. RabbitMQ permissions are classified as configuration, read and write permissions. For this application, it is tolerable to grant all, but this is a security risk in a production application.

rabbitmqctl set_permissions -p / <USERNAME> ".*" ".*" ".*"

Now that we created our user, let’s save the connection string in our application's environment variables. We will be using .env file for this. Add the below line to your .env file in your application:


See the amqp prefix? It stands for “The Advanced Message Queuing Protocol”. This is the default protocol that is used by RabbitMQ. We will be using this with amqp client library for go.

Before we implement the enqueue functionality, let’s confirm if we can check our queues:

$ sudo rabbitmqctl list_queuesTimeout: 60.0 seconds ...
Listing queues for vhost / ...
name messages

Since we have not created a queue yet, we are not expecting to see anything listed by this command.

Enqueueing messages

We will be creating an enqueue function that handles the message production. You can add enqueue() function call to one of your API controllers if you want to test this with your server. I am using POST /fallback endpoint to test this.

Let’s break down the key parts of the function that sends messages to "FallbackAPIQueue".

On line 2, we retrieve our connection URL AMQP_SERVER_URL from environment variables.

Next, we establish a connection using amqp.Dial() . We are not done yet. With RabbitMQ, most of the operations are handled over channels. We can retrieve a channel using Channel() on our connection (on line 8)

Lastly, we can defer Close() functions on the connection and the channel to make sure the connection is closed properly after we submit our message to the queue.

On line 13, we declare the queue to which we want to submit messages. For this, we use AMQP’s func (*Channel)[QueueDeclare] function.

_, err = channelRabbitMQ.QueueDeclare(
true, // durable
false, // auto delete
false, // exclusive(only declaring channel can access the queue)
false, // no wait
nil, // table

This function creates the queue with the given name if it is not already been created.

On line 24, we define our message using amqp.Publishing. We will be using a simple plain text message.

message := amqp.Publishing{
ContentType: "text/plain",
Body: []byte("A request has been sent via fallback API"),

Lastly, we can publish our message calling Publish on our channel.

"", // exchange
"FallbackAPIQueue", // Queue
false, // mandatory
false, // immediate

When the function is triggered the messages will be enqueued to our "FallbackAPIQueue"We can see the queue and the number of messages by using the same command:

➜  ~ sudo rabbitmqctl list_queues
Timeout: 60.0 seconds ...
Listing queues for vhost / ...
name messages
FallbackAPIQueue 3

With this, we now completed the message publishing mechanism 🚀

In the next chapter, we will look into consuming these messages on a daily basis using gocron package.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alican Sungur

Alican Sungur

Co-Creator of Bastion. Distributed systems, and other programming related things