灰度發(fā)布-Openresty+lua實(shí)現(xiàn)動(dòng)態(tài)upstream

動(dòng)態(tài)的負(fù)載均衡控制类垫;
平滑上下線服務(wù)培廓,升級(jí)服務(wù);
高可能保證–自動(dòng)踢離線服務(wù)下線荣堰;
具體思路
利用lua中 "lua_shared_dict" 指令開辟一個(gè)共享內(nèi)存空間;
通過API動(dòng)態(tài)根據(jù)key值&參數(shù)修改 upstream (這里使用 host 作為key);
利用 proxy_pass 可使用變量特性及l(fā)ua指令 "set_by_lua" 動(dòng)態(tài)修改當(dāng)前 upstream 變量即可床未;
配置:

worker_processes  1;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
 
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
    sendfile        on;
 
    keepalive_timeout  65;
          
    #lua_shared_dict upstreams 1m;  # 聲明一個(gè)ngx多進(jìn)程全局共享內(nèi)存區(qū)域,_G 作為基于shm的Lua字典的存儲(chǔ)空間ngx.shared.<name>   
    lua_shared_dict _ups_zone 64m; # 定義upstream共享內(nèi)存空間
 
 
        
    upstream backend {                        # 配置后端服務(wù)器組
                server 127.0.0.1:18084;
                server 127.0.0.1:18083;
    }
 
    server {
        listen       8000;
        server_name  localhost;
 
        access_log  logs/80.access.log  main;
        error_log   logs/80.error.log error;
 
 
#更新API接口
        location = /ups_update {  
            content_by_lua '  
                local ups = ngx.req.get_uri_args()["ups"]  
                if ups == nil then 
                    ngx.say("usage: /ups_update?ups=x.x.x.x")  
                    return 
                end  
                local host = ngx.var.http_host  
                local ups_from = ngx.shared._ups_zone:get(host);  
                ngx.log(ngx.WARN, host, " update upstream from ", ups_from, " to ", ups)  
                ngx.shared._ups_zone:set(host, ups);  
                ngx.say(host, " update upstream from ", ups_from, " to ", ups)  
            ';  
        }
 
location / {  
            # 動(dòng)態(tài)設(shè)置當(dāng)前 upstream, 未設(shè)置則使用默認(rèn) upstream  
            set_by_lua $cur_ups '  
                local ups = ngx.shared._ups_zone:get(ngx.var.http_host)  
                if ups ~= nil then 
                    ngx.log(ngx.ERR, "get [", ups, "] from ngx.shared._ups_zone")  
                    return ups  
                end  
                --ngx.log(ngx.INFO, "use default upstream");  
                return "qq_backend";  
            '  
            proxy_next_upstream off;  
            proxy_set_header    Host $host;  
            proxy_http_version  1.1;  
            proxy_set_header    Connection  "";  
            proxy_pass $scheme://$cur_ups;  
        }
 
 
}
 

}

測(cè)試切換upstream的backend效果:
1振坚、默認(rèn)情況【請(qǐng)求4次】:

root@ubuntu:~# curl  127.0.0.1:8000
83 server
root@ubuntu:~# curl  127.0.0.1:8000
84 server 
root@ubuntu:~# curl  127.0.0.1:8000
83 server
root@ubuntu:~# curl  127.0.0.1:8000
84 server 

切換upstream

root@ubuntu:~# curl  127.0.0.1:8000/ups_update?ups=127.0.0.1:18083
127.0.0.1:8000 update upstream from 127.0.0.1:18083 to 127.0.0.1:18083
root@ubuntu:~# curl  127.0.0.1:8000
83 server
root@ubuntu:~# curl  127.0.0.1:8000
83 server
root@ubuntu:~# curl  127.0.0.1:8000
83 server
root@ubuntu:~# curl  127.0.0.1:8000
83 server

切換upstream:

worker_processes  1;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
 
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
    sendfile        on;
 
    keepalive_timeout  65;
          
    lua_shared_dict upstreams 1m;  # 聲明一個(gè)ngx多進(jìn)程全局共享內(nèi)存區(qū)域,_G 作為基于shm的Lua字典的存儲(chǔ)空間ngx.shared.<name>   
    upstream default_upstream {                        # 配置后端服務(wù)器組
                server 127.0.0.1:18081;
                server 127.0.0.1:18082;
    }
         
    upstream lua_upstream {                        # 配置后端服務(wù)器組
                server 127.0.0.1:18084;
                server 127.0.0.1:18083;
    }
 
    server {
        listen       8000;
        server_name  localhost;
 
        access_log  logs/80.access.log  main;
        error_log   logs/80.error.log error;
 
        location = /_switch_upstream {
             content_by_lua_block{
                    local ups = ngx.req.get_uri_args()["upstream"]
                    if ups == nil or ups == "" then
                        ngx.say("upstream is nil 1")
                        return nil
                    end
                    local host = ngx.var.http_host
                    local upstreams = ngx.shared.upstreams
                    local ups_src = upstreams:get(host)
                    ngx.say("Current upstream is :",ups_src)
                    ngx.log(ngx.WARN, host, " change upstream from ", ups_src, " to ", ups)
                    local succ, err, forcible = upstreams:set(host,  ups)
                    ngx.say(host, " change upstream from ", ups_src, " to ", ups)
            }
        }
         
        location / {
            set_by_lua_block $my_upstream {
                local ups = ngx.shared.upstreams:get(ngx.var.http_host)
                if ups ~= nil then
                    ngx.log(ngx.ERR, "get [", ups,"] from ngx.shared")
                    return ups
                end
                return "default_upstream"
            }
 
            proxy_next_upstream off;
            proxy_set_header    X-Real-IP           $remote_addr;
            proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
            proxy_set_header    Host                $host;
            proxy_http_version  1.1;
            proxy_set_header    Connection  "";
            proxy_pass          http://$my_upstream ;
        }
    }   
 
   
}

切換:

curl 127.0.0.1:8000/_switch_upstream?upstream=lua_upstream
curl 127.0.0.1:8000/_switch_upstream?upstream=default_upstream

這里用的還是靜態(tài)文件斋扰,改成tomcat服務(wù)之后也是可以進(jìn)行代理的切換渡八。要是想持久化的話 ,得用到redis传货。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末屎鳍,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子问裕,更是在濱河造成了極大的恐慌逮壁,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,284評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件粮宛,死亡現(xiàn)場(chǎng)離奇詭異窥淆,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)巍杈,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門忧饭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人筷畦,你說我怎么就攤上這事词裤。” “怎么了鳖宾?”我有些...
    開封第一講書人閱讀 164,614評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵吼砂,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我鼎文,道長(zhǎng)渔肩,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,671評(píng)論 1 293
  • 正文 為了忘掉前任漂问,我火速辦了婚禮赖瞒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蚤假。我一直安慰自己栏饮,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評(píng)論 6 392
  • 文/花漫 我一把揭開白布磷仰。 她就那樣靜靜地躺著袍嬉,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上伺通,一...
    開封第一講書人閱讀 51,562評(píng)論 1 305
  • 那天箍土,我揣著相機(jī)與錄音,去河邊找鬼罐监。 笑死吴藻,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的弓柱。 我是一名探鬼主播沟堡,決...
    沈念sama閱讀 40,309評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼矢空!你這毒婦竟也來了航罗?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,223評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤屁药,失蹤者是張志新(化名)和其女友劉穎粥血,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體酿箭,經(jīng)...
    沈念sama閱讀 45,668評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡复亏,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了七问。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蜓耻。...
    茶點(diǎn)故事閱讀 39,981評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖械巡,靈堂內(nèi)的尸體忽然破棺而出刹淌,到底是詐尸還是另有隱情,我是刑警寧澤讥耗,帶...
    沈念sama閱讀 35,705評(píng)論 5 347
  • 正文 年R本政府宣布有勾,位于F島的核電站,受9級(jí)特大地震影響古程,放射性物質(zhì)發(fā)生泄漏蔼卡。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評(píng)論 3 330
  • 文/蒙蒙 一挣磨、第九天 我趴在偏房一處隱蔽的房頂上張望雇逞。 院中可真熱鬧,春花似錦茁裙、人聲如沸塘砸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽掉蔬。三九已至廊宪,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間女轿,已是汗流浹背箭启。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蛉迹,地道東北人傅寡。 一個(gè)月前我還...
    沈念sama閱讀 48,146評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像婿禽,于是被迫代替她去往敵國(guó)和親赏僧。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容