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

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

wptr33 2025-01-21 21:57 30 浏览

灰度发布原理

灰度发布(又名金丝雀发布)是指在黑与白之间,能够平滑过渡的一种发布方式。在其上可以进行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



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

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


相关推荐

MySQL进阶五之自动读写分离mysql-proxy

自动读写分离目前,大量现网用户的业务场景中存在读多写少、业务负载无法预测等情况,在有大量读请求的应用场景下,单个实例可能无法承受读取压力,甚至会对业务产生影响。为了实现读取能力的弹性扩展,分担数据库压...

Postgres vs MySQL_vs2022连接mysql数据库

...

3分钟短文 | Laravel SQL筛选两个日期之间的记录,怎么写?

引言今天说一个细分的需求,在模型中,或者使用laravel提供的EloquentORM功能,构造查询语句时,返回位于两个指定的日期之间的条目。应该怎么写?本文通过几个例子,为大家梳理一下。学习时...

一文由浅入深带你完全掌握MySQL的锁机制原理与应用

本文将跟大家聊聊InnoDB的锁。本文比较长,包括一条SQL是如何加锁的,一些加锁规则、如何分析和解决死锁问题等内容,建议耐心读完,肯定对大家有帮助的。为什么需要加锁呢?...

验证Mysql中联合索引的最左匹配原则

后端面试中一定是必问mysql的,在以往的面试中好几个面试官都反馈我Mysql基础不行,今天来着重复习一下自己的弱点知识。在Mysql调优中索引优化又是非常重要的方法,不管公司的大小只要后端项目中用到...

MySQL索引解析(联合索引/最左前缀/覆盖索引/索引下推)

目录1.索引基础...

你会看 MySQL 的执行计划(EXPLAIN)吗?

SQL执行太慢怎么办?我们通常会使用EXPLAIN命令来查看SQL的执行计划,然后根据执行计划找出问题所在并进行优化。用法简介...

MySQL 从入门到精通(四)之索引结构

索引概述索引(index),是帮助MySQL高效获取数据的数据结构(有序),在数据之外,数据库系统还维护者满足特定查询算法的数据结构,这些数据结构以某种方式引用(指向)数据,这样就可以在这些数据结构...

mysql总结——面试中最常问到的知识点

mysql作为开源数据库中的榜一大哥,一直是面试官们考察的重中之重。今天,我们来总结一下mysql的知识点,供大家复习参照,看完这些知识点,再加上一些边角细节,基本上能够应付大多mysql相关面试了(...

mysql总结——面试中最常问到的知识点(2)

首先我们回顾一下上篇内容,主要复习了索引,事务,锁,以及SQL优化的工具。本篇文章接着写后面的内容。性能优化索引优化,SQL中索引的相关优化主要有以下几个方面:最好是全匹配。如果是联合索引的话,遵循最...

MySQL基础全知全解!超详细无废话!轻松上手~

本期内容提醒:全篇2300+字,篇幅较长,可搭配饭菜一同“食”用,全篇无废话(除了这句),干货满满,可收藏供后期反复观看。注:MySQL中语法不区分大小写,本篇中...

深入剖析 MySQL 中的锁机制原理_mysql 锁详解

在互联网软件开发领域,MySQL作为一款广泛应用的关系型数据库管理系统,其锁机制在保障数据一致性和实现并发控制方面扮演着举足轻重的角色。对于互联网软件开发人员而言,深入理解MySQL的锁机制原理...

Java 与 MySQL 性能优化:MySQL分区表设计与性能优化全解析

引言在数据库管理领域,随着数据量的不断增长,如何高效地管理和操作数据成为了一个关键问题。MySQL分区表作为一种有效的数据管理技术,能够将大型表划分为多个更小、更易管理的分区,从而提升数据库的性能和可...

MySQL基础篇:DQL数据查询操作_mysql 查

一、基础查询DQL基础查询语法SELECT字段列表FROM表名列表WHERE条件列表GROUPBY分组字段列表HAVING分组后条件列表ORDERBY排序字段列表LIMIT...

MySql:索引的基本使用_mysql索引的使用和原理

一、索引基础概念1.什么是索引?索引是数据库表的特殊数据结构(通常是B+树),用于...