Introduction
What is messaging or message broker? For this definition you can read in wikipedia actually hehe, the point from message broker is like postman, postman task is deliver letter user A to user B. Message broker commonly use like RabbitMQ, Kafka, NSQ but this article only explain about RabbitMQ.
Why we need Message broker
Okey well, actually if we build some service without message broker can running well but some case we need message broker, okey this is example service without message broker, verification email usually flow is user register like this:
you can see this flow, user register and input email, and after this user click submit and user must be waiting for response from server until email sender to user, how about so many user register together, how about server down and after user click submit register hmmm interest hehe, this user can’t verification email, because server down, if we use message broker message verification user saved in message broker, until server running well like this flow
What is RabbitMQ
What is rabbitMQ ?, you can read documentation in this link, the point is rabbitMQ is tools for message broker, rabbitmq have 2 entity commonly named producer and consumer. Producer is service send some data, and consumer is receiver data is send producer, you can see this complete flow:
Okey after we learn about message broker, flow rabbitmq, this time we will convert rabbitmq and Golang (you can see what is golang this link). This apps we will build producer and consumer with Golang.
Make tree folder
Fist time you can get plugins/ repo with this command
go get github.com/streadway/amqp
After that you create project folder go-rabbitmq
and you can see this tree
you can don’t care file LICENSE
and README.md
but if you create this file its okey nexxxtt, you can see this this project having 2 folder consumer
and producer
. folder consumer will consume data and will print this console and folder produce will send some data and done hehe.
Create Producer
First time we will create produce first, create file is name main.go
in folder produce. After create file main.go
then create connection to rabbitmq and make a channel, you can write this code same like this:
You can see rabbitmq this installed in local computer, after connected make channel for rabbitmq, after make channel make queue declare like this code:
You can see name TRY-QUEUE
is name for messaging, important: this name must be same with consumer, if different its mean 2 message
Create Consumer
Second flow we will create consumer
, consumer for consume messaging sending from producer. Consumer will create in same folder go-rabbitmq
oke and create file main.go
in folder consumer
like tree
First time same like Producer make connection and channel rabbitMQ:
You can see same like Producer, but after this different:
Oke well done, all code already done and starting testing, first running Producer with command:
go run producer/main.go
You see in this terminal running this command and nothing happen, but actually this producer already sending message to rabbitMQ, you can see in rabbitmq console:
Console RabbitMQ already TRY-MESSAGING
, this means you successfull sending message to rabbitMQ with producer, and now we must be running consumer for consume this message, you can run with command
go run consumer/main.go
You can see, message from producer catch it with consumer, this consumer running forever and waiting message again from producer, if producer sending message again, this consumer already catch message.
DONE yeeeeee, you already understanding about messaging rabbitmq with Golang, this source already in github
If you have some discussion ||~
Just Contact Me At:
- Email: cakazies@gmail.com
- Instagram : @cakazies