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

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

off999 2025-03-10 19:16 13 浏览 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



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

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


相关推荐

Python钩子函数实现事件驱动系统(created钩子函数)

钩子函数(HookFunction)是现代软件开发中一个重要的设计模式,它允许开发者在特定事件发生时自动执行预定义的代码。在Python生态系统中,钩子函数广泛应用于框架开发、插件系统、事件处理和中...

Python函数(python函数题库及答案)

定义和基本内容def函数名(传入参数):函数体return返回值注意:参数、返回值如果不需要,可以省略。函数必须先定义后使用。参数之间使用逗号进行分割,传入的时候,按照顺序传入...

Python技能:Pathlib面向对象操作路径,比os.path更现代!

在Python编程中,文件和目录的操作是日常中不可或缺的一部分。虽然,这么久以来,钢铁老豆也还是习惯性地使用os、shutil模块的函数式API,这两个模块虽然功能强大,但在某些情况下还是显得笨重,不...

使用Python实现智能物流系统优化与路径规划

阅读文章前辛苦您点下“关注”,方便讨论和分享,为了回馈您的支持,我将每日更新优质内容。在现代物流系统中,优化运输路径和提高配送效率是至关重要的。本文将介绍如何使用Python实现智能物流系统的优化与路...

Python if 语句的系统化学习路径(python里的if语句案例)

以下是针对Pythonif语句的系统化学习路径,从零基础到灵活应用分为4个阶段,包含具体练习项目和避坑指南:一、基础认知阶段(1-2天)目标:理解条件判断的逻辑本质核心语法结构if条件:...

[Python] FastAPI基础:Path路径参数用法解析与实例

查询query参数(上一篇)路径path参数(本篇)请求体body参数(下一篇)请求头header参数本篇项目目录结构:1.路径参数路径参数是URL地址的一部分,是必填的。路径参...

Python小案例55- os模块执行文件路径

在Python中,我们可以使用os模块来执行文件路径操作。os模块提供了许多函数,用于处理文件和目录路径。获取当前工作目录(CurrentWorkingDirectory,CWD):使用os....

python:os.path - 常用路径操作模块

应该是所有程序都需要用到的路径操作,不废话,直接开始以下是常用总结,当你想做路径相关时,首先应该想到的是这个模块,并知道这个模块有哪些主要功能,获取、分割、拼接、判断、获取文件属性。1、路径获取2、路...

原来如此:Python居然有6种模块路径搜索方式

点赞、收藏、加关注,下次找我不迷路当我们使用import语句导入模块时,Python是怎么找到这些模块的呢?今天我就带大家深入了解Python的6种模块路径搜索方式。一、Python模块...

每天10分钟,python进阶(25)(python进阶视频)

首先明确学习目标,今天的目标是继续python中实例开发项目--飞机大战今天任务进行面向对象版的飞机大战开发--游戏代码整编目标:完善整串代码,提供完整游戏代码历时25天,首先要看成品,坚持才有收获i...

python 打地鼠小游戏(打地鼠python程序设计说明)

给大家分享一段AI自动生成的代码(在这个游戏中,玩家需要在有限时间内打中尽可能多的出现在地图上的地鼠),由于我现在用的这个电脑没有安装sublime或pycharm等工具,所以还没有测试,有兴趣的朋友...

python线程之十:线程 threading 最终总结

小伙伴们,到今天threading模块彻底讲完。现在全面总结threading模块1、threading模块有自己的方法详细点击【threading模块的方法】threading模块:较低级...

Python信号处理实战:使用signal模块响应系统事件

信号是操作系统用来通知进程发生了某个事件的一种异步通信方式。在Python中,标准库的signal模块提供了处理这些系统信号的机制。信号通常由外部事件触发,例如用户按下Ctrl+C、子进程终止或系统资...

Python多线程:让程序 “多线作战” 的秘密武器

一、什么是多线程?在日常生活中,我们可以一边听音乐一边浏览新闻,这就是“多任务处理”。在Python编程里,多线程同样允许程序同时执行多个任务,从而提升程序的执行效率和响应速度。不过,Python...

用python写游戏之200行代码写个数字华容道

今天来分析一个益智游戏,数字华容道。当初对这个游戏颇有印象还是在最强大脑节目上面,何猷君以几十秒就完成了这个游戏。前几天写2048的时候,又想起了这个游戏,想着来研究一下。游戏玩法用尽量少的步数,尽量...

取消回复欢迎 发表评论: