Skip to content

A microservices, built with .Net, DDD, CQRS, Event Sourcing, Vertical Slice Architecture, Event-Driven Architecture, and the latest technologies.

License

Notifications You must be signed in to change notification settings

amirhossein18121380/booking-microservices

ย 
ย 

Repository files navigation

booking-microservices
ci-status build-status

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, Event Sourcing, CQRS, DDD, gRpc, MongoDB, RabbitMq, Masstransit in .Net, 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 Domain Driven Design (DDD) to implement all business processes in microservices.
  • โ‡๏ธ Using Rabbitmq on top of Masstransit for Event Driven Architecture between our microservices.
  • โ‡๏ธ Using gRPC for internal communication between our microservices.
  • โ‡๏ธ Using CQRS implementation with MediatR library.
  • โ‡๏ธ Using Postgres for write side of some microservices.
  • โ‡๏ธ Using MongoDB for read side of some microservices.
  • โ‡๏ธ Using Event Store for write side of Booking-Microservice to store all historical state of aggregate.
  • โ‡๏ธ 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.
  • โ‡๏ธ Using Unit Testing, Integration Testing, End To End Testing for testing level.
  • โ‡๏ธ Using Fluent Validation and a Validation Pipeline Behaviour on top of MediatR.
  • โ‡๏ธ Using Minimal API for all endpoints.
  • โ‡๏ธ Using Health Check for reporting the health of app infrastructure components.
  • โ‡๏ธ Using Docker-Compose and Kubernetes for our deployment mechanism.
  • โ‡๏ธ Using Kibana on top of Serilog for logging.
  • โ‡๏ธ Using Jaeger on top of OpenTelemetry for tracing.
  • โ‡๏ธ Using Prometteuse and Grafana on top of OpenTelemetry for monitoring.
  • โ‡๏ธ Using IdentityServer for authentication and authorization base on OpenID-Connect and OAuth2.
  • โ‡๏ธ Using Yarp as a microservices gateway.
  • โ‡๏ธ Using Kubernetes to achieve efficient scaling and ensure high availability for each of our microservices.
  • โ‡๏ธ Using Nginx Ingress Controller for load balancing between our microservices top of Kubernetes.
  • โ‡๏ธ Using cert-manager to Configure TLS in kubernetes cluster.

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

  • โœ”๏ธ .NET 7 - .NET Framework and .NET Core, including ASP.NET and ASP.NET Core
  • โœ”๏ธ MVC Versioning API - Set of libraries which add service API versioning to ASP.NET Web API, OData with ASP.NET Web API, and ASP.NET Core
  • โœ”๏ธ EF Core - Modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations
  • โœ”๏ธ Masstransit - Distributed Application Framework for .NET.
  • โœ”๏ธ MediatR - Simple, unambitious mediator implementation in .NET.
  • โœ”๏ธ FluentValidation - Popular .NET validation library for building strongly-typed validation rules
  • โœ”๏ธ Swagger & Swagger UI - Swagger tools for documenting API's built on ASP.NET Core
  • โœ”๏ธ Serilog - Simple .NET logging with fully-structured events
  • โœ”๏ธ Polly - Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner
  • โœ”๏ธ Scrutor - Assembly scanning and decoration extensions for Microsoft.Extensions.DependencyInjection
  • โœ”๏ธ Opentelemetry-dotnet - The OpenTelemetry .NET Client
  • โœ”๏ธ DuendeSoftware IdentityServer - The most flexible and standards-compliant OpenID Connect and OAuth 2.x framework for ASP.NET Core
  • โœ”๏ธ EasyCaching - Open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easier.
  • โœ”๏ธ Mapster - Convention-based object-object mapper in .NET.
  • โœ”๏ธ Hellang.Middleware.ProblemDetails - A middleware for handling exception in .Net Core
  • โœ”๏ธ NewId - NewId can be used as an embedded unique ID generator that produces 128 bit (16 bytes) sequential IDs
  • โœ”๏ธ Yarp - Reverse proxy toolkit for building fast proxy servers in .NET
  • โœ”๏ธ Tye - Developer tool that makes developing, testing, and deploying microservices and distributed applications easier
  • โœ”๏ธ gRPC-dotnet - gRPC functionality for .NET.
  • โœ”๏ธ EventStore - The open-source, functional database with Complex Event Processing.
  • โœ”๏ธ MongoDB.Driver - .NET Driver for MongoDB.
  • โœ”๏ธ xUnit.net - A free, open source, community-focused unit testing tool for the .NET Framework.
  • โœ”๏ธ Respawn - Respawn is a small utility to help in resetting test databases to a clean state.
  • โœ”๏ธ Testcontainers - Testcontainers for .NET is a library to support tests with throwaway instances of Docker containers.
  • โœ”๏ธ K6 - Modern load testing for developers and testers in the DevOps era.

The Domain And Bounded Context - Service Boundary

  • Identity Service: The Identity Service is a bounded context for the authentication and authorization of users using Identity Server. This service is responsible for creating new users and their corresponding roles and permissions using .Net Core Identity and Jwt authentication and authorization.

  • Flight Service: The Flight Service is a bounded context CRUD service to handle flight related operations.

  • Passenger Service: The Passenger Service is a bounded context for managing passenger information, tracking activities and subscribing to get notification for out of stock products.

  • Booking Service: The Booking Service is a bounded context for managing all operation related to booking ticket.

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:

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

How to Run

Docker

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

Run this app in docker using the docker-compose.yaml file with the below command at the root of the application:

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

Kubernetes

For Configure TLS in kubernetes cluster we need install cert-manager base on docs and run the following commands for apply TLS in our application. Here we use LetsEncrypt for encryption our certificate.

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

Run the following command to apply all deployments, pods, services, ingress and configmaps that we need

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

Documentation Apis

Each microservice uses swagger open api, navigate to /swagger for a list of every endpoint. For testing apis I used the REST Client plugin for VS Code running this file booking.rest.

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.

About

A microservices, built with .Net, DDD, CQRS, Event Sourcing, Vertical Slice Architecture, Event-Driven Architecture, and the latest technologies.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 95.6%
  • Dockerfile 2.5%
  • JavaScript 1.8%
  • Shell 0.1%