This project is a microservices-based system for a coffee shop application, built using Golang with four primary microservices to manage products, orders, product images, and currency conversion. It leverages RabbitMQ for message brokering, PostgreSQL for data storage, gRPC for inter-service communication, and Protocol Buffers (protobufs) for efficient data serialization..
- Microservices Overview
- System Requirements
- Installation & Setup
- Environment Variables
- Usage
- Graceful Shutdown
- License
This service manages CRUD operations for products and initiates order creation. The API is built using the Gorilla Mux router and utilizes PostgreSQL as its database.
Every endpoint in the Product API is documented using Swagger, and the documentation can be accessed at /docs
.
Endpoints:
/products
: CRUD operations for products/orders
: Creates an order and publishes a message to the RabbitMQ exchange
Message Broker: Uses RabbitMQ to publish messages with the orders_topic
topic.
Database: PostgreSQL
The Order Service processes orders by consuming messages from the RabbitMQ queue and handling order confirmation tasks.
Message Consumption: Listens to the orders_topic
queue to process incoming order messages.
Database: Writes order information to PostgreSQL.
Email Confirmation: Sends a confirmation email upon successful order processing.
The Product Images service handles image uploads for products, providing a handler for multipart data uploads.
Endpoint:
/images/{id}/{filename}
: Uploads and serves images associated with a product ID.
File Server: Configured with http.StripPrefix
to serve files from the specified directory.
This service fetches currency rates from the European National Bank and communicates with the Product API via gRPC.
Features:
- Supports unary and bidirectional gRPC streams.
- Enables currency conversion for product prices based on real-time exchange rates.
- Golang: v1.17 or above
- PostgreSQL: v13 or above
- RabbitMQ: v3.8 or above
- gRPC: v1.38 or above
- Clone the repository:
git clone https://github.com/notoriouscode97/go-microservices.git cd go-microservices
- Set up environment variables for each microservice (see below).
- Run
go mod tidy
in each microservice directory to install dependencies. - Start each service:
go run currency/main.go go run product-api/main.go go run order-service/main.go go run product-images/main.go
Ensure RabbitMQ and PostgreSQL are up and running, with connections configured in .env
files for each service.
Each microservice relies on environment variables for configuration. Define these in a .env
file or export them directly.
POSTGRES_HOST
: Hostname for PostgreSQLPOSTGRES_PORT
: Port for PostgreSQLPOSTGRES_USER
: Database user for PostgreSQLPOSTGRES_PASSWORD
: Password for PostgreSQLRABBITMQ_URL
: Connection string for RabbitMQBASE_PATH
: Base directory for image storage in Product Images service
Example .env:
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=password
RABBITMQ_URL=amqp://guest:guest@localhost:5672/
BASE_PATH=./images