Coder Social home page Coder Social logo

sup3r-us3r / pfa-go Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 1.51 MB

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

License: MIT License

Dockerfile 0.75% Go 99.25%
concurrency docker go-channel go-routine golang grafana prometheus rabbitmq

pfa-go's Introduction

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

pfa-go's People

Contributors

sup3r-us3r avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.