Welcome to the Microservices Architecture project! This repository hosts a Student Service
microservice, built to efficiently manage student data within a university system. This service is Dockerized for seamless deployment and scalability, and is part of a larger system architecture that includes a server and an Nginx reverse proxy.
Here's a quick overview of the project structure:
.
βββ docker-compose.yml
βββ src
β βββ microservice
β β βββ application
β β βββ config
β β βββ data_access
β β βββ domain
β β βββ Dockerfile
β β βββ app.py
β β βββ requirements.txt
β βββ nginx
β β βββ Dockerfile
β β βββ nginx.conf
β βββ server
β βββ app.py
β βββ Dockerfile
β βββ requirements.txt
βββ README.md
This microservice is a self-contained component within the larger university system, designed using a three-layer architecture:
- π₯οΈ Application Layer: Manages the application logic, Flask routes, and handles HTTP requests/responses.
- π§ Domain Layer: Houses the core business logic, handling operations related to student entities and ensuring data consistency.
- πΎ Data Access Layer: Abstracts the data storage/retrieval logic, managing CRUD operations with the database.
The Student microservice offers the following capabilities:
- Add Student: Create a new student record.
- Modify Student: Update existing student details.
- Get Student: Retrieve a student's details by their ID.
- Get All Students: Fetch a list of all students.
- Delete Student: Remove a student's record from the system.
Hereβs a quick glance at the key components of the Dockerfile
used to containerize this microservice:
# Use an official Python runtime as a base image
FROM python:3.9
# Set the working directory in the container to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . .
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port 5000 available to the world outside this container
EXPOSE 5000
# Define environment variable
ENV FLASK_APP=app.py
# Run the application when the container launches
CMD ["flask", "run", "--host=0.0.0.0", "--port=5000"]
The server acts as a gateway, handling client requests and coordinating with the microservice for student data management. It simplifies client interactions by providing a unified API endpoint.
POST /student
: Adds a new student.PUT /student
: Modifies existing student details.GET /student
: Retrieves a studentβs information.DELETE /student
: Deletes a student record.GET /students
: Retrieves information for all students.
# Use an official Python runtime as a base image
FROM python:3.9
# Set the working directory in the container to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . .
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port 4000 available to the world outside this container
EXPOSE 4000
# Define environment variable
ENV FLASK_APP=app.py
# Run the application when the container launches
CMD ["flask", "run", "--host=0.0.0.0", "--port=4000"]
The Nginx service acts as a reverse proxy, efficiently routing incoming HTTP requests to the appropriate backend service. It is configured to ensure optimal load balancing and seamless request forwarding.
Least Connections (least_conn): This algorithm directs traffic to the backend server with the fewest active connections, ensuring efficient load distribution.
events {}
http {
upstream backend {
least_conn;
server microservice:5000;
}
server {
listen 80;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
The microservice uses a PostgreSQL database to manage student data.
Column | Type | Constraints |
---|---|---|
id |
SERIAL |
PRIMARY KEY |
name |
VARCHAR(100) |
|
age |
INT |
|
student_id |
VARCHAR(50) |
UNIQUE |
education_level |
VARCHAR(50) |
education_level
must be either undergraduate
, graduate
, or phd
.
This project uses Docker and Docker Compose to orchestrate the microservice architecture, ensuring each component is containerized for easy deployment and isolation.
version: '3'
services:
nginx:
build: ./src/nginx
ports:
- "7000:80"
depends_on:
- server
- microservice
server:
build: ./src/server
ports:
- "4000:4000"
depends_on:
- postgres
microservice:
build: ./src/microservice
scale: 3
depends_on:
- postgres
postgres:
image: postgres:latest
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: '1qaz2wsx@'
POSTGRES_DB: 'postgres'
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
To run the application, simply use Docker Compose:
docker-compose up --build --scale microservice=3
This command will build the Docker images for each service and start the containers as defined in docker-compose.yml
. The scale
argument can be adjusted to control the number of microservice instances.
Hereβs how the application works in action:
-
Modify Student - Update a studentβs details: ![Modify Student](https://github.com/deepmancer/SE-Lab-Week9/assets/59364943/4f4f214b-e81b-4
For detailed logs, check the container logs:
Clone the repository, navigate to the project directory, and launch the services using Docker Compose to experience this microservices architecture in action!
git clone https://github.com/your-repository-url.git
cd your-repository-directory
docker-compose up --build
This project is licensed under the MIT License. For detailed information, please refer to the LICENSE file.
Feel free to explore, contribute, and provide feedback!
Happy Coding! π