灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式。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. kali删除软件

    kali中主要为2种卸载方法:1.apt2.dpkg 使用apt的方式有:apt-get remove [package]apt-get remove --purge # ------(package ...

  2. Thunder团队项目视频展示

    视频链接:http://v.youku.com/v_show/id_XMzA5MjMzMzcyMA==.html?spm=a2h3j.8428770.3416059.1 视频简介:通过一个小情景开篇, ...

  3. codeforces 578b//"Or" Game// Codeforces Round #320 (Div. 1)

    题意:n个数字,最多操作k次,每次乘x,要使结果数组的与值最大. 能推断出结果是对一个元素操作k次,并且这个元素的二进制最高位比较大.并不一定是取最大的,比如1100和1010,乘以一次2,两种选法分 ...

  4. 最短路-Prim算法 dijkstra算法

    HDU-1233 #include <iostream> #define INF 1000000 using namespace std; ][]; ]; ]; ]; ]; int mai ...

  5. Confluence 6 连接一个目录

    你可以添加下面类型的目录服务器和目录管理器: Confluence 的内部目录(Configuring the Internal Directory). Microsoft Active Direct ...

  6. 在centos7上安装gcc、node.js(源码下载)

    一.在centos7中安装node.js https://www.cnblogs.com/lpbottle/p/7733397.html 1.从源码下载Nodejs cd /usr/local/src ...

  7. Jenkins install

    Linux CentOS 7.1 x64 Java 1.8 x64 apache-maven-3.3.9 Installation sudo wget -O /etc/yum.repos.d/jenk ...

  8. thinkphp关于时间加减几天

    1.当前时间,往后退5天: date('Y-m-d H:i:s',strtotime('-1 days')); 2.有固定时间,往后面退一天或者七天,或者30天: 比如时间:$time = 2014- ...

  9. RAC配置(启停库)

    关库顺序 :先关闭数据库 然后关闭节点资源 [root@rac1 ~]# srvctl stop   database  -d 数据库名[root@rac1 ~]# srvctl stop   ins ...

  10. Mybatis学习总结-----mybatis中refid是什么意思(十)

    1.首先定义一个sql标签,一定要定义唯一id<sql id="Base_Column_List" >name,age</sql>2.然后通过id引用< ...