System that handles a large number of email requests
Link: https://github.com/sunil-karki/Email-System/blob/main/Design_diagram.png
This guide will walk you through the process of starting three services in Python: a producer service, a consumer service, and Kafka servers. These services will communicate with each other using Apache Kafka as the messaging system.
Before you start, ensure that you have the following installed on your system, except MongoDB:
- Python (version 3.9.7 or higher)
- Kafka (version __ or higher)
- Flask (version __ or higher)
- MongoDB
Follow these steps to start the three services:
-
Start Kafka
Start your Kafka server by running the appropriate command based on your installation. This typically involves starting the ZooKeeper server and then starting the Kafka broker. You can refer to the Kafka documentation for detailed setup.
After the setup, start the ZooKeeper for Kafka.\bin\windows\zookeeper-server-start.bat .\config\zookeeper.properties
Start the Kafka broker
.\bin\windows\kafka-server-start.bat .\config\server.properties
-
Set Up MongoDB Database
In your preferred terminal or command prompt:
docker pull mongo
This will pull image file of the MongoDB database
Then, run the container (27017 is port for MongoDB)docker run --name mongodb -d -p 27017:27017 mongo
-
Start the Consumer Service
Run the following command to start the EmailService (consumer 1): (You can skip this if to run in Docker)
python kafkaServerService.py
Run the following command to start the next consumer service (consumer 2): (You can skip this if to run in Docker)
python kafkaDBService.py
To run it on Docker, execute the following four commands below:
Note: In files 'serverServiceDockerfile' and 'dbServiceDockerfile', add IP address/port where kafka server runs and also IP address/port for MongoDB. IP address could be found with ipconfig or ifconfig.
For EmailService (consumer 1):docker build -t email-service -f serverServiceDockerfile .
docker run --name email-service-container2 -d email-service
Then, for DBService (consumer 2):
docker build -t db-service -f dbServiceDockerfile .
docker run --name db-service-container3 -d db-service
-
Start the Producer Service
Run the following command to start the ApiGateway service (producer): (You can skip this if to run in Docker)
python apiGateway.py
To run it on Docker, execute the following two commands:
Note: In apiGatewayDockerfile, add IP address/port where kafka server runs.docker build -t api-gateway -f apiGatewayDockerfile .
Then,
docker run --name api-gateway-container -d -p 8000:8000 api-gateway
-
Interact with the Services
Run the following command to send the email:
curl --header "Content-Type: application/json" --request POST --data "{\"emailFrom\": \"tese320@gmail.com\", \"emailTo\": \"test1@gmail.com\", \"message\": \"Message from gateway\", \"subject\": \"apigateway.\", \"templateFlag\": \"1\"}" http://localhost:8000/sendMail
Run the following command to query the email records/documents:
curl --header "Content-Type: application/json" --request POST --data "{\"emailAddress\": \"tese320@gmail.com\", \"startTime\": \"2022-08-15\", \"endTime\": \"2022-10-15\"}" http://localhost:8000/queryMail