microservice-docker-example

A Dockerized microservice example for managing student data with scalable components, Nginx reverse proxy, and PostgreSQL integration.

MIT License

Stars
1

πŸ›οΈ Microservices Architecture for University Student Management

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.


πŸ“ Overall Service Architecture

πŸ—‚οΈ Project Structure

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

πŸ› οΈ Microservice Design

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.

✨ Key Features

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.

🐳 Dockerfile Breakdown

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"]

🌐 Server Component

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.

πŸ“¬ API Endpoints

  • 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.

🐳 Dockerfile Breakdown

# 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"]

🌍 Nginx Configuration

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.

πŸ”„ Load Balancing

Least Connections (least_conn): This algorithm directs traffic to the backend server with the fewest active connections, ensuring efficient load distribution.

🐳 Nginx Dockerfile

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;
        }
    }
}

πŸ—„οΈ Database Configuration

The microservice uses a PostgreSQL database to manage student data.

πŸ“‹ Student Table Schema

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.

🐳 Docker Configuration

This project uses Docker and Docker Compose to orchestrate the microservice architecture, ensuring each component is containerized for easy deployment and isolation.

πŸ“¦ Docker Compose Setup

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:

βš™οΈ Running the Application

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.

βœ… Testing the Microservice

Here’s how the application works in action:

  1. Initial API Call - Fetch all students: Initial API Call

  2. Adding Students - Add three students: Add Student 1 Add Student 2 Add Student 3

  3. Fetch All Students - Call the /students/ API again: Fetch All Students

  4. Fetch Single Student - Get details of a specific student: Fetch Single Student

  5. Modify Student - Update a student’s details: ![Modify Student](https://github.com/deepmancer/SE-Lab-Week9/assets/59364943/4f4f214b-e81b-4

ca6-b85e-1f8d393a2b48) Updated Student

  1. Delete Student - Remove a student’s record:
    Delete Student
    Post Deletion

πŸ“œ Logs

For detailed logs, check the container logs: Logs


πŸš€ Get Started

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

πŸ“ License

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! πŸŽ‰