Tag Archives: Building Microservices in Python

BUILDING MICROSERVICES IN PYTHON

What are Microservices?

Microservices is also known as the microservice architecture – is an architectural style that structures an application as a collection of loosely coupled services, which implement business capabilities. The microservice architecture allows the continuous delivery/deployment of large and complex applications. It also enables an organization to evolve its technology stack.

The microservice architecture pattern language is a collection of patterns for applying the microservice architecture. It has two goals, they are:

* The pattern language enables the user to decide whether microservices are a good fit for their application or not.

* The pattern language enables the user to use the microservice architecture successfully.

Explore this article and know how to build microservices in python.

Microservices design helps to ease the problems associated with the monolithic model. Implementing microservices is perhaps one of the greatest ways to improve the productivity of a software engineering team. This is especially true if the following takes place:

Building_Microservices_in_Python

* User deploys only the component that was changed. This keeps the deployments and the tests manageable.

* If multiple team members are working on the application, they need to wait until everyone is done with development and testing to move forward. However, with the microservices services model, whenever each piece of functionality is ready, it could be deployed.

* Each microservice runs in its own process space, so if the user wants to scale for any reason, he canscale the particular microservice that needs more resources instead of scaling the entire application.

* When one microservice needs to communicate with another microservice, it uses a lightweight protocol such as HTTP

MICROSERVICES DESIGN

Microservices_Design

Let’s examine how the configuration would look for a Python and Django application that runs on Nginx on a typical Linux server.

As the application code is spread across multiple repos grouped by a logically independent code, this will be the typical organization of the application directories on the server.

[ec2-user@ip-172-31-34-107 www]$ pwd
/opt/www
[ec2-user@ip-172-31-34-107 www]$ ls -lrt
total 8
drwxr-xr-x. 5 root root 4096 Oct 12 14:09 microservice1
drwxr-xr-x. 7 root root 4096 Oct 12 19:00 microservice2
drwxr-xr-x. 5 root root 4096 Oct 12 14:09 microservice3
drwxr-xr-x. 7 root root 4096 Oct 12 19:00 microservice4

Nginx, which is deployed by a front-end gateway or a reverse proxy, will have the configuration:

[ec2-user@ip-172-31-34-107 serviceA]$ cat /etc/nginx/conf.d/service.conf
upstream django1 {
server unix:///opt/www/service1/uwsgi.sock; # for a file socket
}
upstream django2 {
server unix:///opt/www/service2/uwsgi.sock; # for a file socket
}
upstream django3 {
server unix:///opt/www/service3/uwsgi.sock; # for a file socket
}
upstream django4 {
server unix:///opt/www/service4/uwsgi.sock; # for a file socket
}
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name localhost;
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
location /api/service1/ {
uwsgi_pass django1;
include /etc/nginx/uwsgi_params;
}
location /api/service2/ {
uwsgi_pass django2;

include /etc/nginx/uwsgi_params;
}
location /api/service3/ {
uwsgi_pass django3;
include /etc/nginx/uwsgi_params;
}
location /api/service4/ {
uwsgi_pass django4;
include /etc/nginx/uwsgi_params;
}

Multiple uWSGI processes have to be created that would process the request for each microservice:

/usr/bin/uwsgi –socket=/opt/www/service1/uwsgi.sock –module=microservice-test.wsgi –master=true –chdir=/opt/www/service1
/usr/bin/uwsgi –socket=/opt/www/service2/uwsgi.sock –module=microservice-test.wsgi –master=true –chdir=/opt/www/service2
/usr/bin/uwsgi –socket=/opt/www/service3/uwsgi.sock –module=microservice-test.wsgi –master=true –chdir=/opt/www/service3
/usr/bin/uwsgi –socket=/opt/www/service4/uwsgi.sock –module=microservice-test.wsgi –master=true –chdir=/opt/www/