Simple implementation of python load balancing, python Load Balancing implementation

Source: Internet
Author: User

Simple implementation of python load balancing, python Load Balancing implementation

When talking about distributing requests, I believe most people will first think of Nginx. As a multi-function server, Nginx not only provides the reverse proxy to hide the Host ip address, but also has a simple cache acceleration function. Of course, the most powerful function of Nginx is to distribute requests. It not only provides hash, consistent hash, Server Load balancer and other request distribution modes, but also ensures the lightweight and stable services of its own. An Nginx server works in highly concurrent requests all the year round and rarely goes down.

In Nginx Server Load balancer mode, requests are sent to servers with the least pressure and no downtime. Today, we do not consider the pressure on the target server. We use python to implement the simplest load balancing method, which sends requests to servers that are not down.

We want to call the interface in the module_ B module. The module_ B service is deployed on the four ports 10081,10082, 10083,10084 ON THE 10.10.115 server.

#!/usr/bin/python# -*- coding: utf-8 -*-import requestsimport randomimport osimport sysimport timeimport ConnectionErrorimport Module_bExceptionmodule_b = "10.10.10.115:10081,10.10.10.115:10082,10.10.10.115:10083,10.10.10.115:10084"class Module_b():  def __init__(self):    self.url_prefix = [val.strip() for val in module_b.split(',')]  def _request(self, short_uri, payload):    res = None    try_count = 1    url_prefixs = self.url_prefix[:]    url_prefixs.sort(key=lambda f: random.randint(0, 100))          for curr_url_prefix in url_prefixs:      url = os.path.join(curr_url_prefix, short_uri)      try:        res = requests.post(url, data=payload)        break      except ConnectionError as e:        try_count += 1        sys.stderr.write('can not connect to Module_b, retry ...\n')        time.sleep(1)        if try_count == len(url_prefixs):          raise e    if res.status_code != 200:      raise Module_bException('HTTP ERROR: %s' % res.text)    result = res.json()    if result['status'] != '0':      raise Module_bException(result['errstr'])    return result['result']

ConnecttionError and Module_bException are encapsulated error classes.

The implementation of the entire load balancing is also very simple. Pass in the api and parameters, and then randomly select one from all module_ B addresses and splice them into a complete requests request. If you cannot access the module_ B service, switch to another unaccessed module_ B service address until you have accessed all module_ B services.

Summary

The above is a simple implementation method of python Server Load balancer introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in time!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.