This application sends and consumes messages using Message Broker Rabbitmq, along with the power that Golang gives us to deal with situations that we need high performance.
- Rabbitmq to Produce and Consume Messages
- Concurrency using Goroutines
- Channels
- SQLite Database
- HTTP Server
- Workers
- Docker + Docker Compose
Setup Application
$ go mod tidy
The database is already created and available at internal/order/infra/database/sqlite.db
, but if you want to create it from scratch, just follow these steps:
$ cd internal/order/infra/database
$ touch sqlite.db
Now you need to access the database and create the orders
table:
CREATE TABLE orders (
id VARCHAR(255) PRIMARY KEY NOT NULL,
price FLOAT NOT NULL,
tax FLOAT NULL,
final_price FLOAT NOT NULL
)
- Go to http://localhost:15672 and log in to the Rabbitmq Management using guest credentials, by default the user is
guest
and the password isguest
. - Choose the
Queues
tab. - Expand the
Add a new queue
option and set the queue name toorders
. - Click on
Add queue
.
- Choose the
Exchanges
tab. - Click on
amq.direct
.
- Expand the
Bindings
option and define inTo queue
the nameorders
. - Click on
Bind
.
After creating the bind it will look like this
For you to be able to test it, just produce some messages using the producer, it is configured to generate 1.000 messages that will be stored in the orders
queue.
$ go run cmd/producer/main.go
Now to finish, just consume these messages, this procedure will occur concurrently and all messages that are consumed will be stored in the SQLite database.
$ go run cmd/main.go
Setup Prometheus + Grafana (optional)
The environments were configured using Docker Compose, to start the environment you must run:
$ docker-compose up
- Go to http://localhost:9090.
- Choose the
Status
option. - Click on
Targets
.
Make sure the State
column is UP
- Go to http://localhost:3000.
- Click on the
Settings icon
and choose theData sources
option.
- Click on
Add data source
.
- Click on
Prometheus
.
- Update
URL
field value tohttp://prometheus:9090
. - Click on
Save & test
.
- Click on the
Dashboard icon
and choose the+ Import
option.
- In the
Import via grafana.com
field, enter the value10991
and click on theLoad
button.
10991 is the Rabbitmq Dashboard ID, you can get this and others from this link: https://grafana.com/grafana/dashboards
- Select in the
prometheus
option the Prometheus that was configured there in theData source
section and click on theImport
button, you can change the Dashboard name and the location where it will be saved as well.
Rabbitmq Dashboad