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;
  }
}

本示例使用三个后端服务器。在初始化时,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");

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.

你可能感兴趣的:(测试开发专栏,哈希算法,php,memcached)