灰度发布,简单来说,就是根据各种条件,让一部分用户使用旧版本,另一部分用户使用新版本。百度百科中解释:灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式。AB test就

执行过程:
1、当用户请求到达前端web(代理)服务器O
2、Lua获取客户端IP地址,去查询Redis中是否有该键值,如果有返回值执行@clien2,否则执行@client1。
3、Location @client2把请求转发给预发布服务器,location @client1把请求转发给生产服务器,服务器返回结果,整个过程完成。
Openresty部分配置如下:
upstream client1{
server 192.168.0.225:85; #模拟生产服务器
}
upstream client2{
server 192.168.0.225:86; #模拟预发布服务器
}
lua_package_path "/usr/local/openresty/lualib/lua-resty-redis/lib/?.lua;;";
server {
listen 90;
server_name 192.168.0.225;
proxy_ignore_client_abort on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
location / {
content_by_lua_file /usr/local/openresty/luasrc/huidu.lua;
}
location @client1{
proxy_pass http://client1;
}
location @client2{
proxy_pass http://client2;
}
error_log /data/wwwlogs/lua_err.log debug;
}
Lua脚本内容如下:
local redis = require "resty.redis"
local cache = redis.new()
cache:set_timeout(60000)
local ok, err = cache.connect(cache, '127.0.0.1', 6379)
if not ok then
ngx.say("failed to connect:", err)
return
end
local red, err = cache:auth("foobared")
--配置Redis auth 密码为foobared
if not red then
ngx.say("failed to authenticate: ", err)
return
end
local local_ip = ngx.req.get_headers()["X-Real-IP"]
if local_ip == nil then
local_ip = ngx.req.get_headers()["x_forwarded_for"]
end
if local_ip == nil then
local_ip = ngx.var.remote_addr
end
--ngx.say("local_ip is : ", local_ip)
--ngx.exec前不能放ngx.say
local intercept = cache:get(local_ip)
if intercept == local_ip then
ngx.exec("@client2")
return
end
ngx.exec("@client1")
local ok, err = cache:close()
if not ok then
ngx.say("failed to close:", err)
return
end
验证:
url:http://192.168.0.225:90 (模拟生产环境)
客户端IP:192
1、访问 http://192.168.0.225:90

2、 在Redis存入客户端IP(亦指定IP访问预发布环境)
[root@infvie luasrc]# redis-cli
127.0.0.1:6379> auth foobared
OK
127.0.0.1:6379> set 192.168.0.142 192.168.0.142
OK
127.0.0.1:6379> keys *
1) "192.168.0.142"
然后刷新浏览器 ,再次访问:

3、在Redis中删除客户端IP:
127.0.0.1:6379> del 192.168.0.142
(integer) 1
127.0.0.1:6379> keys *
(empty list or set)
然后刷新浏览器 ,再次访问:

咋感觉大家所理解的灰度发布都是基于ip的,现在想基于requestParam和URL做灰度转发。发现大家都没这样的需求呀
IP相对简单,但你可以找属于自己的业务的最优解
请问博主,用的是哪个博客程序啊,感觉这样界面样式、字体都特别好看! 希望得到回复~