Skip to content

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 Driven Architecture, CQRS, DDD, gRpc, MongoDB, RabbitMq in Java Spring Boot.

License

Notifications You must be signed in to change notification settings

meysamhadeli/booking-microservices-java-spring-boot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

90 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

booking-microservices-java-spring-boot
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 Driven Architecture, CQRS, DDD, gRpc, MongoDB, RabbitMq in Java Spring Boot.

๐Ÿ’ก 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, CQRS, DDD and gRpc.

You can find .Net ported of this project in this link: ๐Ÿ”— booking-microservices

Open in Gitpod
Open in GitHub Codespaces

Table of Contents

The Goals of This Project

  • โ‡๏ธ Using Vertical Slice Architecture for architecture level.
  • โ‡๏ธ Using Spring MVC as a Web Framework.
  • โ‡๏ธ Using Domain Driven Design (DDD) to implement all business processes in microservices.
  • โ‡๏ธ Using Spring AMQP on top of Rabbitmq for Event Driven Architecture between our microservices.
  • โ‡๏ธ Using gRPC for internal communication between our microservices.
  • โ‡๏ธ Using CQRS implementation with Mediator library.
  • โ‡๏ธ Using Spring Data JPA for data persistence and ORM in write side with Postgres.
  • โ‡๏ธ Using Spring Data MongoDB for data persistence and ORM in read side with MongoDB.
  • โ‡๏ธ 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 Mockito.
  • โ‡๏ธ Using End-To-End Testing and Integration Testing for testing features with all dependencies using testcontainers.
  • โ‡๏ธ Using Spring Validator and a Validation Pipeline Behaviour on top of Mediator.
  • โ‡๏ธ Using Springdoc Openapi for generating OpenAPI documentation in Spring Boot.
  • โ‡๏ธ Using OpenTelemetry Collector for collecting Metrics, Tracings and Structured Logs.
  • โ‡๏ธ Using Kibana for Logging top of OpenTelemetry Collector.
  • โ‡๏ธ Using Jaeger for Distributed Tracing top of OpenTelemetry Collector.
  • โ‡๏ธ Using Prometheus and Grafana for monitoring top of OpenTelemetry Collector.
  • โ‡๏ธ Using Keycloak for authentication and authorization base on OpenID-Connect and OAuth2.
  • โ‡๏ธ Using Spring Cloud Gateway as a microservices gateway.

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 โœ”๏ธ
Keycloak Service Completed โœ”๏ธ
Flight Service Completed โœ”๏ธ
Passenger Service Completed โœ”๏ธ
Booking Service Completed โœ”๏ธ
Building Blocks Completed โœ”๏ธ

Technologies - Libraries

  • โœ”๏ธ Spring Boot - Framework for building Java applications with pre-configured defaults and embedded server support.
  • โœ”๏ธ Spring AMQP - Simplifies messaging using RabbitMQ with declarative configuration and templates.
  • โœ”๏ธ Spring Data JPA - Enhances JPA with repository abstractions and advanced query capabilities.
  • โœ”๏ธ Spring Data MongoDB - Provides seamless MongoDB integration with Spring-based applications.
  • โœ”๏ธ Spring Security - Comprehensive security framework for authentication and authorization in Java applications.
  • โœ”๏ธ Keycloak - A identity and access management solution supporting OpenID Connect and OAuth 2.0.
  • โœ”๏ธ PostgreSQL - Official JDBC driver for PostgreSQL, enabling Java applications to interact with PostgreSQL databases.
  • โœ”๏ธ Springdoc OpenAPI - Automatically generates OpenAPI 3 documentation for Spring Boot projects.
  • โœ”๏ธ Swagger Core - Core library for building and consuming Swagger-compliant APIs.
  • โœ”๏ธ OpenTelemetry Collector - Collects, processes, and exports telemetry data (traces, metrics, logs) for observability.
  • โœ”๏ธ Lombok - Reduces boilerplate code in Java by generating common methods like getters and setters.
  • โœ”๏ธ Flyway - Database migration tool for version-controlled and repeatable schema changes.
  • โœ”๏ธ JPA Buddy - Productivity tool for working with JPA and Hibernate, simplifying development and debugging.
  • โœ”๏ธ UUID Creator - Library for generating UUIDs in various formats and versions.
  • โœ”๏ธ QueryDSL - Enables type-safe queries for JPA, SQL, and other persistence layers.
  • โœ”๏ธ Reflections - Facilitates metadata scanning and classpath resource analysis in Java.
  • โœ”๏ธ gRPC Spring - Integration of gRPC with Spring Boot for building high-performance RPC services.
  • โœ”๏ธ Testcontainers - Provides lightweight, disposable Docker containers for testing purposes.
  • โœ”๏ธ Mockito - Popular mocking framework for writing clean, maintainable unit tests in Java.
  • โœ”๏ธ JUnit - Essential testing framework for Java developers, supporting unit and integration testing.

The Domain And Bounded Context - Service Boundary

  • Keycloak Service: The Keycloak Service is a identity provider for the authentication and authorization of users using Keycloak. This service is responsible for creating new users and their corresponding roles permissions and handeling authentication and authorization with OpenID-Connect and OAuth2.

  • 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 Keycloak Service 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 controllers, I used the REPR pattern. Each action gets its own small endpoint, consisting of a route, the action, and an IMediator instance. The request is passed to the IMediator instance, routed through a Mediator 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 mediator 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 Compose

Use the command below to run our infrastructure with docker using the docker-compose.infrastructure.yaml file at the root of the app:

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

Build

To build all microservices, run this command in the root of each microservice where the pom.xml file is located:

mvn clean install

Run

To run each microservice, run this command in the root of each microservice where the pom.xml file is located:

mvn spring-boot:run

Test

To test all microservices, run this command in the root of each microservice where the pom.xml file is located:

dotnet test

Documentation Apis

Each microservice provides API documentation and navigate to /swagger-ui/index.html to visit 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.

About

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 Driven Architecture, CQRS, DDD, gRpc, MongoDB, RabbitMq in Java Spring Boot.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages