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…, 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 runserver {port}
 Copy code 

To configure Django

But you may wonder who my request is assigned to , Then in 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 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 


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.

Random recommended