A practical and imaginary microservices for implementing an infrastructure for up and running distributed system with the latest technology and architecture like Vertical Slice Architecture, Event Driven Architecture, CQRS, Postgres, RabbitMq in Golang. 🚀
💡 This project is not business-oriented and most of my focus was in the thechnical part for implement a distributed system with a sample project. In this project I implemented some concept in microservices like Messaging, Tracing, Event Driven Architecture, Vertical Slice Architecture, CQRS.
- The Goals of This Project
- Plan
- Technologies - Libraries
- The Domain and Bounded Context - Service Boundary
- Structure of Project
- How to Run
- Documentation Apis
- Support
- Contribution
- ❇️ Using
Vertical Slice Architecture
forarchitecture level
. - ❇️ Using
Rabbitmq
forEvent Driven Architecture
between our microservices withstreadway/amqp
library. - ❇️ Using
gRPC
forinternal communication
between our microservices withgrpc/grpc-go
library. - ❇️ Using
CQRS
implementation withmehdihadeli/Go-MediatR
library. - ❇️ Using
Postgres
fordatabase
in our microservices withgo-gorm/gorm
library. - ❇️ Using
go-playground/validator
forvalidating input
data in the REST calls. - ❇️ Using
OpenTelemetry
fordistributed tracing
top ofJaeger
. - ❇️ Using
OAuth2
for implementationauthentication
andauthorization
withgo-oauth2/oauth2
library. - ❇️ Using
Echo framework
forRESTFul api
. - ❇️ Using
Swagger
withswaggo/swag
library for api documentation. - ❇️ Using
uber-go/fx
library fordependency injection
. - ❇️ Using
Viper
forconfiguration management
. - ❇️ Using
logrus
as astructured logger
. - ❇️ Using
Unit Testing
,Integration Testing
andEnd To End Testing
for testing level. - ❇️ Using
Docker-Compose
for ourdeployment
mechanism. - 🚧 Using
OpenTelemetry
formonitoring
top ofPrometteuse
andGrafana
- 🚧 Using
MongoDB
for read side withmongo-driver
. - 🚧 Using
Domain Driven Design
(DDD) to implement allbusiness
processes in microservices. - 🚧 Using
Inbox Pattern
for ensuring message idempotency for receiver andExactly once Delivery
. - 🚧 Using
Outbox Pattern
for ensuring no message is lost and there is atAt Least One Delivery
.
🌀This project is a work in progress, new features will be added over time.🌀
I will try to register future goals and additions in the Issues section of this repository.
- ✔️
labstack/echo
- High performance, minimalist Go web framework - ✔️
go-gorm/gorm
- The fantastic ORM library for Go, aims to be developer friendly - ✔️
sirupsen/logrus
- Logrus is a structured logger for Go - ✔️
streadway/amqp
- Go RabbitMQ Client Library - ✔️
spf13/viper
- Go configuration with fangs - ✔️
swaggo/echo-swagger
- Echo middleware to automatically generate RESTful API documentation - ✔️
mehdihadeli/Go-MediatR
- This package is a Mediator Pattern implementation in Go - ✔️
go-playground/validator
- Implements value validations for structs and individual fields based on tags - ✔️
open-telemetry/opentelemetry-go
- Implementation of OpenTelemetry in Go for distributed-tracing - ✔️
meysamhadeli/problem-details
- Error Handler for mapping our error to standardized error payload to client - ✔️
go-resty/resty
- Simple HTTP and REST client library for Go (inspired by Ruby rest-client) - ✔️
grpc/grpc-go
- The Go language implementation of gRPC. HTTP/2 based RPC - ✔️
go-oauth2/oauth2
- An open protocol to allow secure authorization in a simple and standard method - ✔️
stretchr/testify
- A toolkit with common assertions and mocks that plays nicely with the standard library - ✔️
uber-go/fx
- Fx is a dependency injection system for Go - ✔️
cenkalti/backoff
- This is a Go port of the exponential backoff algorithm - ✔️
stretchr/testify
- A toolkit with common assertions and mocks that plays nicely with the standard library - ✔️
testcontainers/testcontainers-go
- it's a package to create and clean up container for automated integration/smoke tests - ✔️
avast/retry-go
- Simple golang library for retry mechanism - ✔️
ahmetb/go-linq
- .NET LINQ capabilities in Go
In this project I used vertical slice architecture and feature folder structure to structure my files.
I used RabbitMQ as my MessageBroker for async communication between microservices using the eventual consistency mechanism.
Microservices are event based
which means they can publish and/or subscribe to any events occurring in the setup. By using this approach for communicating between services, each microservice does not need to know about the other services or handle errors occurred in other microservices.
I treat each request as a distinct use case or slice, encapsulating and grouping all concerns from front-end to back.
When adding or changing a feature in an application in n-tire architecture, we are typically touching many "layers" in an application. We are changing the user interface, adding fields to models, modifying validation, and so on. Instead of coupling across a layer, we couple vertically along a slice. We minimize coupling
between slices
, and maximize coupling
in a slice
.
With this approach, each of our vertical slices can decide for itself how to best fulfill the request. New features only add code, we're not changing shared code and worrying about side effects.
Instead of grouping related action methods in one endpoint, I used the REPR pattern. Each action gets its own small endpoint, and for communication between our endpoint and handlers, I use Go-MediatR for decouple our endpoint to handlers directly, and it gives use some pipeline behavior for logging, caching, validation and... easily.
The use of the mediator pattern in my endpoints creates clean and thin endpoint. By separating action logic into individual handlers we support the Single Responsibility Principle and Don't Repeat Yourself principles, this is because traditional controllers tend to become bloated with large action methods and several injected Services
only being used by a few methods.
I used CQRS to decompose my features into small parts that makes our application:
- Maximize performance, scalability and simplicity.
- Easy to maintain and add features to. Changes only affect one command or query, avoiding breaking changes or creating side effects.
- It gives us better separation of concerns and cross-cutting concern (with help of mediatr behavior pipelines), instead of bloated service classes doing many things.
Using the CQRS pattern, we cut each business functionality into vertical slices, for each of these slices we group classes (see technical folders structure) specific to that feature together (command, handlers, infrastructure, repository, controllers, etc). 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 postgresql 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.
Use the command below to run our infrastructure
with docker
using the infrastructure.yaml file at the root
of the app:
docker-compose -f ./deployments/docker-compose/infrastructure.yaml up -d
I will add docker-compsoe
for up and running whole app here in the next...
To build
each microservice, run this command in the root directory of each microservice where the go.mod
file is located:
go build -v ./...
To run
each microservice, run this command in the root of the microservice where go.mod
is located:
go run -v ./...
To test
each microservice, run this command in the root directory of the microservice where the go.mod
file is located:
go test -v ./...
Each microservice has a Swagger OpenAPI
. Browse to /swagger/index.html
for a list of endpoints.
Note: For generate Swagger OpenAPI, we need to install
swag cli
with this command below:
go install github.com/swaggo/swag/cmd/[email protected]
As part of API testing, I created the shop.rest file which can be run with the REST Client VSCode plugin
.
If you like my work, feel free to:
- ⭐ this repository. And we will be happy together :)
Thanks a bunch for supporting me!
Thanks to all contributors, you're awesome and this wouldn't be possible without you! The goal is to build a categorized community-driven collection of very well-known resources.
Please follow this contribution guideline to submit a pull request or create the issue.
- https://github.com/golang-standards/project-layout
- https://github.com/AleksK1NG/Go-Elasticsearch-RabbitMQ
- https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example
- https://github.com/kgrzybek/modular-monolith-with-ddd
- https://github.com/jbogard/ContosoUniversityDotNetCore-Pages
This project is made available under the MIT license. See LICENSE for details.