Easygoing Microservice with Python

Maxim Cherkasov
Sep 25, 2017 · 3 min read
Image for post
Image for post

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 and return JSON response:

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

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

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 path handler.

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

Our class 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 framework, and 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

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 :

$ 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 to :

Now we a ready to start out microservice:

$ cd ./docker
$ docker-compose up

Open browser and enter

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.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store