Skip to content

Sup3r-Us3r/pfa-go

Repository files navigation

Golang + Rabbitmq

Golang + Rabbitmq

Overview

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

Install dependencies

$ go mod tidy

Create database

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
)

Rabbitmq

Create orders queue

  1. Go to http://localhost:15672 and log in to the Rabbitmq Management using guest credentials, by default the user is guest and the password is guest.
  2. Choose the Queues tab.
  3. Expand the Add a new queue option and set the queue name to orders.
  4. Click on Add queue.

rabbitmq-create-queue

Create bind

  1. Choose the Exchanges tab.
  2. Click on amq.direct.

rabbitmq-exchange-direct

  1. Expand the Bindings option and define in To queue the name orders.
  2. Click on Bind.

rabbitmq-create-exchange-bind

After creating the bind it will look like this

rabbitmq-exchange-bind-complete

Produce messages

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

Consume messages

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)

Run Prometheus + Grafana

The environments were configured using Docker Compose, to start the environment you must run:

$ docker-compose up

Prometheus

Check Rabbitmq status

  1. Go to http://localhost:9090.
  2. Choose the Status option.
  3. Click on Targets.

Make sure the State column is UP

prometheus-targets

Grafana

Configure Rabbitmq Dashboard

  1. Go to http://localhost:3000.
  2. Click on the Settings icon and choose the Data sources option.

grafana-datasource

  1. Click on Add data source.

grafana-datasource-add

  1. Click on Prometheus.

grafana-datasource-add-prometheus

  1. Update URL field value to http://prometheus:9090.
  2. Click on Save & test.

grafana-datasource-prometheus-settings

  1. Click on the Dashboard icon and choose the + Import option.

grafana-dashboard-import

  1. In the Import via grafana.com field, enter the value 10991 and click on the Load button.

10991 is the Rabbitmq Dashboard ID, you can get this and others from this link: https://grafana.com/grafana/dashboards

grafana-dashboard-load

  1. Select in the prometheus option the Prometheus that was configured there in the Data source section and click on the Import button, you can change the Dashboard name and the location where it will be saved as well.

grafana-dashboard-import-rabbitmq

Rabbitmq Dashboad

grafana-rabbitmq-dashboard

About

Example of application in Golang using Rabbitmq + Concurrency + Channels + Database

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published