๐ 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
- The Goals of This Project
- Plan
- Technologies - Libraries
- The Domain and Bounded Context - Service Boundary
- Structure of Project
- How to Run
- Documentation Apis
- Support
- Contribution
- โ๏ธ Using
Vertical Slice Architecture
forarchitecture
level. - โ๏ธ Using
Spring MVC
as aWeb Framework
. - โ๏ธ Using
Domain Driven Design (DDD)
to implement allbusiness processes
in microservices. - โ๏ธ Using
Spring AMQP
on top ofRabbitmq
forEvent Driven Architecture
between our microservices. - โ๏ธ Using
gRPC
forinternal communication
between our microservices. - โ๏ธ Using
CQRS
implementation withMediator
library. - โ๏ธ Using
Spring Data JPA
fordata persistence
andORM
inwrite side
withPostgres
. - โ๏ธ Using
Spring Data MongoDB
fordata persistence
andORM
inread side
withMongoDB
. - โ๏ธ Using
Inbox Pattern
for ensuring message idempotency for receiver andExactly once Delivery
. - โ๏ธ Using
Outbox Pattern
for ensuring no message is lost and there is atAt Least One Delivery
. - โ๏ธ Using
Unit Testing
for testing small units and mocking our dependencies withMockito
. - โ๏ธ Using
End-To-End Testing
andIntegration Testing
for testingfeatures
with all dependencies usingtestcontainers
. - โ๏ธ Using
Spring Validator
and aValidation Pipeline Behaviour
on top ofMediator
. - โ๏ธ Using
Springdoc Openapi
for generatingOpenAPI documentation
in Spring Boot. - โ๏ธ Using
OpenTelemetry Collector
for collectingMetrics
,Tracings
andStructured Logs
. - โ๏ธ Using
Kibana
forLogging
top ofOpenTelemetry Collector
. - โ๏ธ Using
Jaeger
forDistributed Tracing
top ofOpenTelemetry Collector
. - โ๏ธ Using
Prometheus
andGrafana
formonitoring
top ofOpenTelemetry Collector
. - โ๏ธ Using
Keycloak
forauthentication
andauthorization
base onOpenID-Connect
andOAuth2
. - โ๏ธ Using
Spring Cloud Gateway
as a microservicesgateway
.
๐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 โ๏ธ |
- โ๏ธ 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.
-
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 contextCRUD
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.
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.
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
To build
all microservices, run this command in the root
of each microservice where the pom.xml
file is located:
mvn clean install
To run
each microservice, run this command in the root
of each microservice where the pom.xml
file is located:
mvn spring-boot:run
To test
all microservices, run this command in the root
of each microservice where the pom.xml
file is located:
dotnet test
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
.
If you like my work, feel free to:
- โญ this repository. And we will be happy together :)
Thanks a bunch for supporting me!
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.
- https://github.com/jbogard/ContosoUniversityDotNetCore-Pages
- https://github.com/kgrzybek/modular-monolith-with-ddd
- https://github.com/oskardudycz/cqrs-is-simpler-with-java
This project is made available under the MIT license. See LICENSE for details.