Giter VIP home page Giter VIP logo

meysamhadeli / shop-golang-microservices Goto Github PK

View Code? Open in Web Editor NEW
208.0 6.0 27.0 2.02 MB

Practical microservices based on different software architecture and technologies like Golang, CQRS, Vertical Slice Architecture, Docker, RabbitMQ, OpenTelemetry, Postgresql, ...

License: MIT License

Makefile 1.19% Go 98.81%
clean-architecture cqrs go golang microservices rabbitmq vertical-slice-architecture postgresql microservice ddd

shop-golang-microservices's Introduction

CI Go Version

πŸ“ Shop 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. πŸš€

Open in Gitpod

Table of Contents

The Goals of This Project

  • ❇️ Using Vertical Slice Architecture for architecture level.
  • ❇️ Using Rabbitmq for Event Driven Architecture between our microservices with streadway/amqp library.
  • ❇️ Using gRPC for internal communication between our microservices with grpc/grpc-go library.
  • ❇️ Using CQRS implementation with mehdihadeli/Go-MediatR library.
  • ❇️ Using Postgres for database in our microservices with go-gorm/gorm library.
  • ❇️ Using go-playground/validator for validating input data in the REST calls.
  • ❇️ Using OpenTelemetry for distributed tracing top of Jaeger.
  • ❇️ Using OAuth2 for implementation authentication and authorization with go-oauth2/oauth2 library.
  • ❇️ Using Echo framework for RESTFul api.
  • ❇️ Using Swagger with swaggo/swag library for api documentation.
  • ❇️ Using uber-go/fx library for dependency injection.
  • ❇️ Using Viper for configuration management.
  • ❇️ Using logrus as a structured logger.
  • ❇️ Using Unit Testing,Integration Testing and End To End Testing for testing level.
  • ❇️ Using Docker-Compose for our deployment mechanism.
  • 🚧 Using OpenTelemetry for monitoring top of Prometteuse and Grafana
  • 🚧 Using MongoDB for read side with mongo-driver.
  • 🚧 Using Domain Driven Design (DDD) to implement all business processes in microservices.
  • 🚧 Using Inbox Pattern for ensuring message idempotency for receiver and Exactly once Delivery.
  • 🚧 Using Outbox Pattern for ensuring no message is lost and there is at At Least One Delivery.

Plan

πŸŒ€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.

Technologies - Libraries

  • βœ”οΈ 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

The Domain And Bounded Context - Service Boundary

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

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
Todo

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

Build

To build each microservice, run this command in the root directory of each microservice where the go.mod file is located:

go build -v ./...

Run

To run each microservice, run this command in the root of the microservice where go.mod is located:

go run -v ./...

Test

To test each microservice, run this command in the root directory of the microservice where the go.mod file is located:

go test -v ./...

Documentation Apis

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.

Support

If you like my work, feel free to:

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

Thanks a bunch for supporting me!

Contribution

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.

Project References & Credits

License

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

shop-golang-microservices's People

Contributors

github-actions[bot] avatar meysamhadeli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

shop-golang-microservices's Issues

How to use this project?

The project looks good. However, I can't find any instructions on how to use the API.
How do I build and deploy?

Authorization service needs to be added?

I am new in microservices and starting with your project to understand it. Thank you very much. But when starting to deploy your project on my device. I started getting issues, for example, services wants authorization, but I do not have authorization am I right?

Is the user creation API ready?

When trying to call the API create user.
However, i got the 401 error.
Is the API currently available?
I am very interested in this project. Hope you can help.

http://localhost:5002/api/v1/users

Problem with consuming the ProductCreated event in inventory_service

Hello,

I'm using your project as a study due to the organization and of all the ones I analyzed, yours is the best.

However, there is this problem that when trying to create a product, inventory receives the event and processes it, but when trying to create another product afterwards it does nothing.

I already tried to fix it but I couldn't.

Could you help me, please?

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.