Building a simple Microservice

Basic Requirements:

  • A computer with internet connection
  • Python Programming
  • Installing Flask
  • A basic idea of Flask
  • A basic idea of the Python virtual environment is a plus

Before proceeding, let’s create a virtual environment. It is important to create a virtual environment to keep our project dependencies and environment isolated and don’t conflict with the other projects in the computer environment. This is a safer process and easy to manage the project environment.

Use the following commands in the terminal to create a virtual environment:

python -m venv venv

Activate our virtual environment:

In Linux and Mac OS use the below command in the terminal:

source venv/bin/activate

In Windows, use the below command in the command prompt:

venv\Scripts\activate

NOTE: Use deactivate keyword to come out of the virtual environment.

The above will activate the virtual environment and you can see (venv) on the left side of the terminal. Now let us create a directory with the name microservices. Create a requirements.txt file in the microservices directory.

Add the following in the requirements.txt. These all modules are required in our project.

flask
requests
random

Use the following pip command to install all the above modules in our virtual environment:

pip install -r requirements.txt

Create a Python file named microservice.py in the same directory and add the following code:

Python3




from flask import Flask, jsonify
 
app = Flask(__name__)
 
@app.route("/hello", methods=['GET'])
def hello_microservice():
    message = {"message": "Hello from the microservice! This is w3wiki"}
    return jsonify(message)
 
if __name__ == "__main__":
    app.run(port=8000)


Here, we created our microservice at the route /hello and deployed it on port 8000. So you can access it at http://127.0.0.1:8000/hello. Also, the function hello_microservice() returns the JSON data as output.

Run the microservice.py using the Python command:

python microservice.py

Go to http://127.0.0.1:8000/hello in your browser. You can see the JSON output.

Output:

First Microservice

NOTE: Please note that if you encounter a “URL not found” error in the Chrome browser, consider trying the following link instead: http://127.0.0.1:8000//hello

You can also use the curl command in the terminal to get the output. You’ll receive the same JSON response.

curl http://127.0.0.1:8000/hello

Output

{"message": "Hello from the microservice! This is w3wiki"}

Build Your Own Microservices in Flask

Nowadays Python is everywhere. This is majorly due to its simplicity and wide range of applications. Flask is a lightweight web framework for building web applications in Python. Creating microservices with Flask is straightforward.

In this article, we will start with a basic “hello world” microservice. Following that, we’ll delve into the creation of two distinct microservices and explore how to establish seamless communication between them. So let’s begin with understanding the concept of a Microservice.

What is a microservice?

A microservice is a software architectural style whereby an application is divided into smaller sub-parts. These sub-parts are independent of each other i.e., they are independently deployable and scalable. Each microservice performs a specific function or a single task.

Why do we need microservices?

Imagine an e-commerce app like Amazon or Flipkart. Here the app needs to satisfy many tasks like

Order management: placing orders, tracking status etc.

Cart management: Updating cart, calculating cart value etc.

Customer Profile: Name, email, membership management etc.

As you can see, a single app performs many tasks. However, trying to write code for all these tasks within a single framework can make it complicated and challenging to manage and debug. Hence the concept of microservices came into the picture. Each microservice is designed to perform a single task. Furthermore, microservices possess the ability to communicate with each other whenever it’s required.

Similar Reads

Building a simple Microservice

Basic Requirements:...

Communication Between Microservices

...

Contact Us