Coder Social home page Coder Social logo

meysamhadeli / booking-microservices Goto Github PK

View Code? Open in Web Editor NEW
803.0 28.0 152.0 17.6 MB

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

License: MIT License

Dockerfile 2.52% C# 95.53% Shell 0.06% JavaScript 1.89%
cqrs dotnet dotnetcore grpc masstransit microservices vertical-slice-architecture redis kubernetes clean-architecture

booking-microservices's Introduction

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 8, and we will not deal mainly with business. πŸš€

Open in Gitpod
Open in GitHub Codespaces

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 for testing small units and mocking our dependencies with Nsubstitute.
  • ❇️ Using End-To-End Testing and Integration Testing for testing features with all dependencies using testcontainers.
  • ❇️ 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 OpenTelemetry for distributed tracing on top of Jaeger.
  • ❇️ Using OpenTelemetry for monitoring on top of Prometheus and Grafana.
  • ❇️ 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

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:

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

booking-microservices's People

Contributors

amirhossein18121380 avatar bunkrur avatar dependabot[bot] avatar erjanmx avatar github-actions[bot] avatar meysamhadeli avatar timothymakkison 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

booking-microservices's Issues

System.Security.Cryptography.CryptographicException

When i run the docker compose i get the error below

09:41:18 [INFO] Background worker stopped
2023-10-04 09:41:18 09:41:18 [INFO] Subscription to all '"default"'
2023-10-04 09:41:19 09:41:18 [EROR] Hosting failed to start
2023-10-04 09:41:19 System.Security.Cryptography.CryptographicException: The certificate data cannot
2023-10-04 09:41:19 be read with the provided password, the password may be incorrect.
2023-10-04 09:41:19 System.Security.Cryptography.CryptographicException: The certificate data
2023-10-04 09:41:19 cannot be read with the provided password, the password may be incorrect.
2023-10-04 09:41:19 at void System.Security.Cryptography.X509Certificates.UnixPkcs12Reader.
2023-10-04 09:41:19 VerifyAndDecrypt(ReadOnlySpan password, ReadOnlyMemory
2023-10-04 09:41:19 authSafeContents)
2023-10-04 09:41:19 at void System.Security.Cryptography.X509Certificates.UnixPkcs12Reader.
2023-10-04 09:41:19 Decrypt(SafePasswordHandle password, bool ephemeralSpecified)
2023-10-04 09:41:19 at void System.Security.Cryptography.X509Certificates.UnixPkcs12Reader.Decrypt
2023-10-04 09:41:19 (SafePasswordHandle password, bool ephemeralSpecified)
2023-10-04 09:17:21 Unhandled exception. System.Security.Cryptography.CryptographicException: The certificate data cannot be read with the provided password, the password may be incorrect.
2023-10-04 09:17:21 ---> System.Security.Cryptography.CryptographicException: The certificate data cannot be read with the provided password, the password may be incorrect.
2023-10-04 09:17:21 at System.Security.Cryptography.X509Certificates.UnixPkcs12Reader.VerifyAndDecrypt(ReadOnlySpan1 password, ReadOnlyMemory1 authSafeContents)

Use NewId or Ulid instead of Snowflake ID (IdGen)

Base on suggestion in PR #200

Add this feature in PR: #242

Ref:

IdGen

https://github.com/RobThree/IdGen

Ulid

https://github.com/Cysharp/Ulid

NewId

https://github.com/phatboyg/NewId

R&D:

IdGen is 64-bit (8 bytes) and Ulid, Ulid are 128-bit (16 bytes) Id's. Also, IdGen is long and Ulid, Ulid are uniqueidentifier, so we have better performance in the end with IdGen.

For getting a unique Id in all of distributed system is better we use NewId or Ulid. Because we always get a valid unique sequential Id in whole distributed system.

Note: We can handle it with IdGen for get unique Id in whole distributed system. But it's a little tricky and must find a good way to implement it to handle it to scale up in Kubernetes.

unauthorized exception

Hi I am trying to run this project on local machine using docker compose. The command fails with following message:

unauthorized: authentication required

this happens somewhere at pulling eventstore. I closed the terminal already, forgetting to copy the full log geenrated on running the command: docker-compose -f ./deployments/docker-compose/docker-compose.yaml up -d

I found this while digging out: EventStore/EventStore#2706

Any insights please?

Thanks

Save Problem

Brother,
I am facing a problem when saving data. Though I have commented savechanges method, data saves in database.
In my scenario on this table there is no domain events.
var newAircraft = await _flightDbContext.Aircraft.AddAsync(aircraftEntity, cancellationToken);
//await _flightDbContext.SaveChangesAsync(cancellationToken);

Helpful Features

It's Awesome project.
Thanks for making this repository.

I think if you add more feature that would be helpful, like

  1. Login , Authorization & Security.
  2. Complex query 2 or All microservices.
  3. Pagination.
  4. Audit Log
  5. SignalR

@ if you have any plan to add UI then Add React

Thanks.
May Allah live long

Suggestions in .net7

Here are some suggestions from .net 7. Please take this as they are, suggestions, I'm not saying it's better or worse just some alternatives. Congratulations for this awesome sample!

  • Use TypedResults and Multiple Results.
  • In theory Hellang.Middleware.ProblemDetails is not needed anymore since the ProblemDetails are now easy configurable in .net 7
  • Is SnowFlakIdGenerator since MassTransit already uses NewId with same purposes?
  • Use required on props of records to simplify/omit some rules.
  • Question: Should booking service have any reference to a CurrentUser? Will this be called always in a context of a user?

Lately we've been merging the the request, validator, handler in the same class. Since adding or removing the request will almost always involve changing the validator. F12 (Go to definition is magical). We do not use any auto discovery plain old container registration so navigation and find usages work.

public static class Endpoint
{
    public static void MapRoute(IEndpointRouteBuilder route) => route.MapGet("/account/{id:int}", Handle);

    public record Request
    {
        public required int Id { get; init; }
    }

    public class Validator : AbstractValidator<Request>
    {
        public Validator() => RuleFor(x => x.Id).GreaterThan(2);
    }

    public record Response(string Desc);

    public static async Task<Results<Ok<Response>, NotFound>> Handle(
        [AsParameters] Request request,
        ILogger<Request> logger,
        ApiDbContext context,
        CancellationToken cancellationToken)
    {
        if (request.Id == 10)
        {
            return NotFound();
        }

        var entity = await context.Students.FirstOrDefaultAsync(cancellationToken: cancellationToken);

        return Ok(new Response($"everything went fine for: {request.Id}"));
    }
}

In case you want to do unit tests instead of acceptance tests of your endpoints you can inject Queries and Commands instead of db context so you can easily mock.
We've tried TestContainers with server instance per session and each tests gets a new database + migration. It takes 10 sec to start sql server (azure sql edge) and 4ms to create a random new db with 2 tables. Running an acceptance test which: 1. generates new db. 2. creates the tables. 3. starts the api with test server. 4. makes a http get 5. make a query in db. 5. returns response. Takes 1 sec on average. This way ALL tests are insolated and run in parallel (even in the same (implicit) collection).
I'm planning a PoC for TestContainers with RabbitMq where only one instance of rabbitmq is started for a test session (per assembly fixutre) and all tests will use a separate vhost.

We've also dropped MediatR since we have very few truly cross concerns for it's pipeline and the number of request.

  • OpenTelemetry adds logging to http and mass transit so tracebility is convered by that
  • Caching at the api level. Since queries/command are never re-used between features they are tailed for each feature need. We add caching explicitly in the handler.
  • Transactions when need are controlled at the handler level with transient dbcontext. A handler might create multiple transactions or call saves changes multiple times. In case of concurrency issues there are some chances to recover.
  • Validation happens at the api/endpoint level at least the shallow one anything more complicated is handled directly in the handler

The principles we've been guiding ourselves:

  • adhere to SOLID principles
  • everything is an http api. Even cron jobs, background daemons. Use health-checks and probes
  • vertical slices architecture
  • invest a lot in identifying the correct bounded context
  • duplication is far cheaper than wrong/premature abstractization
  • we do not write applications to have tests, we write tests to build reliable applications. Refactoring should be done no matter how many tests need to be changed. Corollary prefer bdd, acceptance, integration and e2e tests which test business requirements instead of unit tests, especially when the MS are very small. Do sprinkle unit tests when business logic is complex.
  • observability and traceability through OpenTelemetry are primordial
  • easy to onboard new developers by self browsing since technical documentation is almost never written/updated.
    • drive developers into the pit of success with composable extensions framework.
    • limit magic behaviors: auto mappers, auto registration reflection. Prefer source generators instead.
  • YAGNI
    • We will not swap databases without effort
    • We will not change transport protocols from grpc/http to anything else without effort
    • We will not switch from an http api to a console app or daemon service without effort.

stored procs

Hi Guys
how would you execute a stored proc, can you please give a guide

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.