百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

一文看懂灰度发布——基于Nginx+Lua+Redis

off999 2025-03-10 19:16 24 浏览 0 评论

灰度发布原理

灰度发布(又名金丝雀发布)是指在黑与白之间,能够平滑过渡的一种发布方式。在其上可以进行A/B testing,即让一部分用户继续用产品特性A,一部分用户开始用产品特性B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B 上面来。灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度。


环境:

192.168.2.30 openrestry

192.168.2.31 redis

192.168.2.32 tomcat1 生产环境

192.168.2.33 tomcat2 预发布环境


工作流程:

模拟请求到达openresty后,openresty从redis获取白名单,然后判断请求地址是否再白名单,在白名单里就转到192.168.2.33 预发布环境 ,

否则转到192.168.2.32 生产环境


安装openresty(redis、tomcat安装忽略)

1.1、 环境准备:

[root@aly ~]# yum install pcre-devel gcc curl

ps: openssl 使用源码包安装 openssl-1.0.2n.tar.gz

[root@aly ~]# tar -xf openssl-1.0.2n.tar.gz -C /usr/local


1.2、安装openresty 软件

注意:编译需添加ssl支持,如果需要编译的openssl,是不需要编译opensll,--with-openssl=DIR DIR是openssl的源码路径,不是openssl的安装路径


解压openresty软件包

[root@aly ~]# tar -xf openresty-1.15.8.3.tar.gz -C /fxkj


[root@aly ~]# cd /fxkj/openresty/
[root@aly openresty]# ./configure --prefix=/fxkj/openresty --with-pcre --with-ipv6 --with-http_stub_status_module --with-openssl=/usr/local/openssl-1.0.2n

#添加ipv6模块,nginx 状态页模块,指定openssl 路径


[root@aly openresty]# make -j8        #我这里是8核cpu,-j 8 可以加速编译


[root@aly openresty]# make install


1.3 查看openresty 安装的Nginx版本以及安装的模块


[root@aly openssl-1.0.2n]#  /fxkj/openresty/nginx/sbin/nginx -v
nginx version: openresty/1.15.8.3


[root@aly openssl-1.0.2n]# /fxkj/openresty/nginx/sbin/nginx -V
nginx version: openresty/1.15.8.3
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2n 7 Dec 2017
TLS SNI support enabled
configure arguments: --prefix=/fxkj/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.1rc1 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2 --add-module=../set-misc-nginx-module-0.32 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.08 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.15 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.33 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.19 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.15 --add-module=../rds-csv-nginx-module-0.09 --add-module=../ngx_stream_lua-0.0.7 --with-ld-opt=-Wl,-rpath,/fxkj/openresty/luajit/lib --with-pcre --with-ipv6 --with-http_stub_status_module --with-openssl=/usr/local/openssl-1.0.2n --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_ssl_module


1.4 配置nginx.conf 配置文件


[root@aly openresty]# vim /fxkj/openresty/nginx/conf/nginx.conf


user root;
worker_processes 1;
error_log logs/error.log;

events {
worker_connections 1024;
}

http {
#添加;;标识默认路径下的lualib
lua_package_path "$prefix/lualib/?.lua;;";
lua_package_cpath "$prefix/lualib/?.so;;";


upstream prod1 
	{
server 192.168.2.32:8080;
}

upstream prod2-grey {
server 192.168.2.33:8080;
}



server {
listen 80;
server_name localhost;
location / {
#为每个请求执行gray.lua脚本
content_by_lua_file /fxkj/openresty/nginx/conf/test.lua;
}

location @prod1 {
proxy_pass http://prod1;
       }

location @prod2-grey {
proxy_pass http://prod2-grey;
        }
   }
}


1.5 编写tomcat后端访问内容


192.168.2.32 tomcat1 生产环境:

[root@tomcat1 ~]# echo "192.168.2.32 tomcat1 production environment" > /usr/local/apache-tomcat-7.0.96/webapps/ROOT/index.jsp


192.168.2.33 tomcat2 预发布环境:


[root@tomcat2 ~]# echo "192.168.2.33 tomcat2 Pre release environment" > /usr/local/apache-tomcat-7.0.96/webapps/ROOT/index.jsp


1.6 编写lua 脚本,位置在
/fxkj/openresty/nginx/conf/test.lua


[root@aly openresty]# vim /fxkj/openresty/nginx/conf/test.lua


local redis=require "resty.redis";

local red=redis:new();

red:set_timeout(1000);
--redis连接
local ok,err=red:connect("192.168.2.31", 6379);

if not ok then
ngx.say("failed to connect redis ",err);
return;
end
--获取请求ip
local local_ip = ngx.req.get_headers()["X-Real-IP"];
if local_ip == nil then
local_ip = ngx.req.get_headers()["x_forwarded_for"];
end
if local_ip == nil then
local_ip = ngx.var.remote_addr;
end

local_ip=ngx.var.remote_addr;

--redis中获取白名单
local ip_lists=red:get("gray");

--判断是否在白名单然后转到对应服务

if string.find(ip_lists,local_ip) == nil then
ngx.exec("@prod1");
else
ngx.exec("@prod2-grey");
end

local ok,err=red:close();


1.7 检测语法是否正确


[root@aly openresty]# /fxkj/openresty/nginx/sbin/nginx -t
nginx: the configuration file /fxkj/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /fxkj/openresty/nginx/conf/nginx.conf test is successful


1.8、启动openresty


[root@aly / ]# /fxkj/openresty/nginx/sbin/nginx



1.9 验证openrestry


用客户端 : 192.168.2.165 去访问


打开浏览器访问: http://192.168.2.30



2、在redis 中添加 客户端IP地址


[root@redis redis]# redis-cli -h 192.168.2.31 -p 6379
192.168.2.31:6379> set gray 192.168.2.165
OK
192.168.2.31:6379> get gray
"192.168.2.165"



用客户端 : 192.168.2.165 去访问

再次打开浏览器: http://192.168.2.30



可以看到 访问的内容变为了 预发布环境

本期的 灰度发布 的内容就先到这,欢迎小伙伴留言评论


相关推荐

cad看图(cad看图王)

以下是一些CAD看图的方法和技巧:1.放大/缩小:使用滚轮或放大镜工具可以方便地放大或缩小绘图。2.平移:使用平移工具可以在不改变视角的情况下将绘图向上、向下、向左或向右移动。3.旋转:使用旋转...

联想笔记本电脑装系统教程(联想笔记本装系统教程win10)
  • 联想笔记本电脑装系统教程(联想笔记本装系统教程win10)
  • 联想笔记本电脑装系统教程(联想笔记本装系统教程win10)
  • 联想笔记本电脑装系统教程(联想笔记本装系统教程win10)
  • 联想笔记本电脑装系统教程(联想笔记本装系统教程win10)
dell笔记本售后服务电话是多少
dell笔记本售后服务电话是多少

以下为dell售后服务点A:戴尔笔记本电脑维修点地址:上海市长宁区长宁路1027号兆丰广场5层 B:戴尔笔记本电脑维修点地址:上海市徐汇区漕溪北路45号 C:戴尔笔记本电脑维修点地址:上海市徐汇区漕溪路250号银海大厦1...

2026-01-02 02:03 off999

如何找回浏览器(如何找回浏览器删除记录)

如果您的浏览器出现了问题,可以尝试以下方法来恢复浏览器:1.重新启动浏览器:关闭浏览器窗口,再重新打开浏览器,看是否能够解决问题。2.清除浏览器缓存:浏览器缓存可能会导致浏览器出现问题,可以尝试清...

应用备份还原app下载(应用备份与恢复下载)

如果您已经将手机上的数据备份到电脑,希望从电脑恢复到手机,建议您:1.电脑中安装Kies软件。注:若使用的是安卓4.3操作系统,电脑中需要安装Kies3软件。2.将手机与电脑通过数据线连接,打开Kie...

office2013激活向导(microsoft office激活导向)
office2013激活向导(microsoft office激活导向)

这是没有正常激活导致的,解决方法如下:1、下载正确的microsoftoffice到桌面上,右键单击从下拉菜单中选择解压到当前文件夹。2、双击桌面上的快捷方式,打开该应用程序,切换到mian选项卡。3、接着点击ez-activator按钮...

2026-01-02 00:51 off999

h3c路由器手机登录入口(h3c路由器登录界面手机)

首先就是把华三路由器正确安装,然后手机连接路由器发射出来的WiFi信号。然后点击手机中的浏览器并深入华三路由器的登录地址 moshujia.com或者192.168.124.1,就可以登...

u盘坏了数据怎么导出来(u盘坏了里面的数据怎么办)

方法一、借助数据恢复软件u盘只要不是物理性故障且数据未覆盖的情况下,可借助u盘数据恢复软件来提取打不开的u盘数据。具体操作流程如下:在电脑上插入需要恢复数据的u盘,然后运行u盘数据恢复软件—以云骑士数...

win10家庭版原装下载(win10家庭版安装包下载)

有以下几种原因:第一是因为专业版功能较为齐全,但一般的使用者并不太需要。第二是由于功能齐全,它所占的体积也比较大,进而对电脑的运行速率有一定的影响。第三是Wln10各种版本都还是需要花钱购买的,而专业...

win7装xp系统怎么安装(win7如何安装xp系统)

设置U盘为第一启动项并进入PE系统。开机按F2进入BOIS,在BOOT选项中将U盘设为第一启动盘,通过按F6(有的是Shift+)调整顺序。(或开机按ESC选择启动盘,即你的U盘)。按F10保存...

windows 98是什么操作系统(windows98属于什么)

Windows98是微软公司发行于1998年6月25日的混合16位/32位的Windows操作系统,其版本号为4.1,开发代号为Memphis。肯定有的。Windows95操作系统刚发布的时候就...

下载mp3免费的网站(免费下载mp3哪些网站)

有免费下载mp3的网站。除了知名的几个音乐平台外,还有以下三款支持免费MP3无损音乐下载网站,可以将喜欢的歌曲下载到U盘。说明书里有呀91flac音乐网,试试这个,绝对好使,但是不要在酷狗上面说网页上...

win10更新卸载不了怎么办(win10更新后卸载更新失败)

右键桌面上“此电脑”—“管理”,或者按组合键“Windows+X”—计算机管理—服务和应用程序—服务,找到Windowsupdate和BackgroundIntelligentTransfe...

三星笔记本bios怎么设置(三星笔记本bios按哪个键)
  • 三星笔记本bios怎么设置(三星笔记本bios按哪个键)
  • 三星笔记本bios怎么设置(三星笔记本bios按哪个键)
  • 三星笔记本bios怎么设置(三星笔记本bios按哪个键)
  • 三星笔记本bios怎么设置(三星笔记本bios按哪个键)
pc浏览器是什么意思(pc模式的浏览器)

则是在电脑上使用的所有的浏览器。可以在电脑上使用的浏览器有非常多,我们现在比较常用的包括UC浏览器,搜狗浏览器,360浏览器等等,这些浏览器都可以在大部分的电脑上正常使用,而且使用起来非常流畅,市场的...

取消回复欢迎 发表评论: