DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Manage Microservices With Docker Compose
  • How to Build Microservices With Node.js
  • OpenCV Integration With Live 360 Video for Robotics
  • Application-Level Tracing: The Good, the Bad, and the Alternative

Trending

  • Chaos Engineering for Microservices
  • Zero Trust for AWS NLBs: Why It Matters and How to Do It
  • Why Documentation Matters More Than You Think
  • A Modern Stack for Building Scalable Systems
  1. DZone
  2. Data Engineering
  3. Data
  4. How to Build Microservices in Python

How to Build Microservices in Python

In this article, learn how to deploy microservices that are created using either Python and Django on both Nginx and uWSGI.

By 
Rakesh Kalidindi user avatar
Rakesh Kalidindi
·
Nov. 15, 16 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
27.7K Views

Join the DZone community and get the full member experience.

Join For Free

microservices design helps alleviate some 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:

  • you deploy only the component that was changed (as opposed to the whole application). 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 and ready 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 we want to scale for any reason, we can scale the particular microservice that needs more resources instead of scaling the entire application .
  • when one microservice needs to communicate with another micro-service, it uses a lightweight protocol such as http.

this post examines how to deploy microservices created using python and django on nginx and uwsgi.

before going into the details of a microservices design, let's understand and examine how a typical monolithic application looks.

monolithic design

monolithic-design

however, with breaking the functionality into logically isolated micro-services, this is how the design would like.

microservices design

microservices-design

now, let's examine how the configuration would look for a python and django application that runs on nginx on a typical linux server.

because the application code is spread across multiple repos (or subrepos) grouped by 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 typically deployed a front-end gateway or a reverse proxy, will have this 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/service4


microservice Python (language) application Build (game engine)

Published at DZone with permission of Rakesh Kalidindi. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Manage Microservices With Docker Compose
  • How to Build Microservices With Node.js
  • OpenCV Integration With Live 360 Video for Robotics
  • Application-Level Tracing: The Good, the Bad, and the Alternative

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!