利用nginx+lua+memcache实现灰度发布
一、灰度发布原理说明
灰度发布在百度百科中解释:
灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式。AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B上面 来。灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度。
这里的用于WEB系统新代码的测试发布,让一部分(IP)用户访问新版本,一部分用户仍然访问正常版本,其原理如图:

执行过程:
1、 当用户请求到达前端代理服务Nginx,内嵌的lua模块解析Nginx配置文件中的lua脚本代码;
2、 Lua变量获得客户端IP地址,去查询memcached缓存内是否有该键值,如果有返回值执行@client_test,否则执行@client。
3、 Location @client_test把请求转发给部署了new版代码的服务器,location @client把请求转发给部署了normal版代码的服务器,服务器返回结果。整个过程完成。
下面把安装配置过程详细说明。
二、安装配置过程详解
1、安装nginx
安装依赖包
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers make pcre-devel yum -y install gd gd2 gd-devel gd2-devel lua lua-devel yum –y install memcached
下载lua模块、lua-memcache操作库文件和nginx包
wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.5.tar.gz wget https://github.com/agentzh/lua-resty-memcached/archive/v0.11.tar.gz wget http://nginx.org/download/nginx-1.4.2.tar.gz tar xvf nginx-1.4..tar.gz cd nginx-1.4./ ./configure --prefix=/soft/nginx/ --with-http_gzip_static_module --add-module=/root/ngx_devel_kit-0.2./ --add-module=/root/lua-nginx-module-0.8./ make make install
拷贝lua的memcached操作库文件
tar xvf v0..tar.gz cp -r lua-resty-memcached-0.11/lib/resty/ /usr/lib64/lua/5.1/
配置nginx
#vim /soft/nginx/conf/nginx.conf
worker_processes ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
proxy_next_upstream error timeout;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $http_x_forwarded_for;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100m;
client_body_buffer_size 256k;
proxy_connect_timeout ;
proxy_send_timeout ;
proxy_read_timeout ;
proxy_buffer_size 8k;
proxy_buffers 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
upstream client {
server 192.168.200.29:;
}
upstream client_test {
server 192.168.200.29:;
}
server {
listen ;
server_name localhost;
location / {
content_by_lua '
clientIP = ngx.req.get_headers()["X-Real-IP"]
if clientIP == nil then
clientIP = ngx.req.get_headers()["x_forwarded_for"]
end
if clientIP == nil then
clientIP = ngx.var.remote_addr
end
local memcached = require "resty.memcached"
local memc, err = memcached:new()
if not memc then
ngx.say("failed to instantiate memc: ", err)
return
end
local ok, err = memc:connect("127.0.0.1", )
if not ok then
ngx.say("failed to connect: ", err)
return
end
local res, flags, err = memc:get(clientIP)
if err then
ngx.say("failed to get clientIP ", err)
return
end
if res == "" then
ngx.exec("@client_test")
return
end
ngx.exec("@client")
';
}
location @client{
proxy_pass http://client;
}
location @client_test{
proxy_pass http://client_test;
}
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
location = /50x.html {
root html;
}
}
}
检测配置文件。
#/soft/nginx/sbin/nginx -t nginx: the configuration file /soft/nginx/conf/nginx.conf syntax is ok nginx: configuration file /soft/nginx/conf/nginx.conf test is successful
启动nginx
/soft/nginx/sbin/nginx
启动memcached服务
memcached -u nobody -m -c -p –d
三、测试验证
测试lua模块是否运行正常
访问http://测试服务器ip地址/hello。如果显示:hello,lua 表示安装成功。

在另一台测试机(这里是192.168.200.29)设置两个虚拟主机,一个用80端口是执行正常代码,一个是81端口执行灰度测试代码。
在memcached中以你的客户机IP地址为key,value值为1。这里我的IP是192.168.68.211.
telnet localhost Trying ::... Connected to localhost. Escape character is '^]'. set 192.168.68.211 360 STORED get 192.168.68.211 VALUE 192.168.68.211 END quit
注意:
set后第一个值为key值。
192.168.68.211这是key值是需要灰度测试的IP地址;
0 表示一个跟该key有关的自定义数据;
3600 表示该key值的有效时间;
1 表示key所对应的value值的字节数。
下面访问Nginx,效果符合预期,我的IP已经在memcached中存储值,所以请求转发给执行灰度测试代码的主机。

从memcached删除我的主机IP值。

再次请求Nginx,请求转发给执行正常代码内容的主机。

整个配置并不复杂,整个判断过程对服务的影响非常小。如果需要使用这个系统最好自己看看lua脚本。
利用nginx+lua+memcache实现灰度发布的更多相关文章
- nginx+lua+redis实现灰度发布_test
nginx+lua+redis实现灰度发布: 灰度发布是指在黑白之间能够平滑过渡的一种方式 AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见, ...
- 基于nginx+lua简单的灰度发布系统
upstream.conf upstream grey_1 { keepalive 1000; server localhost:8020; } upstream grey_2 { keepalive ...
- 01 . OpenResty简介部署,优缺点,压测,适用场景及用Lua实现服务灰度发布
简介 OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极高的动态 ...
- nginx 根据IP 进行灰度发布
灰度发布,简单来说,就是根据各种条件,让一部分用户使用旧版本,另一部分用户使用新版本. nginx 的语法本身可以看作是一门小型的编程语言,通过简单的编程,可以轻松实现基于IP的灰度发布. 需求:搭建 ...
- Nginx基础 - Nginx+Lua实现灰度发布与WAF
1.Nginx加载Lua环境默认情况下Nginx不支持Lua模块, 需要安装LuaJIT解释器, 并且需要重新编译Nginx, 建议使用openrestry 1)环境准备 [root@localhos ...
- nginx+lua实现灰度发布/waf防火墙
nginx+lua 实现灰度发布 waf防火墙 课程链接:[课程]Nginx 与 Lua 实现灰度发布与 WAF 防火墙(完)_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili 参考博客 Nginx ...
- 使用Nginx实现灰度发布
灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式.AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B ...
- 使用Nginx实现灰度发布(转)
灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式.AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B ...
- 干货分享|使用 Istio 实现灰度发布
Kubernetes 作为基础平台,提供了强大的容器编排能力.但是在其上部署业务和服务治理上,仍然会面对一些复杂性和局限性.在服务治理上,已经有许多成熟的 ServiceMesh 框架用于扩充其能力, ...
随机推荐
- MVC3 Model Binding验证方式
1.使用ModelState在Action中进行验证 [HttpPost] public ViewResult MakeBooking(Appointment appt) { if (string.I ...
- Unity3D 优化相关
抛砖引玉: http://www.luzexi.com/unity3d%E4%BC%98%E5%8C%96%E4%B9%8B%E8%B7%AF/ 关于图片 一.Unity3D自身会把导入的图片进行压缩 ...
- linux源代码阅读笔记 fork和execve的区别
1. man exec就可以知到: The exec() family of functions replaces the current process image with a new proce ...
- UVA 11174 Stand in a Line (组合+除法的求模)
题意:村子里有n个人,给出父亲和儿子的关系,有多少种方式可以把他们排成一列,使得没人会排在他父亲的前面 思路:设f[i]表示以i为根的子树有f[i]种排法,节点i的各个子树的根节点,即它的儿子为c1, ...
- Selenium--cssselector
CSS(即层叠样式表Cascading Stylesheet) Selector来定位(locate)页面上的元素(Elements).Selenium官网的Document里极力推荐使用CS ...
- Bash 小知识点
变量定义的时候=两边不能有空格,例如: a='Hello World' 如果变量和其它字符相连,可以用{}把变量引起来,这样就可以和相连的字符隔离 除了在变量赋值和在FOR循环语句头中,BASH中的变 ...
- [z]CAP原理和BASE思想
分布式领域CAP理论,Consistency(一致性), 数据一致更新,所有数据变动都是同步的Availability(可用性), 好的响应性能Partition tolerance(分区容错性) 可 ...
- 【动态规划】流水作业调度问题与Johnson法则
1.问题描述: n个作业{1,2,…,n}要在由2台机器M1和M2组成的流水线上完成加工.每个作业加工的顺序都是先在M1上加工,然后在M2上加工.M1和M2加工作业i所需的时间分别为ai和bi ...
- for each
public class ArrayForeach{ public static void main(String []args){ int [][]a={{1,2,3},{4,5,6},{7,8,9 ...
- QT进度条QProgressBar的练习(定制QProgressBar,单独成为一个控件)
progressbar.h #ifndef PROGRESSBAR_H #define PROGRESSBAR_H #include <QProgressBar> class QStrin ...