thespacebetweenstars.com

Building Your First Microservice: Infrastructure Setup Guide

Written on

Chapter 1: Introduction to Microservices

In this initial segment of my third article on microservices, we will construct a straightforward application with a focused purpose. This is at the core of what microservices represent. As mentioned in my previous piece, a microservice functions as an independent service tailored to execute a particular task. It zeroes in on one function and addresses it, thereby contributing to a broader solution offered by the overall system.

To enhance portability and efficiency, we will package the application within a Docker container for execution.

"This segment will provide a practical example rather than just theoretical insights into microservices."

Section 1.1: Project Overview

This article marks a shift from theory to practice as we develop a small Node.js application that retrieves cryptocurrency data every 15 minutes and stores it in a database. Its sole function will be to keep this data current.

The application will run continuously, fetching data from the CoinGecko API and updating the database while also purging outdated information. It's important to avoid overcomplicating the design; having one service for data retrieval and another for deletion would only add unnecessary complexity.

We will utilize PostgreSQL for data storage, allowing multiple applications to access the database. Some will gather data, while others will read it for various purposes, such as notifying users when a coin exceeds a certain threshold or displaying real-time statistics.

For the completed application, please refer to the repository linked below, but I highly recommend following this article step by step to fully grasp the processes involved.

Section 1.2: Setting Up the Folder Structure

The first order of business is to establish a basic folder structure. Given the simplicity of this application, a few directories will suffice. Since we plan to operate all applications within containers, it’s advisable to keep the source code separate from the top-level structure. This is a good practice for any type of application development.

The foundational folder structure should resemble the following layout:

Basic folder structure for microservice application

You can explore this structure by checking the relevant commit. To clone the project, use the following commands:

git clone [email protected]:itd24/microservices-crypto-watcher.git

To check out the specific commit, refer to this tag:

git checkout basic-folder-structure

Chapter 2: Crafting the Dockerfile

As our application will run inside a Docker container, it is crucial to create a Dockerfile. If you're not familiar with Docker or Docker Compose, I recommend reviewing those concepts before proceeding, as they will be integral to our process.

The Dockerfile contains a sequence of instructions guiding Docker on what actions to perform, such as copying files from the host to the container, executing commands, and starting the application.

Let’s examine the next step:

git checkout writing-a-dockerfile

The Dockerfile is quite straightforward. We start by using a public Node.js container, which saves us from redundant work. This gives us a functional container with Node.js 14 and NPM pre-installed.

Next, we set the working directory and transfer the package.json file, along with any existing package-lock.json, into the container. Utilizing wildcards is helpful here. Once the package.json is in the container, we install all necessary packages.

Afterward, we copy the application’s source code into the container. While we could share the host system's directory, copying ensures that the container functions as a black box, performing its duties without exposing internal processes.

The final step involves running our application. After building the container, everything except the last step will remain dormant until we execute the container. However, we won’t utilize the Dockerfile just yet; we need to develop the application first, which entails coding, testing, and sometimes troubleshooting.

Section 2.1: Establishing a Development Environment with Docker Compose

I prefer keeping my machine uncluttered, avoiding unnecessary installations like Node.js, Python, or SQL servers. To maintain a clean workspace, we'll utilize Docker Compose.

Next, check the following step:

git checkout creating-a-development-environment

We will create a new file, docker-compose.yml, which contains instructions for running multiple containers and managing their interactions. You can either utilize your own containers or leverage existing public ones.

If you're interested in learning more about Docker development environments, I have an article detailing the setup process.

Here’s what our docker-compose.yml looks like:

The file is quite simple. The first line specifies the Docker Compose file version, which generally does not impact functionality but defines the commands available based on the Docker Compose documentation.

We then set up the services; each service corresponds to one container. In our scenario, we only have the app container, utilizing a public Node image. The container name can be anything unique.

The next line outlines our restart policy, ensuring the container restarts automatically unless we shut it down manually. If the application crashes, the container will stop but will restart automatically.

Instead of copying files that we will modify, we will share the directory with the container, allowing changes to reflect immediately. This setup is ideal for development, but there is a caveat: syncing files often incurs overhead. Excluding the node_modules folder from sharing is essential, as it typically contains numerous files.

We will need to run npm install inside the container whenever we update package.json, but this trade-off greatly enhances development efficiency.

The penultimate line sets our working directory, while the last line tells the container to execute a non-terminating command, ensuring it remains active until shut down. This is crucial for our development environment, allowing us to log in and run our application whenever needed.

To kick off our development process, we will start our containers and access the app container via SSH.

Use this command to start all services defined in the docker-compose.yml in the background:

docker-compose up -d

To verify that the containers are running, execute:

docker ps

If your response indicates that the container is operational, you can log into it via SSH and begin developing:

docker exec -it cryptowatcher bash

Congratulations! You can now run commands within your very own Node.js container.

Chapter 3: Conclusion and Next Steps

After careful consideration, I’ve decided to split this article into two parts to maintain reader engagement. My goal is to ensure that my writing remains interesting and digestible. In the subsequent article, I will guide you through building your first microservice using the infrastructure we have established here. Happy coding!

A comprehensive video on constructing your first microservice with .NET.

Learn how to build and deploy your first microservice in .NET with practical insights.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Error Management Strategies in Axum Framework for REST APIs

Explore effective error management strategies in Axum for robust REST APIs in Rust.

Eruption of Yellowstone Supervolcano: Are We Due for a Blast?

Exploring the potential of the Yellowstone Supervolcano's eruption and its implications for humanity.

# Essential Features I Wish My Apple Watch Had

Discover the features I desperately want on my Apple Watch for a more independent experience.

Understanding Business Capabilities for Strategic Success

Exploring how business capabilities integrate people, processes, and technology to enhance strategic planning and value delivery.

Identifying Workplace Culture Killers: What to Watch For

Discover common indicators of poor workplace culture and how to address them effectively.

Embracing Positivity: A Journey Through Life's Challenges

A reflection on overcoming life's challenges through positivity and self-awareness.

# Exploring the Journey of Dr. Maria Brigida Brunetti in Science

Dr. Maria Brigida Brunetti shares her intriguing journey in science, from early influences to her work in particle physics.

Mastering Browser Automation with Python's Playwright Library

Discover how to use Python's Playwright for web automation, including installation, scripting, and debugging techniques.