使用openresty實現按照流量百分比控制的灰度分流控制


安裝好以后直接就可以配置實踐了,openresty將lua都集成好了,nginx配置文件無需特殊聲明引入lua file。

1.nginx.conf 添加兩個灰度發布的環境 #grey 灰度環境地址 #prd生產環境地址

http塊操作

    upstream grey {
        server 127.0.0.1:8080;
    }

    upstream prd {
        server 139.162.116.84:8080;
    }

server塊操作,添加對應location

    location @grey{
        proxy_pass http://grey  
    }

   

    location @prd{
        proxy_pass http://prd  
    }
    
    # 在server 里根location指定lua文件
    location / {
        default_type 'text/html';
        content_by_lua_file /root/openresty/nginx/conf/lua_conf/grey.lua;
    }
[root@luka77 lua_conf]# cat penresty/nginx/conf/lua_conf/grey.lua
foreign_env = 'grey'
china_env = 'prd'
--流量比率
abtest_num = 50   
local num = math.random(100);
if (num <= abtest_num) then
   ngx.log(ngx.INFO,'use foreign environment',foreign_env)
   ngx.exec("@"..foreign_env)
else
   ngx.log(ngx.INFO,'use foreign environment',china_env)
   ngx.exec("@"..china_env)
end


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM