灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式。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所对应的服务器。

两台服务器分别定义为:

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

upstream hilinux_01 {
   server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
} upstream hilinux_02 {
   server 192.168.1.200:8080 max_fails=1 fail_timeout=60;
} upstream default {
   server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
} server {
 listen 80;
 server_name  www.hi-linux.com;
 access_log  logs/www.hi-linux.com.log  main;  #match cookie
 set $group "default";
   if ($http_cookie ~* "version=V1"){
       set $group hilinux_01;
   }    if ($http_cookie ~* "version=V2"){
       set $group hilinux_02;
   }  location / {                      
   proxy_pass http://$group;
   proxy_set_header   Host             $host;
   proxy_set_header   X-Real-IP        $remote_addr;
   proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
   index  index.html index.htm;
 }
}
  • 用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所对应的服务器。

upstream hilinux_01 {
   server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
} upstream hilinux_02 {
   server 192.168.1.200:8080 max_fails=1 fail_timeout=60;
} upstream default {
   server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
} map $COOKIE_version $group {
~*V1$ hilinux_01;
~*V2$ hilinux_02;
default default;
} server {
 listen 80;
 server_name  www.hi-linux.com;
 access_log  logs/www.hi-linux.com.log  main;  location / {                      
   proxy_pass http://$group;
   proxy_set_header   Host             $host;
   proxy_set_header   X-Real-IP        $remote_addr;
   proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
   index  index.html index.htm;
 }
}

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

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

upstream hilinux_01 {
   server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
} upstream hilinux_02 {
   server 192.168.1.200:8080 max_fails=1 fail_timeout=60;
} upstream default {
   server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
} server {
 listen 80;
 server_name  www.hi-linux.com;
 access_log  logs/www.hi-linux.com.log  main;  set $group default;
 if ($remote_addr ~ "211.118.119.11") {
     set $group hilinux_02;
 } location / {                      
   proxy_pass http://$group;
   proxy_set_header   Host             $host;
   proxy_set_header   X-Real-IP        $remote_addr;
   proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
   index  index.html index.htm;
 }
}

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

server {
 listen 80;
 server_name  www.hi-linux.com;
 access_log  logs/www.hi-linux.com.log  main;  set $rootdir "/var/www/html";
   if ($remote_addr ~ "211.118.119.11") {
      set $rootdir "/var/www/test";
   }    location / {
     root $rootdir;
   }
}

到此最基本的实现灰度发布方法就讲解完了,如果要做更细粒度灰度发布可参考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

 

使用Nginx实现灰度发的更多相关文章

  1. 【原创】大叔经验分享(77)openresty(nginx+lua)发http请求

    openresty(nginx+lua)发http请求 利用location+proxy_pass间接实现 location ^~ /test/http { internal; proxy_pass ...

  2. 使用Nginx实现灰度发布

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

  3. 使用Nginx实现灰度发布(转)

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

  4. nginx 通过rsyslog发日志 rsyslog服务器挂掉 日志丢失问题

    nginx 配置: user nginx; worker_processes 1; syslog local5 nginx-zjzc01; rsyslog 服务器收到的消息: -rw-r--r-- 1 ...

  5. 【原创】大叔经验分享(80)openresty(nginx+lua)发邮件

    nginx配置 lua_package_path "/usr/local/openresty/lualib/resty/smtp/?.lua;;"; lua_need_reques ...

  6. nginx+lua实现灰度发布/waf防火墙

    nginx+lua 实现灰度发布 waf防火墙 课程链接:[课程]Nginx 与 Lua 实现灰度发布与 WAF 防火墙(完)_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili 参考博客 Nginx ...

  7. Nginx基础 - Nginx+Lua实现灰度发布与WAF

    1.Nginx加载Lua环境默认情况下Nginx不支持Lua模块, 需要安装LuaJIT解释器, 并且需要重新编译Nginx, 建议使用openrestry 1)环境准备 [root@localhos ...

  8. Nginx 模块开发(1)—— 一个稍稍能说明问题模块开发 Step By Step 过程

    1. Nginx 介绍        Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,它的发音为“engine X”, 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/S ...

  9. nginx的基础应用

    nginx的基础应用 一.简介 今天我们将介绍一些nginx的简单应用,启动.停止nginx,重载nginx的配置,nginx配置文件的格式,如何配置nginx服务静态资源,如何配置nginx作为反向 ...

随机推荐

  1. 如何 Graphics 对象设置背景色

    用 Clear 方法可以轻松地给 Graphics 对象设置背景色. using (Bitmap bmp = new Bitmap(width, height)){    using (Graphic ...

  2. English trip -- VC(情景课)1 A Get ready

    Meet your classmates 见见你的同学 Look at the picture. What do you see? 看图片.你看到了什么? calendar  日历 bookcase ...

  3. spojPlay on Words

    题意:给出几个词语,问能不能接龙. 一开始猜只要所有字母连通,并且只有一个字母出现在开头次数为奇,一个字母末尾为奇,其它偶,就行.后来发现全为偶也行.而且条件也不对,比如ac,ac,ac就不行.实际上 ...

  4. H5表单基础知识(二)

    表单新增属性 <!--<input type="text" class="name" />--> <!-- placeholder ...

  5. President's Path CodeForces - 416E (最短路,计数)

    大意: 给定无向图, 求任意两点间所有最短路经过的边数 刚开始想先用floyd跑出最短路, 然后在DAG上DP, 交了1发, 发现会算重复 贴一下题解的做法 #include <iostream ...

  6. Python下图片的高斯模糊化的优化

    资源下载 #本文PDF版下载 Python下图片的高斯模糊化的优化(或者单击我博客园右上角的github小标,找到lab102的W6目录下即可) #本文代码下载 高斯模糊(一维)优化代码(和本文方法集 ...

  7. git 添加tag

    前言 什么是tag?tag是节点的意思,一般在上线的时候使用.比如说:你在本地做了好几个功能,然后把这些功能提交到了上线的分支上,某个时刻,你想上线你的新功能,这个时候你需要你个tag来标记一下,告诉 ...

  8. hdu-2227-dp+bit

    Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/3 ...

  9. POJ-2251 Dungeon Master (BFS模板题)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  10. GDI+ DrawString字间距问题

    ///   <summary> ///   绘制任意间距文字 /// </summary> ///   <param   name= "text "& ...