• Upstream Consistent Hash


    介绍

    https://www.nginx.com/resources/wiki/modules/consistent_hash/地址

    ngx_http_upstream_consistent_hash - a load balancer that uses an internal consistent hash ring to select the right backend node. It is designed to be compatible with memcache.hash_strategy = consistent of the php-memcache module. This means you can store values into a memcached cluster using the php-memcache module, and later NGINX can find that value in the cluster and read it from there.

    ngx_http_upstream_consistent_hash-一种使用内部一致哈希环来选择正确后端节点的负载均衡器。它被设计为与memcache兼容。hash_strategy=与php memcache模块一致。这意味着您可以使用php memcache模块将值存储到memcache集群中,之后NGINX可以在集群中找到该值并从那里读取。

    此参数必须在上游定义内。它打开一致性哈希上游模块,并定义必须进行哈希的字符串,以便在哈希环上找到正确的后端。例如,您可以这样做:

    upstream somestream {
      consistent_hash $request_uri;
      server 10.50.1.3:11211;
      server 10.50.1.4:11211;
      server 10.50.1.5:11211;
    }
    
    ...
    
    server {
      listen       80;
      server_name  localhost;
    
      location / {
        default_type text/html;
        set $memcached_key $request_uri;
        memcached_pass somestream;
        error_page      500 404 405 = @fallback;
      }
    
      location @fallback {
        root /srv/www/whatever;
        fastcgi_intercept_errors on;
        error_page 404 = @404;
    
        set $script $uri;
        set $path_info "";
    
        include /usr/local/nginx/conf/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /srv/www/whatever/test.php;
        fastcgi_param SCRIPT_NAME $script;
        fastcgi_param REQUEST_URI $uri;
        fastcgi_pass   127.0.0.1:9000;
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    本示例使用三个后端服务器。在初始化时,NGINX将创建一个哈希环,其中包含每个服务器(160*weight)次,其方式与hash_strategy=consistent的php memcache模块相同。基于$request_uri的散列,它将决定必须使用哪个后端服务器。现在是测试。上面示例中的php脚本可能如下所示:

    $memcache = new Memcache;
    
    $memcache->addServer('10.50.1.3', 11211);
    $memcache->addServer('10.50.1.4', 11211);
    $memcache->addServer('10.50.1.4', 11211);
    
    $memcache->set($_SERVER["REQUEST_URI"], $_SERVER["REQUEST_URI"] . "from memcache");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Important to know

    我使用PHP memcache模块版本1.2.8进行了测试。该版本中的模块似乎有一个错误,如果hash_strategy设置为consistent_hash,则会导致它完全忽略权重。NGINX一致性哈希上游知道权重参数,但如果您将其与memcache模块1.2.8一起使用,则不应触及任何后端服务器的权重。

    我用NGINX 0.7.61和0.6.34测试了该模块,其他版本没有保证

    Bugs/Feedback

    If you find any bugs, please email me and I will try to help.

    I would appreciate every kind of feedback or problem reports.

    Mail: mauro.stettler(A.T)gmail.com

    Download

    On github I have to branches “master” and “dns”. The reason for this is that if you want to use DNS entries on the PHP side, instead of IPs, you will need to apply a patch to the NGINX to make this work. So if your PHP does not use DNS names to connect to memcache, its nicer to download the “master” branch, because this is a clean module. If your PHP uses DNS names, you have to download the “dns” branch, which includes a patch for NGINX.

  • 相关阅读:
    趣学算法:贪心算法
    #{}和${}的区别
    什么是REST API
    Java版本+企业电子招投标系统源代码+支持二开+招投标系统+中小型企业采购供应商招投标平台
    使用 Windows 20 年后我如何切换到 Ubuntu(2022 年指南)
    踩坑日记 《正确的使用Vuex》基于 uniapp Vue3 setup 语法糖 vuex4 项目 太多坑了要吐了
    人工神经网络教学视频第三版,人工神经网络教程
    基于亚马逊云科技打造的游戏AIGC专业版,创梦天地快速上线AI生图服务
    mysql—各种函数的使用
    VUE3写后台管理(3)
  • 原文地址:https://blog.csdn.net/liuyuzhu111/article/details/126832618