JAVA全系列 教程
3762个小节阅读:7095.5k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
互联网产品开发有个非常特别的地方,就是不停的升级,升级,再升级。采用敏捷开发的方式,基本上保持每周或者每两周一次的发布频率,系统升级总是伴随着各种风险,新旧版本兼容的风险,用户使用习惯突然改变而造成用户流失的风险,系统宕机的风险,500错误服务不可用的风险等等。为了避免这些风险,很多产品都采用了灰度发布的策略,其主要思想就是把影响集中到一个点,然后再发散到一个面,出现意外情况后很容易就回退,即使影响也是可控的。
灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式。AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B上面来。灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度。
创建 app1 app2项目
xxxxxxxxxx
[root@localhost nginx]# pwd
/usr/local/openresty/nginx
[root@localhost nginx]# cd html/
[root@localhost html]# mkdir -p test1 test2
[root@localhost html]# cd test1
[root@localhost test1]# vim index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to OpenResty!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to OpenResty!</h1>
<p>If you see this page, the OpenResty web platform is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<h2>===========app1================</h2>
<p><em>Thank you for flying OpenResty.</em></p>
</body>
</html>
[root@localhost html]# cd test2
[root@localhost test1]# vim index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to OpenResty!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to OpenResty!</h1>
<p>If you see this page, the OpenResty web platform is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<h2>===========app2======新版项目==========</h2>
<p><em>Thank you for flying OpenResty.</em></p>
</body>
</html>
赋值权限
[root@localhost nginx]# chmod -R 777 html
创建app1.conf app2.conf
xxxxxxxxxx
#编辑test1.conf
[root@localhost vhosts]# vim test1.conf
server {
listen 9091;
server_name *.*;
location / {
root root /opt/application/html/app1;
index index.html index.htm;
}
}
#编辑test2.conf
[root@localhost vhosts]# vim app2.conf
server {
listen 9092;
server_name *.*;
location / {
root /opt/application/html/app2;
index index.html index.htm;
}
}
编写lua脚本
xxxxxxxxxx
[root@localhost test]# vim test.lua
ngx.header.content_type="text/plian"
--获取请求参数
--参考地址:https://github.com/openresty/lua-nginx-module#lua_need_request_body
--demo http://127.0.0.1?ver=1.0 http://127.0.0.1?ver=2.0
ngx.req.read_body()
--获取请求数据
local post_args_tab,err=ngx.req.get_post_args()
--ngx.say(type(post_args_tab))
if not post_args_tab then
ngx.say('请拼接提交参数')
return
end
--获取请求参数
local request_data={}
for k,v in pairs(post_args_tab) do
request_data[k]=v
end
--判断版本控制参数是否存在
if not request_data['ver'] then
ngx.say('ver not is null')
return
end
--[[
--循环查看数据
for key,item in pairs(request_data) do
ngx.say('key',key)
ngx.say('item',item)
end
ngx.say(request_data['ver'])
ngx.exit(200)
--]]
--根据版本号判断跳转,初始化灰度发布的版本号
local ver=math.floor(2.0)
--版本是2.0跳转 test2 [代表是新版代码灰度发布]
--使用的知识点是ngx.exec内部重定向
--地址:https://github.com/openresty/lua-nginx-module#ngxexec
if math.floor(request_data['ver']) == ver then
--ngx.say(2.0)
ngx.exec("@test2")
return
end
--其它版本跳转test
--ngx.say(1.0)
ngx.exec("@test1")
关闭nginx代码缓存,修改lua脚本不需要重启
xxxxxxxxxx
lua_code_cache off;
编辑nginx.conf,引入lua脚本,使用nginx_proxy upstream
xxxxxxxxxx
[root@localhost nginx]# vim conf/nginx.conf
#test1(默认项目代码)
upstream test1 {
server 192.168.11.200:9091;
}
#test2(新版代码)
upstream test2 {
server 192.168.11.200:9092;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
#引入lua脚本
content_by_lua_file /usr/local/openresty/lualib/myself_project/test/test.lua;
}
#默认环境
location @test1 {
proxy_pass http://test1;
}
#新版代码(灰度发布)
location @test2 {
proxy_pass http://test2;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#引入vhosts
include vhosts/*.conf;