mehdihadeli / go-ecommerce-microservices
- понедельник, 28 августа 2023 г. в 00:00:04
🧺 A practical e-commerce microservices, built with golang, domain-driven design, cqrs, event sourcing, vertical slice architecture, event-driven architecture, and the latest technologies.
A practical e-commerce sample, built with Golang and different software architecture and technologies like Microservices Architecture, Vertical Slice Architecture , CQRS Pattern, Domain Driven Design (DDD), Event Sourcing, Event Driven Architecture and Dependency Injection. For communication between independent services, We use asynchronous messaging with using RabbitMQ, and sometimes we use synchronous communication for real-time communications with using REST and gRPC calls.
You can use this project as a template to build your Backend project in the Go language on top of this project.
💡 This application is not `business oriented`` and my focus is mostly on technical part, I just want to implement a sample with using different technologies, software architecture design, principles and all the thing we need for creating a microservices app.
🚀 This Application is in-progress
and I will add new features and technologies over time.
For your simplest golang projects, you can use my go-vertical-slice-template
project:
For more advance projects, with two microservices
and modular monolith architecture
, check the C# version:
Vertical Slice Architecture
as a high level architectureEvent Driven Architecture
on top of RabbitMQ Message Broker with a custom Event BusData Centeric Architecture
based on CRUD in Catalogs Read ServiceEvent Sourcing
in Audit Based
services like Orders ServiceCQRS Pattern
and Mediator Pattern
on top of Go-MediatR libraryDependency Injection
and Inversion of Control
on top of uber-go/fx libraryPostgres
and EventStoreDB
for write databases with fully supports transactions(ACID)MongoDB
and Elastic Search
for read databases (NOSQL)OpenTelemetry
for collection Distributed Tracing
with using Jaeger and ZipkinOpenTelemetry
for collection Metrics
with using Prometheus and GrafanaUnit Test
for testing small units with mocking dependent classes and using Mockery for mocking dependenciesEnd2End Test
and Integration Test
for testing features with all of their real dependeinces using docker containers (cleanup tests) and testcontainers-go libraryZap
and structured loggingViper
for configuration managementdocker-compose
for deploymentDomain Driven Design
in some of services like Catalogs Write Service and Orders ServiceHelm
and Kubernetes
for deploymentOutbox Pattern
for all microservices for Guaranteed Delivery or At-least-once DeliveryInbox Pattern
for handling Idempotency in reciver side and Exactly-once Deliverylabstack/echo
- High performance, minimalist Go web frameworkuber-go/zap
- Blazing fast, structured, leveled logging in Go.emperror/errors
- Drop-in replacement for the standard library errors package and github.com/pkg/errorsopen-telemetry/opentelemetry-go
- OpenTelemetry Go API and SDKopen-telemetry/opentelemetry-go-contrib
- Collection of extensions for OpenTelemetry-Go.rabbitmq/amqp091-go
- An AMQP 0-9-1 Go client maintained by the RabbitMQ team. Originally by @streadway: streadway/amqp
stretchr/testify
- A toolkit with common assertions and mocks that plays nicely with the standard librarymehdihadeli/go-mediatr
- Mediator pattern implementation in Golang and helpful in creating CQRS based applications.grpc-ecosystem/go-grpc-middleware
- Golang gRPC Middlewares: interceptor chaining, auth, logging, retries and moregrpc/grpc-go
- The Go language implementation of gRPC. HTTP/2 based RPCelastic/go-elasticsearch
- The official Go client for Elasticsearchavast/retry-go
- Simple golang library for retry mechanismahmetb/go-linq
- .NET LINQ capabilities in GoEventStore/EventStore-Client-Go
- Go Client for Event Store version 20 and above.olivere/elastic/v7
- Deprecated: Use the official Elasticsearch client for Go atswaggo/swag
- Automatically generate RESTful API documentation with Swagger 2.0 for Go.prometheus/client_golang
- Prometheus instrumentation library for Go applicationsmongodb/mongo-go-driver
- The Go driver for MongoDBgo-redis/redis
- Type-safe Redis client for Golanggo-gorm/gorm
- The fantastic ORM library for Golang, aims to be developer friendlygo-playground/validator
- Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array divingspf13/viper
- Go configuration with fangscaarlos0/env
- A simple and zero-dependencies library to parse environment variables into structs.joho/godotenv
- A Go port of Ruby's dotenv library (Loads environment variables from .env files)mcuadros/go-defaults
- Go structures with default values using tagsuber-go/fx
- A dependency injection based application framework for Go.testcontainers/testcontainers-go
- Testcontainers for Go is a Go package that makes it simple to create and clean up container-based dependencies for automated integration/smoke tests.Each microservices are based on these projects structure:
In this project I used vertical slice architecture or Restructuring to a Vertical Slice Architecture also I used feature folder structure in this project.
Minimize coupling
between slices
, and maximize coupling
in a slice
.Also here I used CQRS for decompose my features to very small parts that makes our application:
With using CQRS, our code will be more aligned with SOLID principles, especially with:
Here instead of some Technical Splitting for example a folder or layer for our services
, controllers
and data models
which increase dependencies between our technical splitting and also jump between layers or folders, We cut each business functionality into some vertical slices, and inner each of these slices we have Technical Folders Structure specific to that feature (command, handlers, infrastructure, repository, controllers, data models, ...).
Usually, when we work on a given functionality we need some technical things for example:
Now we could all of these things beside each other and it decrease jumping and dependencies between some layers or folders.
Keeping such a split works great with CQRS. It segregates our operations and slices the application code vertically instead of horizontally. In Our CQRS pattern each command/query handler is a separate slice. This is where you can reduce coupling between layers. Each handler can be a separated code unit, even copy/pasted. Thanks to that, we can tune down the specific method to not follow general conventions (e.g. use custom SQL query or even different storage). In a traditional layered architecture, when we change the core generic mechanism in one layer, it can impact all methods.
For formatting, I used mvdan/gofumpt, golines and golangci-lint in my GoLand. you can do this formatting automatically by this guide.
For live reloading in dev mode I use air library. for guid about using this tools you can read this article.
For running each microservice in live reload mode
, inner each service folder type bellow command after installing air:
air
The application is in development status. You are feel free to submit pull request or create the issue.
The project is under MIT license.