Here’s a small tutorial on how to do pub/sub with RabbitMQ in golang using go-micro which is a pluggable RPC framework for microservices.
We’ll be creating a service which creates two subroutines. One will be listening on a routing key on a rabbitmq exchange, while another writes to the exchange every second.
Here’s your main.go
Let’s use docker to create the service and dependent servers like rabbitmq and consul which is need by go-micro.
Here’s a simple Dockerfile
Here’s the docker-compose.yml file I used where we can use environment variables to set the rabbitmq connection details.
If you prefer to not use environment variables for setting the rabbitmq address or exchange value, you can do it on the code by instantiating a new rabbitmq broker like this