智能摘要文章源自JAVA秀-https://www.javaxiu.com/35042.html
灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度。client把请求转发给部署了normal版代码的服务器,服务器返回结果。整个配置并不复杂,整个判断过程对服务的影响非常小。最近就围绕服务器的主题创建了一个微信群,喜欢玩服务器或者想自己开发一款产品的读者可以进来,相互学习交流!群通知中给大家分享了一套搭建服务器的视频教程哦。文章源自JAVA秀-https://www.javaxiu.com/35042.html
原文约 2063 字 | 图片 8 张 | 建议阅读 5 分钟 | 评价反馈文章源自JAVA秀-https://www.javaxiu.com/35042.html
基于 Nginx+lua+Memcache 实现灰度发布
菜鸟要飞 文章源自JAVA秀-https://www.javaxiu.com/35042.html
作者:文彪 原文:https://www.cnblogs.com/wenbiao/p/3227998.html文章源自JAVA秀-https://www.javaxiu.com/35042.html
一、灰度发布原理说明文章源自JAVA秀-https://www.javaxiu.com/35042.html
灰度发布在百度百科中解释:文章源自JAVA秀-https://www.javaxiu.com/35042.html
灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式。AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B上面 来。灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度。文章源自JAVA秀-https://www.javaxiu.com/35042.html
这里的用于WEB系统新代码的测试发布,让一部分(IP)用户访问新版本,一部分用户仍然访问正常版本,其原理如图:文章源自JAVA秀-https://www.javaxiu.com/35042.html
文章源自JAVA秀-https://www.javaxiu.com/35042.html
文章源自JAVA秀-https://www.javaxiu.com/35042.html
执行过程:文章源自JAVA秀-https://www.javaxiu.com/35042.html
当用户请求到达前端代理服务Nginx,内嵌的lua模块解析Nginx配置文件中的lua脚本代码;文章源自JAVA秀-https://www.javaxiu.com/35042.html
Lua变量获得客户端IP地址,去查询memcached缓存内是否有该键值,如果有返回值执行@client_test,否则执行@client。文章源自JAVA秀-https://www.javaxiu.com/35042.html
Location @client_test把请求转发给部署了new版代码的服务器,location @client把请求转发给部署了normal版代码的服务器,服务器返回结果。整个过程完成。文章源自JAVA秀-https://www.javaxiu.com/35042.html
下面把安装配置过程详细说明。文章源自JAVA秀-https://www.javaxiu.com/35042.html
文章源自JAVA秀-https://www.javaxiu.com/35042.html
二、安装配置过程详解文章源自JAVA秀-https://www.javaxiu.com/35042.html
1、安装nginx文章源自JAVA秀-https://www.javaxiu.com/35042.html
安装依赖包文章源自JAVA秀-https://www.javaxiu.com/35042.html
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-develyum -y install gd gd2 gd-devel gd2-devel lua lua-develyum –y install memcached
文章源自JAVA秀-https://www.javaxiu.com/35042.html下载lua模块、lua-memcache操作库文件和nginx包文章源自JAVA秀-https://www.javaxiu.com/35042.html
wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gzwget https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.5.tar.gzwget https://github.com/agentzh/lua-resty-memcached/archive/v0.11.tar.gzwget http://nginx.org/download/nginx-1.4.2.tar.gz#解压编译安装tar xvf nginx-1.4.2.tar.gzcd nginx-1.4.2/./configure \--prefix=/soft/nginx/ \--with-http_gzip_static_module \--add-module=/root/ngx_devel_kit-0.2.18/ \--add-module=/root/lua-nginx-module-0.8.5/make&&make install
文章源自JAVA秀-https://www.javaxiu.com/35042.html拷贝lua的memcached操作库文件文章源自JAVA秀-https://www.javaxiu.com/35042.html
tar xvf v0.11.tar.gzcp -r lua-resty-memcached-0.11/lib/resty/ /usr/lib64/lua/5.1/
文章源自JAVA秀-https://www.javaxiu.com/35042.html配置nginx文章源自JAVA秀-https://www.javaxiu.com/35042.html
#vim /soft/nginx/conf/nginx.confworker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; 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 180; proxy_send_timeout 180; proxy_read_timeout 180; proxy_buffer_size 8k; proxy_buffers 8 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; upstream client { server 192.168.200.29:80; } upstream client_test { server 192.168.200.29:81; } server { listen 80; 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", 11211) 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 == "1" 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; } }}
文章源自JAVA秀-https://www.javaxiu.com/35042.html检测配置文件。文章源自JAVA秀-https://www.javaxiu.com/35042.html
#/soft/nginx/sbin/nginx -tnginx: the configuration file /soft/nginx/conf/nginx.conf syntax is oknginx: configuration file /soft/nginx/conf/nginx.conf test is successful
文章源自JAVA秀-https://www.javaxiu.com/35042.html启动nginx文章源自JAVA秀-https://www.javaxiu.com/35042.html
/soft/nginx/sbin/nginx
文章源自JAVA秀-https://www.javaxiu.com/35042.html启动memcached服务文章源自JAVA秀-https://www.javaxiu.com/35042.html
memcached -u nobody -m 1024 -c 2048 -p 11211 –d
文章源自JAVA秀-https://www.javaxiu.com/35042.html三、测试验证文章源自JAVA秀-https://www.javaxiu.com/35042.html
测试lua模块是否运行正常文章源自JAVA秀-https://www.javaxiu.com/35042.html
访问http://测试服务器ip地址/hello。如果显示:hello,lua 表示安装成功。文章源自JAVA秀-https://www.javaxiu.com/35042.html
文章源自JAVA秀-https://www.javaxiu.com/35042.html
在另一台测试机(这里是192.168.200.29)设置两个虚拟主机,一个用80端口是执行正常代码,一个是81端口执行灰度测试代码。文章源自JAVA秀-https://www.javaxiu.com/35042.html
在memcached中以你的客户机IP地址为key,value值为1。这里我的IP是192.168.68.211.文章源自JAVA秀-https://www.javaxiu.com/35042.html
telnet localhost 11211Trying ::1...Connected to localhost.Escape character is '^]'.set 192.168.68.211 0 3600 11STOREDget 192.168.68.211VALUE 192.168.68.211 9 11ENDquit
文章源自JAVA秀-https://www.javaxiu.com/35042.html注意:文章源自JAVA秀-https://www.javaxiu.com/35042.html
set后第一个值为key值。文章源自JAVA秀-https://www.javaxiu.com/35042.html
192.168.68.211这是key值是需要灰度测试的IP地址;文章源自JAVA秀-https://www.javaxiu.com/35042.html
0 表示一个跟该key有关的自定义数据;文章源自JAVA秀-https://www.javaxiu.com/35042.html
3600 表示该key值的有效时间;文章源自JAVA秀-https://www.javaxiu.com/35042.html
1 表示key所对应的value值的字节数。文章源自JAVA秀-https://www.javaxiu.com/35042.html
下面访问Nginx,效果符合预期,我的IP已经在memcached中存储值,所以请求转发给执行灰度测试代码的主机。文章源自JAVA秀-https://www.javaxiu.com/35042.html
文章源自JAVA秀-https://www.javaxiu.com/35042.html
从memcached删除我的主机IP值。文章源自JAVA秀-https://www.javaxiu.com/35042.html
文章源自JAVA秀-https://www.javaxiu.com/35042.html
再次请求Nginx,请求转发给执行正常代码内容的主机。文章源自JAVA秀-https://www.javaxiu.com/35042.html
文章源自JAVA秀-https://www.javaxiu.com/35042.html
整个配置并不复杂,整个判断过程对服务的影响非常小。如果需要使用这个系统最好自己看看lua脚本。文章源自JAVA秀-https://www.javaxiu.com/35042.html
文章源自JAVA秀-https://www.javaxiu.com/35042.html
说个题外话,鸟哥是个比较喜欢折腾的程序员,业余喜欢开发自己网站、小程序、App等,这些东西统统离不开服务器!最近就围绕服务器的主题创建了一个微信群,喜欢玩服务器或者想自己开发一款产品的读者可以进来,相互学习交流!群通知中给大家分享了一套搭建服务器的视频教程哦。非常适合新手学习!我也会时不时的带大家撸点和服务器相关的优惠券!不感兴趣,不喜欢折腾的就没必要凑着闹了!文章源自JAVA秀-https://www.javaxiu.com/35042.html
识别二维码,添加微信后文章源自JAVA秀-https://www.javaxiu.com/35042.html
发送【服务器】即可获取邀请链接文章源自JAVA秀-https://www.javaxiu.com/35042.html
文章源自JAVA秀-https://www.javaxiu.com/35042.html
这是我部署的机器人,请勿调戏!文章源自JAVA秀-https://www.javaxiu.com/35042.html
推荐阅读文章源自JAVA秀-https://www.javaxiu.com/35042.html
嘘!刚刚发现了一个山寨版某库....文章源自JAVA秀-https://www.javaxiu.com/35042.html
可怕!公司部署了一个东西,悄悄盯着你···文章源自JAVA秀-https://www.javaxiu.com/35042.html
发小被绿,我竭尽所学黑科技,动用云控捉奸寻找证据….文章源自JAVA秀-https://www.javaxiu.com/35042.html
终于把废旧电脑变成了服务器!差点被女票拿去换洗脸盆,真香!
文章源自JAVA秀-https://www.javaxiu.com/35042.html

评论