Demo (Handle Kafka & Mongo)
this project uses SpringBoot 3 that helps creating production-grade and stand-alone java applications & services with absolute minimum fuss.
This project implements a Payment Service using hexagonal architecture. The service consumes messages from a Kafka topic, validates and stores them in a MongoDB database.
The Payment Service follows the hexagonal (or ports/adapter) architecture:
- Core Domain: Contains the business logic and domain models.
- Ports: Define interfaces for incoming and outgoing interactions.
- Adapters: Implement the interfaces defined by the ports.
src/
|-- common/
| |-- event/
└──PaymentEvent.java
| |-- exception/
|
|-- config/
| |-- kafka/
| |-- mongodb/
|
|-- core/
|-- port/infra/
└──PaymentDataGateway
|-- domain/
| |-- model/
└──Payment.java
| |-- service/
└──PaymentService.java # interface
└──PaymentServiceImpl.java
|-- adapter/
|-- messaging/
| |-- model/
| | └──KPayment.java
| └──KPaymentConsumer.java
|-- mongodb/
| |-- model/
| └──MGPayment
| |-- respository
| └──PaymentAdapter.java
|
|-- Application.java
|
test/
|
docker-compose.yml
start-local-env.sh
pom.xml
README.md
. Java environment
> java -version
java 22
. Have Maven Installed
> mvn -version
Apache Maven 3.9.8
mvn spring-boot:run
Make sure that you have installed the following tools on your local machine
- Docker,
- docker-compose. (you should have version 1.29 or higher)
Installing Docker Desktop (for Mac & Windows) is the easiest way to get your environment ready
A script that automates the process was provided ;)
./start-local-env.shOnce the script completes successfully:
MongoDB cluster will be available at mongodb://localhost:27017,localhost:27018,localhost:27019/test?replicaSet=rs0
Kafka broker will be accessible at localhost:9092
In order for your machine to recognize the hosts mongo1, mongo2, and mongo3, you must add these hostnames to your local hosts file as follows:
127.0.0.1 mongo1
127.0.0.1 mongo2
127.0.0.1 mongo3
The location of the hosts file varies by operating system:
- For macOS: /private/etc/hosts
- For Windows: C:/Windows/System32/drivers/etc/hosts
Note : We need to create a Mongo Replica Set in order to be able to test some promising features like @Transactional annotation, etc
Compass is an interactive tool for querying, optimizing, and analyzing the MongoDB data
The Connection String URI Format For our Replica Set is as the following
mongodb://localhost:27017,localhost:27018,localhost:27019/?replicaSet=rs0
Offset Explorer is one of the best kafka UI tool that allow to quickly view our kafka cluster, including the borkers, topics and consumers
- Download & Install Offset Explorer
- Launch the app & Add a New Connection
- In the Advanced tab :
Boostrap servers : localhost:9092
you can use Conduktor which is a beautiful and fully-featured desktop client for Apache Kafka
🚀 This project includes simple unit and integration tests to ensure the reliability and correctness of the payment service as it evolves
Example: PaymentServiceTest.java
tests individual components in isolation.
--
Example: PaymentTestIT.java
It tests the entire payment processing flow.
The integration test utilizes spring-kafka-test with an embedded Kafka broker and embedded MongoDB instance to simulate the production environment.
Feel free to open an issue or submit a pull request if you have any suggestions or improvements!
