RabbitMQ in Golang: Getting started!
Hey Gophers! 👋 Here’s a quick tutorial to help in setting up RabbitMQ to publish and consume messages in your Golang application.
What is RabbitMQ?
RabbitMQ is one of the most popular open-source, lightweight message brokers which helps in transferring data based on a messaging protocol. RabbitMQ supports multiple messaging protocols, and one of the most commonly used underlying protocol to route messages is Advanced Messaging Queueing Protocol (AMQP) which we will be taking a closer look into with this tutorial.
AMQP has three primary entities – namely: Queues, Exchanges and Bindings. AMQP being a programmable protocol, provides flexibility to configure these entities to help transfer messages from producer to consumer.
When a publisher publishes a message to RabbitMQ, it is first received at an exchange. The exchange routes the message to variously connected queues which is then received by the consumers.
Setting up a RabbitMQ instance locally
To get started, we’ll first need to get a RabbitMQ instance running on our machine that we can interact and play with it. The easiest and quickest approach is to use the docker run
command and specifying the image name that we want to run.
$ docker run -d --hostname my-rabbit --name some-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management
This gets our RabbitMQ instance locally with its management interface available at http://localhost:15672
.
Connecting to RabbitMQ instance
Let’s start getting our hands dirty!
Firstly, we would need to initialize our Go repository and add a file (let’s call it producer.go
) which will act as the entry-point of execution. To communicate with the RabbitMQ instance, we will be leveraging the streadway/amqp package.
To establish a connection, we use the packages’s Dial
function which returns the connection to our RabbitMQ instance. Once included, here’s our resulting code:
On running our code with go run producer.go
we should be able to see the following log on successful connection to the instance:
$ go run main.goRabbitMQ in Golang: Getting started tutorial
Successfully connected to RabbitMQ instance
Publishing a message
To interact with the instance, we would now need to open a channel over the connection created. This channel allows us to publish messages to an exchange and also declare queues to which we can subscribe to.
For this tutorial we will use the default exchange. The default exchange is a pre-declared direct exchange with no name, referred by an empty string. When using the default exchange, the message is delivered to the queue with a name equal to the routing key.
Note: Every queue is automatically bound to the default exchange with a routing key same as the queue name.
For creating a new queue, we use the packages’s QueueDeclare
function defined in the channel with our desired queue properties. Once the queue is created, we can now publish a message over the channel using its Publish
function.
On running our code, we should now be able to see the following logs on successful execution:
Queue status: {testing 1 0}
Successfully published message
Great! When run a couple more times, we should be able to observe the message count increase.
Consuming messages from a queue
Coming to our last step: consuming messages from the queue. Let’s create a consumer (consumer.go
) which connects and has a channel created to talk to our RabbitMQ instance. We can now consume the messages over this channel by the Consume
method defined on it.
We pass in the queue name along with other properties as parameters to the function and listen to it in a loop.
On running our consumer (go run consumer.go
), and then publishing a message with our producer (go run producer.go
), we will now be able to see the message passed over in the terminal output.
Waiting for messages...
Received Message: Test Message
Awesome, we did it! We are now able to publish and subscribe to our RabbitMQ instance. If you’d like a more closer look into the code, you can find the complete Github repository here.
I hope this tutorial was helpful – happy learning! :D