nginx一致性hash

consistencehash
https://github.com/replay/ngx_http_consistent_hash->download
https://github.com/replay/ngx_http_consistent_hash/archive/master.zip
[root@localhost soft]# wget https://github.com/replay/ngx_http_consistent_hash/archive/master.zip
[root@localhost soft]# unzip master
[root@localhost soft]# cd ngx_http_consistent_hash-master#readme
[root@localhost ngx_http_consistent_hash-master]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.4.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
configure arguments: --prefix=/usr/local/nginx
[root@localhost soft]# cd nginx-1.4.2
[root@localhost nginx-1.4.2]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@localhost nginx-1.4.2]# ./configure --prefix=/usr/local/nginx --add-module=/root/soft/ngx_http_consistent_hash-master/
[root@localhost nginx-1.4.2]# make &&make install
cd nginx
[root@localhost nginx]# cd sbin
[root@localhost sbin]# ./nginx

  upstream memserver {  
        server localhost:11211;
        server localhost:11212;
        server localhost:11213;
    }
    
     location / {
           # root   html;
           set $memcached_key $uri;
           memcached_pass memserver;  // memserver爲上面的memcache節點的名稱
           error_page 404 /writemem.php;
           index  index.php index.html index.htm;
        }
    [root@localhost nginx]# /usr/local/bin/memcached -u nobody -vv
    [root@localhost nginx]# /usr/local/bin/memcached -u nobody -vv -p 11212
    [root@localhost nginx]# /usr/local/bin/memcached -u nobody -vv -p 11213
    訪問urlhttp://192.168.88.156/use5.html
    <7 get /use5.html
    >7 END
   <7 connection closed.
[root@localhost nginx]# vi conf/nginx.conf
     upstream memserver {  
     consistent_hash $request_uri;#在一臺上訪問
        server localhost:11211;
        server localhost:11212;
        server localhost:11213;
    }
root@localhost nginx]# ./sbin/nginx -s reload
添加多臺服務器
php也要添加和nginx同樣多的服務器
$mem=new memcache();
$mem->addServer('localhost',11211);
$mem->addServer('localhost',11212);
$mem->addServer('localhost',11213);
修改php.ini
vim /usr/local/fastphp/lib/php.ini
memcache.hash_strategy=consistent
pkill -9 php-fpm
/usr/local/fastphp/sbin/php-fpm
大量訪問量優化總體思路
1.減小請求, expires利用瀏覽器緩存減小查詢
2.合併css背景圖片減小mysql查詢
3.利用cdn響應請求
4.最終剩下的,不可避免的請求,--服務集羣+負載均衡來支撐

配置memcache集羣

    upstream memserver {  把用到的memcached節點,聲明在一個組裏
        hash_key $request_uri;  // hash計算時的依據,以uri作依據來hash
        server localhost:11211;
        server localhost:11212;
    }
Location裏

        location / {
           # root   html;
           set $memcached_key $uri;
           memcached_pass memserver;  // memserver爲上面的memcache節點的名稱
           error_page 404 /writemem.php;
           index  index.php index.html index.htm;
        }

在nginx中作集羣與負載均衡,步驟都是同樣的
Upstream {}模塊 把多臺服務器加入到一個組
而後 memcached_pass, fastcgi_pass, proxy_pass ==> upstream組

默認的負載均衡的算法:
是設置計數器,輪流請求N臺服務器.
能夠安裝第3方模式,來利用uri作hash等等.

如http://wiki.nginx.org/NginxHttpUpstreamConsistentHash
這個模塊就是用一致性hash來請求後端結節,而且其算法,與PHP中的memcache模塊的一致性hash算法,兼容.

安裝該模塊後:
Nginx.conf中

    upstream memserver {
        consistent_hash $request_uri;
        server localhost:11211;
        server localhost:11212;
    }

在PHP.ini中,以下配置

memcache.hash_strategy = consistent

這樣: nginx與PHP便可完成對memcached的集羣與負載均衡算法.



php

相關文章
相關標籤/搜索