Openresty+Lua+Redis灰度发布

转载https://www.cnblogs.com/Eivll0m/p/6774622.html 

https://blog.csdn.net/jiangeeq/article/details/80534989

灰度发布,简单来说,就是根据各种条件,让一部分用户使用旧版本,另一部分用户使用新版本。百度百科中解释:灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式。AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B上面 来。灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度。上述描述的灰度方案A和B需要等量的服务器,这里我们所做的灰度发布稍作改变:用1-2台机器作为B,B测试成功再部署A。用于WEB系统新代码的测试发布,让一部分(IP)用户访问新版本,一部分用户仍然访问正常版本,原理如图:

执行过程:
1、当用户请求到达前端web(代理)服务器Openresty,内嵌的lua模块解析Nginx配置文件中的lua脚本代码;
2、Lua获取客户端IP地址,去查询Redis中是否有该键值,如果有返回值执行@clien2,否则执行@client1。
3、Location @client2把请求转发给预发布服务器,location @client1把请求转发给生产服务器,服务器返回结果,整个过程完成。

Openresty部分配置如下:

复制代码
upstream client1 { 
        server 127.0.0.1:8080;  #模拟生产服务器
    }
upstream client2 {
        server 127.0.0.1:8090;  #模拟预发布服务器
    }

server {
        listen       80;
        server_name  localhost;
        
        location ^~ /test {
            content_by_lua_file /app/ngx_openresty/nginx/conf/huidu.lua
        }
        
        location @client1{
                proxy_pass http://client1;
        }
        location @client2{
                proxy_pass http://client2;
        }
     }
复制代码

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")
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)

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.116.145/test/n.jpg (模拟生产环境)
客户端IP:192.168.116.1(模拟公司办公网IP)

1、访问http://192.168.116.145/test/n.jpg
返回的结果是生产服务器的。

在Redis存入客户端IP:

继续访问:
请求到的是预发布服务器返回的结果。

在Redis中删除客户端IP:

然后刷新浏览器:
返回生产服务器的结果。

 

 

 

灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式。AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B上面来。

灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度。

灰度发布常见一般有三种方式:

  • Nginx+LUA方式

  • 根据Cookie实现灰度发布

  • 根据来路IP实现灰度发布

本文主要将讲解根据Cookie和来路IP这两种方式实现简单的灰度发布,Nginx+LUA这种方式涉及内容太多就不再本文展开了。

 

A/B测试流程


 




Nginx根据Cookie实现灰度发布
 

根据Cookie查询Cookie键为version的值,如果该Cookie值为V1则转发到hilinux_01,为V2则转发到hilinux_02。Cookie值都不匹配的情况下默认走hilinux_01所对应的服务器。

两台服务器分别定义为:

  1.  
    hilinux_01  192.168.1.100:8080
  2.  
    hilinux_02  192.168.1.200:8080
  • 用if指令实现

  1.  
    upstream hilinux_01 {
  2.  
       server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
  3.  
    }
  4.  
     
  5.  
    upstream hilinux_02 {
  6.  
       server 192.168.1.200:8080 max_fails=1 fail_timeout=60;
  7.  
    }
  8.  
     
  9.  
    upstream default {
  10.  
       server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
  11.  
    }
  12.  
     
  13.  
    server {
  14.  
     listen 80;
  15.  
     server_name  www.hi-linux.com;
  16.  
     access_log  logs/www.hi-linux.com.log  main;
  17.  
     
  18.  
     #match cookie
  19.  
     set $group "default";
  20.  
       if ($http_cookie ~* "version=V1"){
  21.  
           set $group hilinux_01;
  22.  
       }
  23.  
     
  24.  
       if ($http_cookie ~* "version=V2"){
  25.  
           set $group hilinux_02;
  26.  
       }
  27.  
     
  28.  
     location / {                      
  29.  
       proxy_pass http://$group;
  30.  
       proxy_set_header   Host             $host;
  31.  
       proxy_set_header   X-Real-IP        $remote_addr;
  32.  
       proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
  33.  
       index  index.html index.htm;
  34.  
     }
  35.  
    }
  • 用map指令实现

在Nginx里面配置一个映射,$COOKIE_version可以解析出Cookie里面的version字段。$group是一个变量,{}里面是映射规则。

如果一个version为V1的用户来访问,$group就等于hilinux_01。在server里面使用就会代理到http://hilinux_01上。version为V2的用户来访问,$group就等于hilinux_02。在server里面使用就会代理到http://hilinux_02上。Cookie值都不匹配的情况下默认走hilinux_01所对应的服务器。

  1.  
    upstream hilinux_01 {
  2.  
       server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
  3.  
    }
  4.  
     
  5.  
    upstream hilinux_02 {
  6.  
       server 192.168.1.200:8080 max_fails=1 fail_timeout=60;
  7.  
    }
  8.  
     
  9.  
    upstream default {
  10.  
       server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
  11.  
    }
  12.  
     
  13.  
    map $COOKIE_version $group {
  14.  
    ~*V1$ hilinux_01;
  15.  
    ~*V2$ hilinux_02;
  16.  
    default default;
  17.  
    }
  18.  
     
  19.  
    server {
  20.  
     listen 80;
  21.  
     server_name  www.hi-linux.com;
  22.  
     access_log  logs/www.hi-linux.com.log  main;
  23.  
     
  24.  
     location / {                      
  25.  
       proxy_pass http://$group;
  26.  
       proxy_set_header   Host             $host;
  27.  
       proxy_set_header   X-Real-IP        $remote_addr;
  28.  
       proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
  29.  
       index  index.html index.htm;
  30.  
     }
  31.  
    }

Nginx根据来路IP实现灰度发布

如果是内部IP,则反向代理到hilinux_02(预发布环境);如果不是则反向代理到hilinux_01(生产环境)。

  1.  
    upstream hilinux_01 {
  2.  
       server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
  3.  
    }
  4.  
     
  5.  
    upstream hilinux_02 {
  6.  
       server 192.168.1.200:8080 max_fails=1 fail_timeout=60;
  7.  
    }
  8.  
     
  9.  
    upstream default {
  10.  
       server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
  11.  
    }
  12.  
     
  13.  
    server {
  14.  
     listen 80;
  15.  
     server_name  www.hi-linux.com;
  16.  
     access_log  logs/www.hi-linux.com.log  main;
  17.  
     
  18.  
     set $group default;
  19.  
     if ($remote_addr ~ "211.118.119.11") {
  20.  
         set $group hilinux_02;
  21.  
     }
  22.  
     
  23.  
    location / {                      
  24.  
       proxy_pass http://$group;
  25.  
       proxy_set_header   Host             $host;
  26.  
       proxy_set_header   X-Real-IP        $remote_addr;
  27.  
       proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
  28.  
       index  index.html index.htm;
  29.  
     }
  30.  
    }

如果你只有单台服务器,可以根据不同的IP设置不同的网站根目录来达到相同的目的。

  1.  
    server {
  2.  
     listen 80;
  3.  
     server_name  www.hi-linux.com;
  4.  
     access_log  logs/www.hi-linux.com.log  main;
  5.  
     
  6.  
     set $rootdir "/var/www/html";
  7.  
       if ($remote_addr ~ "211.118.119.11") {
  8.  
          set $rootdir "/var/www/test";
  9.  
       }
  10.  
     
  11.  
       location / {
  12.  
         root $rootdir;
  13.  
       }
  14.  
    }

到此最基本的实现灰度发布方法就讲解完了,如果要做更细粒度灰度发布可参考ABTestingGateway项目。

ABTestingGateway是新浪开源的一个动态路由系统。ABTestingGateway是一个可以动态设置分流策略的灰度发布系统,工作在7层,基于nginx和ngx-lua开发,使用redis作为分流策略数据库,可以实现动态调度功能。

ABTestingGateway:https://github.com/CNSRE/ABTestingGateway

参考文档

http://www.google.com
http://www.jianshu.com/p/88f206f48278
http://blog.chinaunix.net/uid-531464-id-4140473.html

posted @ 2020-11-20 10:40  技术颜良  阅读(731)  评论(0编辑  收藏  举报