Skip to content

xbucks/go-microservices

Repository files navigation

Golang Microservices

The main idea of creating this project is implementing an infrastructure for up and running distributed system with the latest technology and architecture like Vertical Slice Architecture, OpenTelemetry, RabbitMq in Golang, and we will not deal mainly with business.

Plan

🌀This project is a work in progress, new features will be added over time.🌀

Structure of Project

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.

How to Run

Docker-Compose

Run our infrastructure with docker using the infrastructure.yaml file with the below command at the root of app:

docker-compose -f ./deployments/docker-compose/infrastructure.yaml up -d
Todo

I will add docker-compsoe for up and running whole app here in the next...

Build

For building each microservice, Run this command in root of each microservice where go.mod located:

go build -v ./...

Run

For runing each microservice, Run this command in root of each microservice where go.mod located:

go run -v ./...

Test

For testing each microservice, Run this command in root of each microservice where go.mod located:

go test -v ./...

Documentation Apis

Each microservice uses swagger open api, navigate to /swagger for getting the list endpoints.

Also, to test apis, I created the shop.rest file. This file run with REST Client VSCode plugin.

Support

If you like my work, feel free to:

  • ⭐ this repository. And we will be happy together :)

Thanks a bunch for supporting me!

Contribution

Please follow this contribution guideline to submit a pull request or create the issue.

Project References & Credits

License

This project is made available under the MIT license. See LICENSE for details.

About

Golang, Microservice, Docker

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published