Easygoing Microservice with Python

You can find all code from this article here: https://github.com/rema7/python-web-microservice/tree/1.0

Description

This small note portray process of creation a self-sufficient microservice.

Microservice will be a web servise, which have only one method ‘/rest/’ and return JSON response:

{
"result": "Wow! Resul!:)",
"timestamp": "2017-09-25T10:43:46.750270"
}

Falcon doesn’t support python datatime JSON seriallization “out of the box”. To handle this issue we’ll write a middleware.

Tools and frameworks

We will you next list of tools and frameworks:

All stuff above help us create a self-sufficient microservice.

Prepare environment

There are a lot of ways to organize project structure. I prefer project structure as below.

Create python virtual environment:

$ mkvirtualenv python-falcon-microservice -p python3.6

Create source dir and add dependencies

$ mkdir -p src/.meta
$ vi src/.meta/packages

Fill dependencies file with information from below:

Install dependencies via next command:

$ pip install -r src/.meta/packages

Create a simple web service

Firstly we will write a ‘/rest’ path handler.

$ mkdir -p src/api/apy.py
$ vi src/api/api.py

Our class RestResourse handle only GET request from web browser. As a result, it returns a JSON object with two fields: result and timestamp.

Secondly, we should write a middleware which responsible for encoding respose to JSON.

$ mkdir -p src/middlewares.py
$ vi src/middlewares.py

And now we’ll create app.py file which is a main file of our service.

$ vi src/app.py

Here we import Falcon framework, RestResource and ContentEncodingMiddleware and chain them into our web service.

Now we a ready to start our web service, but before we should configure our Gunicorn HTTP server.

$ vi src/gunicorn.conf.py

Here wi configure next server parameters:

  • host and port
  • numbers of workers — based on cpu count
  • workers_connections — maximum number of simultaneous clients
  • workers timeout — workers silent for more than this many seconds are killed and restarted

Finally, we can test our service by run whis command:

$ cd ./src
$ gunicorn app:app -c gunicorn.conf.py --reload

Open browser and enter http://localhost:8000/rest

From web to microservice

Create Docker

Docker helps out microservice becomes self-sufficient:

At first, we create docker service directories structure. From root of our project lets enter next commands:

$ mkdir -p docker/backend

Create a Dockerfile:

$ vi docker/backend/Dockerfile

Create a script to start our service:

$ vi docker/backend/start.sh

After script created we must make one executable:

$ chmod a+x docker/backend/start.sh

Finally we craete a docker compose file which will run our service as docker container. Service port change from 8000 to 9000:

Now we a ready to start out microservice:

$ cd ./docker
$ docker-compose up

Open browser and enter http://localhost:9000/rest

Conclusion

I deliberately avoid questions related to testing, databases, documentations and etc.

Of course all of its are important, but the main aim was desire to display how easy to create a skeleton of microservice.