current position:Home>How to configure load balancing for Django service
How to configure load balancing for Django service
2022-02-01 00:05:15 【somenzz】
k current Web Service has a very important performance index called QPS,QPS The full name is Queries Per Second intend “ Query rate per second ”, Is the number of queries a server can query per second , It is a measure of how much traffic a specific query server handles in a given period of time . QPS The higher the , High concurrency , The server can serve more users per second .
Python Most enthusiasts will choose Django To develop Web application . however Python The performance of multithreading is not very high , How to improve Django The concurrency and availability of services ? A very simple way is load balancing , This article shares how to provide Django Service configuration load balancing .
The so-called load balancing is not difficult to understand , There will be multiple back-end services , It is generally distributed on multiple servers , The front-end requests will be evenly distributed to the back-end services , The schematic diagram is shown below :
The load balancer receives requests from clients , Then forward the request to one of the servers . The server then sends the required HTML Content or resources are returned to the load balancer , The load balancer then forwards it back to the client .
There are a variety of load balancers to choose from , for example Nginx、Apache、Tomcat and HAProxy, You can choose any of them , But now I'll just show you how to Windows Upper use Nginx.
download nginx
First , To nginx Download address nginx.org/en/download…, It will display the following page :
Then click on any version of Nginx, I recommend using the stable version , But it doesn't matter . After downloading , Unzip it anywhere ( On your desktop or document ). After decompressing , Enter the just installed nginx Catalog , Enter... In the directory address “cmd”, Here's the picture . And the command prompt will appear .
At the command prompt , Input “start nginx”. After completion , You can open your browser and url Enter... In the column “localhost” To check nginx Whether it works properly . If you're typing “localhost” Then the following screen appears ,Nginx It works .
once Nginx Perfect operation , We can configure Nginx To distribute the load to our servers , I use it here. Django Of runserver Demonstrate as a service .
To configure nginx
Get into nginx' Catalog , open “conf” Folder , And then you should see “nginx.conf” file . Open it in any text editor you are familiar with , I will now use Visual Studio Code, By default nginx.conf The contents of are as follows :
Just comment it out http Part or delete it directly , Add the following :
http {
upstream myproject {
server localhost:8000;
server localhost:8001;
server localhost:8002;
server localhost:8003;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://myproject;
}
}
}
Copy code
This configuration means nginx Will monitor this machine 80 port , Once there is an access , It will be randomly assigned to the following four services for requests :
server localhost:8000;
server localhost:8001;
server localhost:8002;
server localhost:8003;
Copy code
Please make sure to turn on 4 A terminal to simulate 4 A service , Each terminal executes this command , Follow your own port number :
python manage.py runserver {port}
Copy code
To configure Django
But you may wonder who my request is assigned to , Then in views.py Add this method to :
from django.http import HttpResponse
def nginx_view(request):
html = "<h1>Using port " + request.META["SERVER_PORT"] + "<h1>"
return HttpResponse(html)
Copy code
And then in the corresponding urls.py Configure the path on the / , To map to the view function above :
from django.contrib import admin
from django.urls import path
from .views import nginx_view
urlpatterns = [
path('test_load_banlance/', nginx_view),
]
Copy code
verification
Open the browser , Address field input localhost/story10 enter , You will find the following page :
Then close it 8000 service , Refresh the browser again , You will find that the load balancer has been forwarded to 8001:
Isn't it cool ?
If you keep refreshing , It will make random cyclic requests in the remaining services , That is, every time you refresh , Will randomly change a server to request . Yes, of course , You can refer to nginx file Modify this way of selecting back-end services .
Last words
This article shares nginx As the simplest configuration of load balancer , And how to Django Detect which service is accessed in , It's all pretty simple , If you have any harvest, please praise 、 Looking at 、 Focus on supporting .
copyright notice
author[somenzz],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202010005120974.html
The sidebar is recommended
- leetcode 1560. Most Visited Sector in a Circular Track(python)
- leetcode 1995. Count Special Quadruplets(python)
- How to program based on interfaces using Python
- leetcode 1286. Iterator for Combination(python)
- leetcode 1418. Display Table of Food Orders in a Restaurant (python)
- Python Matplotlib drawing histogram
- Python development foundation summary (VII) database + FTP + character coding + source code security
- Python modular package management and import mechanism
- Django serialization (II)
- Python dataloader error "dataloader worker (PID XXX) is killed by signal" solution
guess what you like
-
apache2. 4 + Django + windows 10 Automated Deployment
-
leetcode 1222. Queens That Can Attack the King(python)
-
leetcode 1387. Sort Integers by The Power Value (python)
-
Tiger sniffing 24-hour praise device, a case with a crawler skill, python crawler lesson 7-9
-
Python object oriented programming 01: introduction classes and objects
-
Baidu Post: high definition Python
-
Python Matplotlib drawing contour map
-
Python crawler actual combat, requests module, python realizes IMDB movie top data visualization
-
Python classic: explain programming and development from simple to deep and step by step
-
Python implements URL availability monitoring and instant push
Random recommended
- Python avatar animation, come and generate your own animation avatar
- leetcode 1884. Egg Drop With 2 Eggs and N Floors(python)
- leetcode 1910. Remove All Occurrences of a Substring(python)
- Python and binary
- First acquaintance with Python class
- [Python data collection] scrapy book acquisition and coding analysis
- Python crawler from introduction to mastery (IV) extracting information from web pages
- Python crawler from entry to mastery (III) implementation of simple crawler
- The apscheduler module in Python implements scheduled tasks
- 1379. Find the same node in the cloned binary tree (Java / C + + / Python)
- Python connects redis, singleton and thread pool, and resolves problems encountered
- Python from 0 to 1 (day 11) - Python data application 1
- Python bisect module
- Python + OpenGL realizes real-time interactive writing on blocks with B-spline curves
- Use the properties of Python VTK implicit functions to select and cut data
- Learn these 10000 passages and become a humorous person in the IT workplace. Python crawler lessons 8-9
- leetcode 986. Interval List Intersections(python)
- leetcode 1860. Incremental Memory Leak(python)
- How to teach yourself Python? How long will it take?
- Python Matplotlib drawing pie chart
- Django paging (II)
- Concurrent. For Python concurrent programming Futures or multiprocessing?
- Programmers over the age of 25 can't know a few Chinese herbal medicines. Python crawler lessons 9-9
- Python crawler from introduction to pit full series of tutorials (detailed tutorial + various practical combat)
- The second bullet of class in Python
- Python object oriented programming 03: class inheritance and its derived terms
- How IOS developers learn Python Programming 13 - function 4
- Python crawler from introduction to mastery (VI) form and crawler login
- Python crawler from entry to mastery (V) challenges of dynamic web pages
- Deeply understand pandas to read excel, TXT, CSV files and other commands
- Daily python, Chapter 18, class
- "I just want to collect some plain photos in Python for machine learning," he said. "I believe you a ghost!"
- Django view
- Python implements filtering emoticons in text
- When winter comes, python chooses a coat with temperament for mom! Otherwise, there's really no way to start!
- Python crawler - get fund change information
- Highlight actor using Python VTK
- Python crawler actual combat: crawling southern weekend news articles
- leetcode 406. Queue Reconstruction by Height(python)
- leetcode 1043. Partition Array for Maximum Sum (python)