溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Nginx與Lua灰度發(fā)布的實(shí)現(xiàn)

發(fā)布時(shí)間:2020-09-14 07:46:07 來源:腳本之家 閱讀:162 作者:思過崖 欄目:服務(wù)器

安裝 memcached

yum install -y memcached
#啟動(dòng)memcached服務(wù),端口11211,以nobody身份啟動(dòng),-d后臺(tái)模式運(yùn)行
memcached -p11211 -u nobody -d
#將自己的IP設(shè)為灰度測(cè)試的IP
telnet 127.0.0.1 11211
set 119.32.216.122 0 0 1
1
quit

安裝 memcached for lua

wget https://github.com/openresty/lua-resty-memcached/archive/v0.11.tar.gz
tar -zxvf v0.11.tar.gz
mkdir -p /usr/local/share/lua/5.1
cp -r lua-resty-memcached-0.11/lib/resty /usr/local/share/lua/5.1

創(chuàng)建 test.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)
  --ngx.say("value key: ",res,clientIP)
  if err then
    ngx.say("failed to get clientIP",err)
    return
  end
  if res == "1" then
    ngx.exec("@server_test")
    return
  end
  ngx.exec("@server_product")

nginx配置

  server {
    listen 8867;
    server_name www.siguoya.name;
    root /usr/local/nginx/1.12.1/html;
    location / {
      content_by_lua_file /etc/nginx/test.lua;
    }
    location /hello {
      default_type text/plain;
      content_by_lua 'ngx.say("hello lua")';
    }
    location /ua {
      default_type text/plain;
      content_by_lua '
        clientIP=ngx.req.get_headers()["USER-AGENT"]
        ngx.say("IP:",clientIP)
        ';
    }
    location @server_product {
      default_type text/plain;
      return 200 'product';
    }
    location @server_test {
      default_type text/plain;
      return 200 'test';
    }
  }

由于我們將 119.32.216.122 這個(gè)IP添加進(jìn)了memcached中,因此訪問 http://www.siguoya.name:8867/ 的時(shí)候,看到的內(nèi)容就是 test

而開啟全局代理之后,發(fā)現(xiàn)看到的內(nèi)容就是 product,這樣就實(shí)現(xiàn)了針對(duì)部分用戶進(jìn)行灰度測(cè)試的效果

到此這篇關(guān)于Nginx與Lua灰度發(fā)布的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Nginx與Lua灰度發(fā)布內(nèi)容請(qǐng)搜索億速云以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持億速云!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI