meysamhadeli / booking-microservices

Practical microservices, built with .Net 8, DDD, CQRS, Event Sourcing, Vertical Slice Architecture, Event-Driven Architecture, and the latest technologies.
MIT License
907 stars 182 forks source link
aspnetcore clean-architecture cqrs ddd dotnet dotnet-core dotnetcore event-driven-architecture event-sourcing grpc kubernetes masstransit messaging microservice microservices mongodb oauth2 opentelemetry redis vertical-slice-architecture
booking-microservices
ci-status build-status

πŸš€ 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 Sourcing, CQRS, DDD, gRpc, MongoDB, RabbitMq, Masstransit in .Net 8.

πŸ’‘ 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, Event Sourcing, CQRS, DDD and gRpc.

Open in Gitpod
Open in GitHub Codespaces

Table of Contents

The Goals of This Project

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.

High-level plan is represented in the table

Feature Status
API Gateway Completed βœ”οΈ
Identity Service Completed βœ”οΈ
Flight Service Completed βœ”οΈ
Passenger Service Completed βœ”οΈ
Booking Service Completed βœ”οΈ
Building Blocks Completed βœ”οΈ

Technologies - Libraries

The Domain And Bounded Context - Service Boundary

Structure of Project

In this project I used a mix of clean architecture, vertical slice architecture and I used feature folder structure to structure my files.

I used yarp reverse proxy to route synchronous and asynchronous requests to the corresponding microservice. Each microservice has its dependencies such as databases, files etc. Each microservice is decoupled from other microservices and developed and deployed separately. Microservices talk to each other with Rest or gRPC for synchronous calls and use RabbitMq or Kafka for asynchronous calls.

We have a separate microservice (IdentityServer) for authentication and authorization of each request. Once signed-in users are issued a JWT token. This token is used by other microservices to validate the user, read claims and allow access to authorized/role specific endpoints.

I used RabbitMQ as my MessageBroker for async communication between microservices using the eventual consistency mechanism. Each microservice uses MassTransit to interface with RabbitMQ providing, messaging, availability, reliability, etc.

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.

After saving data in write side, I save a Internal Command record in my Persist Messages storage (like something we do in outbox pattern) and after committing transaction in write side, trigger our command handler in read side and this handler could save their read models in our MongoDB database.

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 controller, as found in traditional ASP.net controllers, I used the REPR pattern. Each action gets its own small endpoint, consisting of a route, the action, and an IMediator instance (see MediatR). The request is passed to the IMediator instance, routed through a Mediatr pipeline where custom middleware can log, validate and intercept requests. The request is then handled by a request specific IRequestHandler which performs business logic before returning the result.

The use of the mediator pattern in my controllers creates clean and thin controllers. 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:

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 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.

Development Setup

Dotnet Tools Packages

For installing our requirement packages with .NET cli tools, we need to install dotnet tool manifest.

dotnet new tool-manifest

And after that we can restore our dotnet tools packages with .NET cli tools from .config folder and dotnet-tools.json file.

dotnet tool restore

Husky

Here we use husky to handel some pre commit rules and we used conventional commits rules and formatting as pre commit rules, here in package.json. of course, we can add more rules for pre commit in future. (find more about husky in the documentation) We need to install husky package for manage pre commits hooks and also I add two packages @commitlint/cli and @commitlint/config-conventional for handling conventional commits rules in package.json. Run the command bellow in the root of project to install all npm dependencies related to husky:

npm install

Note: In the root of project we have .husky folder and it has commit-msg file for handling conventional commits rules with provide user friendly message and pre-commit file that we can run our scripts as a pre-commit hooks. that here we call format script from package.json for formatting purpose.

Upgrade Nuget Packages

For upgrading our nuget packages to last version, we use the great package dotnet-outdated. Run the command below in the root of project to upgrade all of packages to last version:

dotnet outdated -u

How to Run

Config Certificate

Run the following commands to Config SSL in your system:

Windows using Linux containers

dotnet dev-certs https -ep %USERPROFILE%\.aspnet\https\aspnetapp.pfx -p password
dotnet dev-certs https --trust

Note: for running this command in powershell use $env:USERPROFILE instead of %USERPROFILE%

macOS or Linux

dotnet dev-certs https -ep ${HOME}/.aspnet/https/aspnetapp.pfx -p $CREDENTIAL_PLACEHOLDER$
dotnet dev-certs https --trust

Docker Compose

To run this app in Docker, use the docker-compose.yaml and execute the below command at the root of the application:

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

Kubernetes

To configure TLS in the Kubernetes cluster, we need to install cert-manager based on the docs and run the following commands to apply TLS in our application. Here, we use Let's Encrypt to encrypt our certificate.

kubectl apply -f ./deployments/kubernetes/booking-cert-manager.yml

To apply all necessary deployments, pods, services, ingress, and config maps, please run the following command:

kubectl apply -f ./deployments/kubernetes/booking-microservices.yml

Build

To build all microservices, run this command in the root of the project:

dotnet build

Run

To run each microservice, run this command in the root of the Api folder of each microservice where the csproj file is located:

dotnet run

Test

To test all microservices, run this command in the root of the project:

dotnet test

Documentation Apis

Each microservice has a Swagger OpenAPI. Browse to /swagger for a list of endpoints.

As part of API testing, I created the booking.rest file which can be run with the REST Client VSCode plugin.

Support

If you like my work, feel free to:

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.