国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > Nginx > 正文

Nginx 實(shí)現(xiàn)灰度發(fā)布的三種方法總結(jié)

2024-08-30 12:28:57
字體:
供稿:網(wǎng)友

Nginx 實(shí)現(xiàn)灰度發(fā)布的三種方法總結(jié)

灰度發(fā)布的主要原理是訪問路由的控制,重點(diǎn)是保證每次訪問的是同一個節(jié)點(diǎn)。

方式一:通過調(diào)節(jié)負(fù)載均衡權(quán)重

        負(fù)載均衡 建立在現(xiàn)有網(wǎng)絡(luò)結(jié)構(gòu)之上,它提供了一種廉價有效透明的方法擴(kuò)展網(wǎng)絡(luò)設(shè)備和服務(wù)器的帶寬、增加吞吐量、加強(qiáng)網(wǎng)絡(luò)數(shù)據(jù)處理能力、提高網(wǎng)絡(luò)的靈活性和可用性。

        負(fù)載均衡,英文名稱為Load Balance,其意思就是分?jǐn)偟蕉鄠€操作單元上進(jìn)行執(zhí)行,例如Web服務(wù)器、FTP服務(wù)器、企業(yè)關(guān)鍵應(yīng)用服務(wù)器和其它關(guān)鍵任務(wù)服務(wù)器等,從而共同完成工作任務(wù)。

Nginx,實(shí)現(xiàn)灰度發(fā)布,實(shí)現(xiàn)灰度發(fā)布的幾種方法

簡單配置如下:

http {   upstream cluster {     ip_hash; #如果你的系統(tǒng)中沒有使用第三方緩存管理工具 ,建議使用此方式    server 192.168.1.210:80 weight=5;     server 192.168.1.211:80 weight=3;     server 192.168.1.212:80 weight=1;   }     server {     listen 80;    location / {     proxy_next_upstream   error timeout;  proxy_redirect     off;  proxy_set_header    Host $host;  #proxy_set_header    X-Real-IP $remote_addr;  proxy_set_header    X-Real-IP $http_x_forwarded_for;  proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;  client_max_body_size  100m;  client_body_buffer_size 256k;  proxy_connect_timeout  180;  proxy_send_timeout   180;  proxy_read_timeout   180;  proxy_buffer_size    8k;  proxy_buffers      8 64k;  proxy_busy_buffers_size 128k;  proxy_temp_file_write_size 128k;  proxy_pass http://cluster;     }   } } 

這種方式灰度發(fā)布通過weight來實(shí)現(xiàn),但是這種方式只適合修改節(jié)點(diǎn)的行為,而且要求應(yīng)用都是一模一樣的,其實(shí)質(zhì)作用是,節(jié)點(diǎn)增加或刪除之后,對負(fù)載能力的調(diào)節(jié),最終目的是為了讓流量最終保持均衡。

方式二.使用nginx+lua實(shí)現(xiàn)web項目的灰度發(fā)布

location / { content_by_lua '      myIP = ngx.req.get_headers()["X-Real-IP"]      if myIP == nil then        myIP = ngx.req.get_headers()["x_forwarded_for"]      end      if myIP == nil then        myIP = ngx.var.remote_addr      end      if myIP == "公司出口IP" then        ngx.exec("@client")      else        ngx.exec("@client_test")      end    ';} location @client{  proxy_next_upstream   error timeout;  proxy_redirect     off;  proxy_set_header    Host $host;  #proxy_set_header    X-Real-IP $remote_addr;  proxy_set_header    X-Real-IP $http_x_forwarded_for;  proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;  client_max_body_size  100m;  client_body_buffer_size 256k;  proxy_connect_timeout  180;  proxy_send_timeout   180;  proxy_read_timeout   180;  proxy_buffer_size    8k;  proxy_buffers      8 64k;  proxy_busy_buffers_size 128k;  proxy_temp_file_write_size 128k;  proxy_pass http://client;}location @client_test{  proxy_next_upstream   error timeout;  proxy_redirect     off;  proxy_set_header    Host $host;  #proxy_set_header    X-Real-IP $remote_addr;  proxy_set_header    X-Real-IP $http_x_forwarded_for;  proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;  client_max_body_size  100m;  client_body_buffer_size 256k;  proxy_connect_timeout  180;  proxy_send_timeout   180;  proxy_read_timeout   180;  proxy_buffer_size    8k;  proxy_buffers      8 64k;  proxy_busy_buffers_size 128k;  proxy_temp_file_write_size 128k;  proxy_pass http://client_test;} 

由于使用了nginx+lua模塊,這種方式適合很多場景,非常強(qiáng)大,但是問題是你可能需要學(xué)習(xí)很多l(xiāng)ua的語法。

 方式三.使用http頭信息判斷+權(quán)重(灰度值)

http請求傳輸過程中,會自動帶上User-Agent,Host,Referer,Cookie等信息。我們只需要判斷ip地址段,用戶代理,Cookie中的信息等。我們這里以Cookie為例。

當(dāng)然,這里需要解決兩個問題:

①首次訪問靜態(tài)頁面可能不會產(chǎn)生cookie

②我們需要通過代碼動態(tài)設(shè)置路由

③通過weight控制灰度值

我們可以通過一個例子來解決上述中的②與③的問題

upstream tts_V6 {    server 192.168.3.81:5280 max_fails=1 fail_timeout=60;}upstream tts_V7 {    server 192.168.3.81:5380 max_fails=1 fail_timeout=60;}upstream default {  #通過upstream default + weight節(jié)點(diǎn)控制權(quán)重    server 192.168.3.81:5280 max_fails=1 fail_timeout=60 weight=5;    server 192.168.3.81:5380 max_fails=1 fail_timeout=60 weight=1;}server {    listen 80;    server_name test.taotaosou.com;    access_log logs/test.taotaosou.com.log main buffer=32k;    #match cookie    set $group "default";    if ($http_cookie ~* "tts_version_id=tts1"){ #動態(tài)控制路由        set $group tts_V6;    }    if ($http_cookie ~* "tts_version_id=tts2"){        set $group tts_V7;    }    location / {                    proxy_pass http://$group;        proxy_set_header  Host       $host;        proxy_set_header  X-Real-IP    $remote_addr;        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;        index index.html index.htm;    } }

對于問題①,我們可以在index頁面通過script來訪問動態(tài)頁面:

<script src="https://test.taotaosou.com/cookieinfo.php" /><script>

此外,我們還要在cookieinfo.php中判斷和生成cookie

<?phpif(!session_id()){ session_start();}if(!isset($_COOKIE["tts_version_id"])){ $cookieValue = $_SERVER['SERVER_PORT']==5280?"tts1":"tts2"; setcookie("tts_version_id", $cookieValue, time()+3600, "/");}?>

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 陈巴尔虎旗| 沐川县| 玉山县| 武宣县| 滁州市| 若尔盖县| 永春县| 高台县| 北海市| 介休市| 宁国市| 余干县| 呼和浩特市| 安平县| 金溪县| 衡阳县| 谷城县| 漯河市| 綦江县| 兴和县| 麻栗坡县| 龙井市| 吕梁市| 尼木县| 前郭尔| 连城县| 遂溪县| 大理市| 民县| 潢川县| 江津市| 肥东县| 沁阳市| 绍兴县| 宜丰县| 合江县| 石林| 呼伦贝尔市| 新安县| 宜川县| 尚志市|