Skip to content

microservices-suite/node-microservices-suite

Repository files navigation

Microservices Suite 🦧

Overview

Welcome to the πŸ“¦ Microservices Suite project! This suite is a collection of Node.js microservices built using the 🦧 mono-repo strategy and leveraging the yarn workspaces concept. Each microservice runs in its isolated Docker container, and Kubernetes orchestrates the deployment, providing scalability and efficiency.

Project file structure

β”œβ”€ node-microservices-suite
β”‚  β”œβ”€ api-gateways/
|  β”‚  β”œβ”€ app-1/
|  β”‚  β”‚  β”œβ”€ nginx
|  β”‚  β”‚  β”œβ”€ apache
|  β”‚  β”‚  β”œβ”€ README.md
β”‚  β”œβ”€ graphql/
|  β”‚  β”œβ”€ app-1/
|  β”‚  β”‚  β”œβ”€ apollo-server # placeholder file atm
|  β”‚  β”‚  β”œβ”€ README.md
β”‚  β”œβ”€ data-persistence/
|  β”‚  β”œβ”€ db-1/
|  β”‚  β”‚  β”œβ”€ Dockerfile
|  β”‚  β”‚  β”œβ”€ README.md
|  β”‚  β”œβ”€ sqlite/ #for in memory or disk db for miniature devices or prototyping
|  β”‚  β”œβ”€ β”œβ”€ db/
|  β”‚  β”‚  β”œβ”€ README.md
β”‚  β”œβ”€ k8s/
|  β”‚  β”œβ”€ service-1/
|  β”‚  β”‚  β”œβ”€ cluster-ip-service.yml
|  β”‚  β”‚  β”œβ”€ cluster-deployment.yml
|  β”‚  β”‚  β”œβ”€ ingress-service.yml
|  β”‚  β”‚  β”œβ”€ README.md
|  β”œβ”€ microservices/
|  β”‚  β”œβ”€ service-1/
|  β”‚  β”‚  β”œβ”€ src
|  β”‚  β”‚  β”œβ”€ .env
|  β”‚  β”‚  β”œβ”€ .env.dev
|  β”‚  β”‚  β”œβ”€ app.js
|  β”‚  β”‚  β”œβ”€ Dockerfile
β”œβ”€ β”‚  β”‚  β”œβ”€ Dockerfile.dev
|  β”‚  β”‚  β”œβ”€ ecosystem.config.js
|  β”‚  β”‚  β”œβ”€ index.js
|  β”‚  β”‚  β”œβ”€ package.json
|  β”‚  β”‚  β”œβ”€ task.json
|  β”œβ”€ shared/
|  β”‚  β”œβ”€ library-1/
|  β”‚  β”‚  β”œβ”€ APIError.js
|  β”‚  β”‚  β”œβ”€ catchAsync.js
|  β”‚  β”‚  β”œβ”€ index.js
|  β”‚  β”‚  β”œβ”€ package.json
|  β”‚  β”‚  β”œβ”€ pick.js
|  β”‚  β”‚  β”œβ”€ README.md
|  β”‚  β”‚  β”œβ”€ validate
β”‚  β”œβ”€ tests/
|  β”‚  β”œβ”€ service-1/
|  β”‚  β”‚  β”œβ”€ e2e/
|  β”‚  β”‚  β”œβ”€ integration/
|  β”‚  β”‚  β”œβ”€ snapshot/ #if it's a micro-frontend service
|  β”‚  β”‚  β”œβ”€ unit/
|  β”‚  β”‚  β”œβ”€ README.md
|  β”œβ”€ .gitignore
|  β”œβ”€ .npmrc
|  β”œβ”€ .yarnrc.yml
|  β”œβ”€ docker-compose.yml
|  β”œβ”€ package.json
|  β”œβ”€ production.yml
|  β”œβ”€ README.md
|  β”œβ”€ Taskfile.yml
|  β”œβ”€ yarn.lock

Monorepo strategy benefits for microservices:

  • Enforce DRY Principles:

  • Collaboration:

    • Working within the same repository facilitates learning from peers.
    • Enables the adoption of good coding practices and the avoidance of bad ones.
  • Centralized Development Chores:

    • Manage common files like .gitignore centrally from the root directory
    • Reduce unnecessary repetitions in the codebase.
  • Easily Integrate Development Automation:

    • Task runner configurations and docker-compose can be managed from the root directory.
    • simplify the automation of repetitive workflows.
  • Code Sharing Anywhere:

    • Publish and import organization-scoped libraries to the npm registry with
    yarn publish 
    yarn add <@microservices-suite/foo> or
    yarn @microservices-suite/<workspace-name> add <@microservices-suite/foo>
  • Easily Containerize and Scale:
    • Decouple every microservice to scale individually.
    • Leverage the no-hoist yarn workspace feature and custom scripts to enable efficient packaging of microservices into isolated containers.

Technologies Used

  • Yarn Workspaces:

    • Simplifies managing multiple packages within a single repository.
    • Encourage code sharing & reduce duplication
    • Make it easier to handle dependencies and scripts.
  • Docker Containers:

    • Provides lightweight, portable, and self-sufficient containers for packaging and deploying microservices.

Alpine Images

  • Alpine images are very minimalistic Linux minidistros that cost you only ~5MB of real estate. That is why they are used inside your favourite smart watch ⌚️.
  • One objective of this project is to optimize image builds for Continuous Integration (CI) and production environments through the utilization of the slimmest possible images so that you dont bloat ⚠️ your machine in dev and we have a lean server in prod.

Key Strategies:

  • Multi-Stage Builds:

    • We employ simple Docker constructs like multi-stage builds to optimize our image size.
  • Hoisting and Symlinking:

    • While these practices promote the DRY (Don't Repeat Yourself) principle, they pose a threat to our objective of achieving minimized images. To mitigate this, we leverage no-hoisting and symlinked libraries
    • selectively copying only the node_modules generated by non-hoisted workspaces.
    • These are optimized to consume less disk space, ensuring that only the essential shared libraries required by a specific service are included, thereby maintaining the slim profile of our images.
    • The service itself will build her core node modules normally inside the dockerfile from her package.json

Docker & Node Best Practices

Kubernetes (k8s)

  • Kubernetes offers automated deployment, scaling, and management of containerized applications, ensuring reliability and scalability.

Getting Started

Welcome to our project! To ensure a smooth setup and development experience, ensure you have the following tools installed on your machine:

  • Docker:
    • For containerization and managing containerized applications.
    • πŸ‘‰ Install docker here.
    • We love πŸ’š alpine images <small,simple,secure>.
    • You can read about the specific flavor here
  • Task Runner Automation Tool:
  • Node.js:
  • At the project <service_root> create .env, .env.dev and .env.staging files and copy environment variables from the .env.example file

Running Services

  • This project uses Task Runner Automation Tool to streamline the process of starting services in both development and production. Follow these steps to get your environment up and running:
  • You can derive the service_name of a service from the workspace name found in the package.json "name": property e.g
"name": "@microservices-suite/<service_name>"
  • To run a service in either modes [dev,staging,prod]:
  • Action is similar to [up,down] docker compose syntax where up & down start and stop the service respectively with docker-compose <action>
task do:<service_name>:<mode>:<action>
  • This command uses docker-compose to start your service(s)
  • The task runner will handle the setup, ensuring your service is ready.

Running Services Without Docker

  • If you prefer not to use Docker, you can use the vanilla task command to start services using node PM2 engine in production or nodemon in any other mode:
task vanilla:<service_name>:<mode>

Using Docker-Compose Directly

  • Should you need to use docker-compose directly for more control over the container orchestration, you can utilize the standard commands provided by Docker:
docker-compose up
docker-compose down

Contributing

Contributions are welcome! If you'd like to contribute to the Microservices Suite project, please follow these guidelines:

  1. Fork the repository and clone it to your local machine.
git clone https://github.com/microservices-suite/node-microservices-suite.git
  1. Create a new branch for your feature or bug fix:
git checkout -b feat/<my-feature>
  1. Make your changes and make sure that tests pass.
  2. Commit your changes using the Angular commit message convention:
  3. Push to the branch:
git push origin feat/<my-feature>
  1. Submit a pull request detailing your changes.
  2. Please ensure that your pull request adheres to the project's code style and conventions.

License

  • This project is licensed under the MIT License. Feel free to use, modify, and distribute this code for any purpose.

Acknowledgements

We would like to thank the developers and contributors to the following technologies used in this project:

Packages

No packages published

Contributors 3

  •  
  •  
  •