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

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

off999 2025-03-10 19:16 17 浏览 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设计模式 第 13 章 中介者模式(Mediator Pattern)

在行为型模式中,中介者模式是解决“多对象间网状耦合”问题的核心模式。它就像“机场调度中心”——多个航班(对象)无需直接沟通起飞、降落时间,只需通过调度中心(中介者)协调,避免航班间的冲突与混乱...

1.3.1 python交互式模式的特点和用法

什么是Python交互模式Python交互模式,也叫Python交互式编程,是一种在Python解释器中运行的模式,它允许用户在解释器窗口中输入单个Python语句,并立即查看结果,而不需要编写整个程...

Python设计模式 第 8 章 装饰器模式(Decorator Pattern)

在结构型模式中,装饰器模式是实现“动态功能扩展”的核心模式。它就像“手机壳与手机的关系”——手机(原始对象)具备通话、上网等基础功能,手机壳(装饰器)可在不改变手机本身的前提下,为其新增保护、...

python设计模式 综合应用与实战指南

经过前面16章的学习,我们已系统掌握创建型模式(单例、工厂、建造者、原型)、结构型模式(适配器、桥接、组合、装饰器、外观、享元、代理)、行为型模式(责任链、命令、迭代器、中介者、观察者、状态、策略...

Python入门学习教程:第 16 章 图形用户界面(GUI)编程

16.1什么是GUI编程?图形用户界面(GraphicalUserInterface,简称GUI)是指通过窗口、按钮、菜单、文本框等可视化元素与用户交互的界面。与命令行界面(CLI)相比,...

Python 中 必须掌握的 20 个核心:str()

str()是Python中用于将对象转换为字符串表示的核心函数,它在字符串处理、输出格式化和对象序列化中扮演着关键角色。本文将全面解析str()函数的用法和特性。1.str()函数的基本用法1.1...

Python偏函数实战:用functools.partial减少50%重复代码的技巧

你是不是经常遇到这样的场景:写代码时同一个函数调用了几十次,每次都要重复传递相同的参数?比如处理文件时总要用encoding='utf-8',调用API时固定传Content-Type...

第2节.变量和数据类型【第29课-输出总结】

同学们,关于输出的知识点讲解完成之后,把重点性的知识点做一个总结回顾。·首先对于输出这一章节讲解的比如有格式化符号,格式化符号这里需要同学们额外去多留意的是不是百分号s格式化输出字符串。当然课上也说百...

AI最火语言python之json操作_python json.loads()

JSON(JavaScriptObjectNotation,JavaScript对象表示法)是一种开放标准的文件格式和数据交换格式,它易于人阅读和编写。JSON是一种常用的数据格式,比如对接各种第...

python中必须掌握的20个核心函数—split()详解

split()是Python字符串对象的方法,用于将字符串按照指定的分隔符拆分成列表。它是文本处理中最常用的函数之一。一、split()的基本用法1.1基本语法str.split(sep=None,...

实用方法分享:pdf文件分割方法 横向A3分割成纵向A4

今天在街上打印店给儿子打印试卷时,我在想:能不能,把它分割成A4在家中打印,这样就不需要跑到街上的打印店打印卷子了。原来,老师发的作业,是电子稿,pdf文件,A3格式的试卷。可是家中的打印机只能打印A...

20道常考Python面试题大总结_20道常考python面试题大总结免费

20道常考Python面试题大总结关于Python的面试经验一般来说,面试官会根据求职者在简历中填写的技术及相关细节来出面试题。一位拿了大厂技术岗SpecialOffer的网友分享了他总结的面试经...

Kotlin Data Classes 快速上手_kotlin快速入门

引言在日常开发中,我们常常需要创建一些只用来保存数据的类。问题是,这样的类往往需要写一堆模板化的方法:equals()、hashCode()、toString()……每次都重复,既枯燥又容易出错。//...

python自动化RobotFramework中Collections字典关键字使用(五)

前言介绍安装好robotframework库后,跟之前文章介绍的BuiltIn库一样BuiltIn库使用介绍,在“python安装目录\Lib\site-packages\robot\librarie...

Python中numpy数据分析库知识点总结

Python中numpy数据分析库知识点总结二、对已读取数据的处理②指定一个值,并对该值双边进行修改③指定两个值,并对第一个值的左侧和第二个值的右侧进行修改2.4数组的拼接和行列交换①竖直拼接(np...

取消回复欢迎 发表评论: