博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Nginx实现灰度发布
阅读量:6973 次
发布时间:2019-06-27

本文共 3653 字,大约阅读时间需要 12 分钟。

灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式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:8080hilinux_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

转载地址:http://etesl.baihongyu.com/

你可能感兴趣的文章
GIX4中懒加载
查看>>
数据仓库专题(1)-数据仓库生命周期模型
查看>>
[华为机试练习题]43.在字符串中找出连续最长的数字串
查看>>
LogCat大量Unexpected value from nativeGetEnabledTags: 0
查看>>
一分钟了解阿里云产品:补丁管理
查看>>
区间调度问题
查看>>
一键U盘启动快捷方式
查看>>
阿里云容器服务体验: 部署 ShellPays 条码支付整合服务平台 -- (一)系统概要与环境准备...
查看>>
diff corp's HBA and multipath
查看>>
页面平滑滚动
查看>>
UIImagePickController打开闪光模式拍照瞬间锁屏crash
查看>>
nodejs项目部署到腾讯云详细步骤
查看>>
PHP 代码调试跟踪工具 Ytrace
查看>>
Go并发调用的超时处理
查看>>
Flutter初探
查看>>
python发送邮件
查看>>
拼?还是熬?一次发散且零散的创业心得分享
查看>>
Android控制颜色透明度百分比的方法
查看>>
开发者人手一个的chrome插件——掘金
查看>>
数据库读写分离,主从同步实现方法
查看>>