<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          基于 Nginx+lua+Memcache 實(shí)現(xiàn)灰度發(fā)布

          共 9504字,需瀏覽 20分鐘

           ·

          2021-03-08 06:09

          一、灰度發(fā)布原理說(shuō)明

          灰度發(fā)布在百度百科中解釋?zhuān)?/strong>
          灰度發(fā)布是指在黑與白之間,能夠平滑過(guò)渡的一種發(fā)布方式。AB test就是一種灰度發(fā)布方式,讓一部分用戶(hù)繼續(xù)用A,一部分用戶(hù)開(kāi)始用B,如果用戶(hù)對(duì)B沒(méi)有什么反對(duì)意見(jiàn),那么逐步擴(kuò)大范圍,把所有用戶(hù)都遷移到B上面 來(lái)?;叶劝l(fā)布可以保證整體系統(tǒng)的穩(wěn)定,在初始灰度的時(shí)候就可以發(fā)現(xiàn)、調(diào)整問(wèn)題,以保證其影響度。
          這里的用于WEB系統(tǒng)新代碼的測(cè)試發(fā)布,讓一部分(IP)用戶(hù)訪(fǎng)問(wèn)新版本,一部分用戶(hù)仍然訪(fǎng)問(wèn)正常版本,其原理如圖:

           

          執(zhí)行過(guò)程:

          • 當(dāng)用戶(hù)請(qǐng)求到達(dá)前端代理服務(wù)Nginx,內(nèi)嵌的lua模塊解析Nginx配置文件中的lua腳本代碼;

          • Lua變量獲得客戶(hù)端IP地址,去查詢(xún)memcached緩存內(nèi)是否有該鍵值,如果有返回值執(zhí)行@client_test,否則執(zhí)行@client。

          • Location @client_test把請(qǐng)求轉(zhuǎn)發(fā)給部署了new版代碼的服務(wù)器,location @client把請(qǐng)求轉(zhuǎn)發(fā)給部署了normal版代碼的服務(wù)器,服務(wù)器返回結(jié)果。整個(gè)過(guò)程完成。

          下面把安裝配置過(guò)程詳細(xì)說(shuō)明。

           

          二、安裝配置過(guò)程詳解

          1、安裝nginx

          安裝依賴(lài)包

          yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers make pcre-devel

          yum -y install gd gd2 gd-devel gd2-devel lua lua-devel

          yum –y install memcached


          下載lua模塊、lua-memcache操作庫(kù)文件和nginx包

          wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz
          wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.5.tar.gz
          wget https://github.com/agentzh/lua-resty-memcached/archive/v0.11.tar.gz
          wget http://nginx.org/download/nginx-1.4.2.tar.gz

          #解壓編譯安裝
          tar xvf nginx-1.4.2.tar.gz
          cd nginx-1.4.2/
          ./configure \
          --prefix=/soft/nginx/ \
          --with-http_gzip_static_module \
          --add-module=/root/ngx_devel_kit-0.2.18/ \
          --add-module=/root/lua-nginx-module-0.8.5/

          make&&make install

          拷貝lua的memcached操作庫(kù)文件

          tar xvf v0.11.tar.gz

          cp -r lua-resty-memcached-0.11/lib/resty/ /usr/lib64/lua/5.1/

          配置nginx

          #vim /soft/nginx/conf/nginx.conf
          worker_processes  1;
          events {
              worker_connections  1024;
          }
          http {
              include       mime.types;
              default_type  application/octet-stream;
              sendfile        on;
              keepalive_timeout  65;

              proxy_next_upstream     error timeout;
              proxy_redirect          off;
              proxy_set_header        Host $host;
              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;

               upstream client {
                  server   192.168.200.29:80;
              }
              upstream client_test {
                 server   192.168.200.29:81;
              }

              server {
                listen       80;
                server_name  localhost;
                 location / {
                 content_by_lua '
                      clientIP = ngx.req.get_headers()["X-Real-IP"]
                      if clientIP == nil then
                          clientIP = ngx.req.get_headers()["x_forwarded_for"]
                      end
                      if clientIP == nil then
                          clientIP = ngx.var.remote_addr
                      end
                          local memcached = require "resty.memcached"
                          local memc, err = memcached:new()
                          if not memc then
                              ngx.say("failed to instantiate memc: ", err)
                              return
                          end
                          local ok, err = memc:connect("127.0.0.1", 11211)
                          if not ok then
                              ngx.say("failed to connect: ", err)
                              return
                          end
                          local res, flags, err = memc:get(clientIP)
                          if err then
                              ngx.say("failed to get clientIP ", err)
                              return
                          end
                          if res == "1" then
                              ngx.exec("@client_test")
                              return
                          end
                           ngx.exec("@client")             
                         '
          ;
                 }
                 location @client{
                     proxy_pass http://client;
                 }
                location @client_test{
                     proxy_pass http://client_test;
                 }

              location /hello {
                 default_type 'text/plain';
                content_by_lua 'ngx.say("hello, lua")';
              }

              location = /50x.html {
                  root   html;
              }
             }
          }

          檢測(cè)配置文件。

          #/soft/nginx/sbin/nginx -t

          nginx: the configuration file /soft/nginx/conf/nginx.conf syntax is ok

          nginx: configuration file /soft/nginx/conf/nginx.conf test is successful

          啟動(dòng)nginx

          /soft/nginx/sbin/nginx

          啟動(dòng)memcached服務(wù)

          memcached -u nobody -m 1024 -c 2048 -p 11211 –d


          三、測(cè)試驗(yàn)證

          測(cè)試lua模塊是否運(yùn)行正常
          訪(fǎng)問(wèn)http://測(cè)試服務(wù)器ip地址/hello。如果顯示:hello,lua 表示安裝成功。
           
          在另一臺(tái)測(cè)試機(jī)(這里是192.168.200.29)設(shè)置兩個(gè)虛擬主機(jī),一個(gè)用80端口是執(zhí)行正常代碼,一個(gè)是81端口執(zhí)行灰度測(cè)試代碼。
          在memcached中以你的客戶(hù)機(jī)IP地址為key,value值為1。這里我的IP是192.168.68.211.
          telnet localhost 11211
          Trying ::1...
          Connected to localhost.
          Escape character is '^]'.
          set 192.168.68.211 0 3600 1
          1
          STORED
          get 192.168.68.211
          VALUE 192.168.68.211 9 1
          1
          END
          quit
          注意:
          set后第一個(gè)值為key值。
          192.168.68.211這是key值是需要灰度測(cè)試的IP地址;
          0 表示一個(gè)跟該key有關(guān)的自定義數(shù)據(jù);
          3600 表示該key值的有效時(shí)間;
          1 表示key所對(duì)應(yīng)的value值的字節(jié)數(shù)。
          下面訪(fǎng)問(wèn)Nginx,效果符合預(yù)期,我的IP已經(jīng)在memcached中存儲(chǔ)值,所以請(qǐng)求轉(zhuǎn)發(fā)給執(zhí)行灰度測(cè)試代碼的主機(jī)。

           

          從memcached刪除我的主機(jī)IP值。
           
          再次請(qǐng)求Nginx,請(qǐng)求轉(zhuǎn)發(fā)給執(zhí)行正常代碼內(nèi)容的主機(jī)。
           
          整個(gè)配置并不復(fù)雜,整個(gè)判斷過(guò)程對(duì)服務(wù)的影響非常小。如果需要使用這個(gè)系統(tǒng)最好自己看看lua腳本。


          來(lái)源:nblogs.com/wenbiao/p/3227998.html

          版權(quán)申明:內(nèi)容來(lái)源網(wǎng)絡(luò),版權(quán)歸原創(chuàng)者所有。除非無(wú)法確認(rèn),我們都會(huì)標(biāo)明作者及出處,如有侵權(quán)煩請(qǐng)告知,我們會(huì)立即刪除并表示歉意。謝謝!





          感謝閱讀



          瀏覽 76
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  麻豆久久 | 传媒-熊猫成人网 | 黄色AV毛片 | 91青青草免费 | 欧美A片在线视频 |