使用balancer_by_lua_block做應用層負載均衡

首先感謝章義春大神的openresty,解決了web開發的一些痛點並簡化了web開發的複雜度。

需求:

根據url的一個參數,做負載均衡,使得某一個用戶總是被分配到固定的業務服務器上處理,方便後續的業務處理,做緩存或單元化架構部署

假設這個參數爲dvid,一共有兩個業務服務器, 8088端口和8089端口,分別返回hello和world

  1. server{  
  2. listen 8088;  
  3.         location /hello {  
  4.             content_by_lua ’  
  5.             ngx.say(“hello”)  
  6.             ’;  
  7.     }    
  8. }  
  9. server{  
  10. listen 8089;  
  11.         location /hello {  
  12.             content_by_lua ’  
  13.             ngx.say(“world”)  
  14.             ’;  
  15.     }    
  16. }    
    server{
    listen 8088;
            location /hello {
                content_by_lua '
                ngx.say("hello")
                ';
        }  
    }
    server{
    listen 8089;
            location /hello {
                content_by_lua '
                ngx.say("world")
                ';
        }  
    }  

upstream  balancer_by_lua_block的配置:

  1.     upstream backend{    
  2.         server 0.0.0.0;    
  3.         balancer_by_lua_block {     
  4.             local balancer = require “ngx.balancer”      
  5.             local port = {8088, 8089}      
  6.             local backend = “”    
  7.             local dvid = ngx.req.get_uri_args()[“dvid”] or 0  
  8.             ngx.log(ngx.ERR, ”dvid=”, dvid)  
  9.             local hash = (dvid % 2) + 1     
  10.             ngx.log(ngx.ERR, ”hash=”, hash)  
  11.             backend = port[hash]    
  12.             ngx.log(ngx.ERR, ”backend=”, backend)  
  13.             ngx.log(ngx.ERR, ”dvid=“, dvid, ” hash=“, hash, ” up=”, backend)    
  14.             local ok, err = balancer.set_current_peer(“127.0.0.1”, backend)    
  15.             if not ok then    
  16.                 ngx.log(ngx.ERR, “failed to set the current peer: ”, err)    
  17.                 return ngx.exit(500)    
  18.             end    
  19.             ngx.log(ngx.DEBUG, “current peer ”, backend)    
  20.         }     
  21.     }   
    upstream backend{  
        server 0.0.0.0;  
        balancer_by_lua_block {   
            local balancer = require "ngx.balancer"    
            local port = {8088, 8089}    
            local backend = ""  
            local dvid = ngx.req.get_uri_args()["dvid"] or 0
            ngx.log(ngx.ERR, "dvid=", dvid)
            local hash = (dvid % 2) + 1   
            ngx.log(ngx.ERR, "hash=", hash)
            backend = port[hash]  
            ngx.log(ngx.ERR, "backend=", backend)
            ngx.log(ngx.ERR, "dvid=", dvid, " hash=", hash, " up=", backend)  
            local ok, err = balancer.set_current_peer("127.0.0.1", backend)  
            if not ok then  
                ngx.log(ngx.ERR, "failed to set the current peer: ", err)  
                return ngx.exit(500)  
            end  
            ngx.log(ngx.DEBUG, "current peer ", backend)  
        }   
    } 

接收業務請求的server:

  1. server {  
  2.     listen       80;  
  3.     server_name  localhost;  
  4.   
  5.     #charset koi8-r;  
  6.   
  7.     #access_log  logs/host.access.log  main;  
  8.   
  9.     location / {  
  10.         root   html;  
  11.         index  index.html index.htm;  
  12.     }  
  13.     location /hello {  
  14.         proxy_pass http://backend;  
  15.     }     
  16.     error_page   500 502 503 504  /50x.html;  
  17.     location = /50x.html {  
  18.         root   html;  
  19.     }  
  20.   
  21. }  
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location /hello {
            proxy_pass http://backend;
        }   
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
測試:

http://localhost/hello?dvid=1   返回world

http://localhost/hello?dvid=2   返回hello

測試結果OK


openresty安裝:

https://openresty.org/en/download.html

wget https://openresty.org/download/openresty-1.11.2.2.tar.gz

tar zxf openresty-1.11.2.2.tar.gz

cd openresty-1.11.2.2/

./configure –with-luajit&& make && make install


參考:

http://weibo.com/1834459124/DaEmgBhlD?from=singleweibo&mod=recommand_weibo&type=comment#_rnd1479734607640

http://blog.csdn.net/force_eagle/article/details/52224660

http://www.wtoutiao.com/p/177SJDc.html


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章