{"id":15783630,"url":"https://github.com/deepmancer/microservice-docker-example","last_synced_at":"2025-04-01T16:30:40.067Z","repository":{"id":216971770,"uuid":"742482587","full_name":"deepmancer/microservice-docker-example","owner":"deepmancer","description":"A Dockerized microservice example for managing student data with scalable components, Nginx reverse proxy, and PostgreSQL integration.","archived":false,"fork":false,"pushed_at":"2024-08-16T11:45:28.000Z","size":122,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-11T20:02:29.599Z","etag":null,"topics":["backend-services","boilerplate-template","docker","docker-compose","example-project","gateway","gateway-microservice","microservice","microservices-architecture","nginx","nginx-reverse-proxy","postgresql","python3"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/deepmancer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-12T15:23:02.000Z","updated_at":"2024-08-26T08:51:55.000Z","dependencies_parsed_at":"2024-10-04T20:10:40.234Z","dependency_job_id":null,"html_url":"https://github.com/deepmancer/microservice-docker-example","commit_stats":null,"previous_names":["alirezaheidari-cs/se-lab-week9","deepmancer/se-lab-week9","deepmancer/microservice-gateway-example"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepmancer%2Fmicroservice-docker-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepmancer%2Fmicroservice-docker-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepmancer%2Fmicroservice-docker-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepmancer%2Fmicroservice-docker-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/deepmancer","download_url":"https://codeload.github.com/deepmancer/microservice-docker-example/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246620279,"owners_count":20806738,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backend-services","boilerplate-template","docker","docker-compose","example-project","gateway","gateway-microservice","microservice","microservices-architecture","nginx","nginx-reverse-proxy","postgresql","python3"],"created_at":"2024-10-04T20:00:22.635Z","updated_at":"2025-04-01T16:30:39.805Z","avatar_url":"https://github.com/deepmancer.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🏛️ Microservices Architecture for University Student Management\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://img.shields.io/badge/Flask-000000.svg?style=for-the-badge\u0026logo=Flask\u0026logoColor=white\" alt=\"Flask\"\u003e\n  \u003cimg src=\"https://img.shields.io/badge/NGINX-009639.svg?style=for-the-badge\u0026logo=NGINX\u0026logoColor=white\" alt=\"NGINX\"\u003e\n  \u003cimg src=\"https://img.shields.io/badge/PostgreSQL-4169E1.svg?style=for-the-badge\u0026logo=PostgreSQL\u0026logoColor=white\" alt=\"PostgreSQL\"\u003e\n  \u003cimg src=\"https://img.shields.io/badge/python-3670A0?style=for-the-badge\u0026logo=python\u0026logoColor=ffdd54\" alt=\"Python\"\u003e\n  \u003cimg src=\"https://img.shields.io/badge/Docker-2496ED.svg?style=for-the-badge\u0026logo=Docker\u0026logoColor=white\" alt=\"Docker\"\u003e\n\u003c/p\u003e\n\nWelcome 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.\n\n---\n\n## 📐 Overall Service Architecture\n![Service Architecture Diagram](https://github.com/deepmancer/SE-Lab-Week9/assets/59364943/742d9296-bea8-4e9e-9054-62a9699778cc)\n\n## 🗂️ Project Structure\n\nHere's a quick overview of the project structure:\n\n```\n.\n├── docker-compose.yml\n├── src\n│   ├── microservice\n│   │   ├── application\n│   │   ├── config\n│   │   ├── data_access\n│   │   ├── domain\n│   │   ├── Dockerfile\n│   │   ├── app.py\n│   │   └── requirements.txt\n│   ├── nginx\n│   │   ├── Dockerfile\n│   │   └── nginx.conf\n│   └── server\n│       ├── app.py\n│       ├── Dockerfile\n│       └── requirements.txt\n└── README.md\n```\n\n## 🛠️ Microservice Design\n\nThis microservice is a self-contained component within the larger university system, designed using a **three-layer architecture**:\n\n- **🖥️ Application Layer**: Manages the application logic, Flask routes, and handles HTTP requests/responses.\n- **🧠 Domain Layer**: Houses the core business logic, handling operations related to student entities and ensuring data consistency.\n- **💾 Data Access Layer**: Abstracts the data storage/retrieval logic, managing CRUD operations with the database.\n\n### ✨ Key Features\nThe Student microservice offers the following capabilities:\n\n- **Add Student**: Create a new student record.\n- **Modify Student**: Update existing student details.\n- **Get Student**: Retrieve a student's details by their ID.\n- **Get All Students**: Fetch a list of all students.\n- **Delete Student**: Remove a student's record from the system.\n\n### 🐳 Dockerfile Breakdown\n\nHere’s a quick glance at the key components of the `Dockerfile` used to containerize this microservice:\n\n```Dockerfile\n# Use an official Python runtime as a base image\nFROM python:3.9\n\n# Set the working directory in the container to /app\nWORKDIR /app\n\n# Copy the current directory contents into the container at /app\nCOPY . .\n\n# Install any needed packages specified in requirements.txt\nRUN pip install --no-cache-dir -r requirements.txt\n\n# Make port 5000 available to the world outside this container\nEXPOSE 5000\n\n# Define environment variable\nENV FLASK_APP=app.py\n\n# Run the application when the container launches\nCMD [\"flask\", \"run\", \"--host=0.0.0.0\", \"--port=5000\"]\n```\n\n## 🌐 Server Component\n\nThe 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.\n\n### 📬 API Endpoints\n\n- `POST /student`: Adds a new student.\n- `PUT /student`: Modifies existing student details.\n- `GET /student`: Retrieves a student’s information.\n- `DELETE /student`: Deletes a student record.\n- `GET /students`: Retrieves information for all students.\n\n### 🐳 Dockerfile Breakdown\n\n```Dockerfile\n# Use an official Python runtime as a base image\nFROM python:3.9\n\n# Set the working directory in the container to /app\nWORKDIR /app\n\n# Copy the current directory contents into the container at /app\nCOPY . .\n\n# Install any needed packages specified in requirements.txt\nRUN pip install --no-cache-dir -r requirements.txt\n\n# Make port 4000 available to the world outside this container\nEXPOSE 4000\n\n# Define environment variable\nENV FLASK_APP=app.py\n\n# Run the application when the container launches\nCMD [\"flask\", \"run\", \"--host=0.0.0.0\", \"--port=4000\"]\n```\n\n## 🌍 Nginx Configuration\n\nThe 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.\n\n### 🔄 Load Balancing\n\n**Least Connections (least_conn)**: This algorithm directs traffic to the backend server with the fewest active connections, ensuring efficient load distribution.\n\n### 🐳 Nginx Dockerfile\n\n```nginx\nevents {}\n\nhttp {\n    upstream backend {\n        least_conn;\n        server microservice:5000;\n    }\n\n    server {\n        listen 80;\n        location / {\n            proxy_pass http://backend;\n            proxy_set_header Host $host;\n            proxy_set_header X-Real-IP $remote_addr;\n            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n            proxy_set_header X-Forwarded-Proto $scheme;\n        }\n    }\n}\n```\n\n## 🗄️ Database Configuration\n\nThe microservice uses a PostgreSQL database to manage student data.\n\n### 📋 Student Table Schema\n\n| Column            | Type            | Constraints     |\n|-------------------|-----------------|-----------------|\n| `id`              | `SERIAL`        | `PRIMARY KEY`   |\n| `name`            | `VARCHAR(100)`  |                 |\n| `age`             | `INT`           |                 |\n| `student_id`      | `VARCHAR(50)`   | `UNIQUE`        |\n| `education_level` | `VARCHAR(50)`   |                 |\n\n`education_level` must be either `undergraduate`, `graduate`, or `phd`.\n\n## 🐳 Docker Configuration\n\nThis project uses Docker and Docker Compose to orchestrate the microservice architecture, ensuring each component is containerized for easy deployment and isolation.\n\n### 📦 Docker Compose Setup\n\n```yaml\nversion: '3'\n\nservices:\n  nginx:\n    build: ./src/nginx\n    ports:\n      - \"7000:80\"\n    depends_on:\n      - server\n      - microservice\n\n  server:\n    build: ./src/server\n    ports:\n      - \"4000:4000\"\n    depends_on:\n      - postgres\n\n  microservice:\n    build: ./src/microservice\n    scale: 3\n    depends_on:\n      - postgres\n\n  postgres:\n    image: postgres:latest\n    ports:\n      - \"5432:5432\"\n    environment:\n      POSTGRES_PASSWORD: '1qaz2wsx@'\n      POSTGRES_DB: 'postgres'\n    volumes:\n      - postgres_data:/var/lib/postgresql/data\n\nvolumes:\n  postgres_data:\n```\n\n### ⚙️ Running the Application\n\nTo run the application, simply use Docker Compose:\n\n```bash\ndocker-compose up --build --scale microservice=3\n```\n\nThis 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.\n\n## ✅ Testing the Microservice\n\nHere’s how the application works in action:\n\n1. **Initial API Call** - Fetch all students:\n   ![Initial API Call](https://github.com/deepmancer/SE-Lab-Week9/assets/59364943/f7e69816-3a34-4ba6-a4ce-a8ce9c7f7c27)\n\n2. **Adding Students** - Add three students:\n   ![Add Student 1](https://github.com/deepmancer/SE-Lab-Week9/assets/59364943/415329d3-5a7c-40d4-aaa4-23836289f04a)\n   ![Add Student 2](https://github.com/deepmancer/SE-Lab-Week9/assets/59364943/1cfa2dde-79dc-4aa0-92e0-68cf93c96176)\n   ![Add Student 3](https://github.com/deepmancer/SE-Lab-Week9/assets/59364943/69219662-5770-45f5-b416-1d464bc34a24)\n\n3. **Fetch All Students** - Call the `/students/` API again:\n   ![Fetch All Students](https://github.com/deepmancer/SE-Lab-Week9/assets/59364943/51f59937-02c0-4c51-a834-5e86da255b2c)\n\n4. **Fetch Single Student** - Get details of a specific student:\n   ![Fetch Single Student](https://github.com/deepmancer/SE-Lab-Week9/assets/59364943/4091fec1-0936-4147-b22c-2a5592e5b8b5)\n\n5. **Modify Student** - Update a student’s details:\n   ![Modify Student](https://github.com/deepmancer/SE-Lab-Week9/assets/59364943/4f4f214b-e81b-4\n\nca6-b85e-1f8d393a2b48)\n   ![Updated Student](https://github.com/deepmancer/SE-Lab-Week9/assets/59364943/78695dca-fc33-4c9b-bc98-06fea25673bc)\n\n6. **Delete Student** - Remove a student’s record:\n   ![Delete Student](https://github.com/deepmancer/SE-Lab-Week9/assets/59364943/72fb07b1-c7e0-42bf-b860-72cca3435121)\n   ![Post Deletion](https://github.com/deepmancer/SE-Lab-Week9/assets/59364943/7a2549ca-5d9c-4fa1-ad86-bd7ce442f10e)\n\n### 📜 Logs\n\nFor detailed logs, check the container logs:\n![Logs](https://github.com/deepmancer/SE-Lab-Week9/assets/59364943/7bf57d94-f6eb-458d-8b86-bf3b80b82519)\n\n---\n\n### 🚀 Get Started\n\nClone the repository, navigate to the project directory, and launch the services using Docker Compose to experience this microservices architecture in action!\n\n```bash\ngit clone https://github.com/your-repository-url.git\ncd your-repository-directory\ndocker-compose up --build\n```\n\n---\n\n## 📝 License\n\nThis project is licensed under the MIT License. For detailed information, please refer to the [LICENSE](LICENSE) file.\n\n---\n\nFeel free to explore, contribute, and provide feedback!\n\n**Happy Coding!** 🎉\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeepmancer%2Fmicroservice-docker-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeepmancer%2Fmicroservice-docker-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeepmancer%2Fmicroservice-docker-example/lists"}