Openresty+Lua 实现项目灰度发布

Posted Json2011315

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Openresty+Lua 实现项目灰度发布相关的知识,希望对你有一定的参考价值。

1.安装OpenResty

【使用yum安装】

[root@localhost ~]#  yum-config-manager --add-repo https://openresty.org/yum/cn/centos/OpenResty.repo
 [root@localhost ~]# yum -y install openresty

常用命令:

【平滑启动】
[root@localhost ~]# /usr/local/openresty/nginx/sbin/nginx -s reload
【启动】
[root@localhost ~]# /usr/local/openresty/nginx/sbin/nginx 
【关闭】
[root@localhost ~]# /usr/local/openresty/nginx/sbin/nginx -s stop

2.关闭nginx代码缓存,修改lua脚本不需要重启

[root@localhost nginx]# vim conf/nginx.conf

http 
    include       mime.types;
    default_type  application/octet-stream;
    #关闭代码缓存 。修改lua脚本不需要重启
    lua_code_cache off;


修改后需要重启(会提示cache已经关闭):

[root@localhost nginx]# /usr/local/openresty/nginx/sbin/nginx -s reload
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/nginx.conf:21

2.编写lua脚本

进入openresty的lualib库
[root@localhost ~]# cd /usr/local/openresty/lualib/
创建自己的库目录
[root@localhost lualib]# mkdir -p myself_project/test
[root@localhost myself_project]# pwd
/usr/local/openresty/lualib/myself_project
[root@localhost myself_project]# ls
test
[root@localhost myself_project]# cd test/

#编写lua脚本

[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")

3.创建 test1 test2项目

[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>===========test1================</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>===========test2======新版项目==========</h2>
	<p><em>Thank you for flying OpenResty.</em></p>
	</body>
	</html>

赋值权限
[root@localhost nginx]# chmod -R 777 html

4.编辑nginx.conf,引入lua脚本,使用nginx_proxy upstream

[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;

5.创建vhosts目录,test1.conf test2.conf
[root@localhost conf]# pwd
/usr/local/openresty/nginx/conf
[root@localhost conf]# mkdir vhosts
[root@localhost conf]# cd vhosts/
[root@localhost vhosts]# pwd
/usr/local/openresty/nginx/conf/vhosts

#编辑test1.conf
[root@localhost vhosts]# vim test1.conf 
server 
        listen       9091;
        server_name  www.test1.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / 
                root   /usr/local/openresty/nginx/html/test1;
                index  index.html index.htm;
                #try_files $uri $uri/ /index.php$is_args$args;
        

        #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;
        

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \\.php$ 
        #    proxy_pass   http://127.0.0.1;
        #

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \\.php$ 
        #       root           /usr/local/openresty/nginx/html/test1;
        #       fastcgi_pass   127.0.0.1:9000;
        #       fastcgi_index  index.php;
        #       fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #       include        fastcgi_params;
        #

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\\.ht 
        #    deny  all;
        #


#编辑test2.conf
[root@localhost vhosts]# vim test2.conf 
server 
        listen       9092;
        server_name  www.test2.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / 
                root   /usr/local/openresty/nginx/html/test2;
                index  index.html index.htm;
                #try_files $uri $uri/ /index.php$is_args$args;
        

        #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;
        

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \\.php$ 
        #    proxy_pass   http://127.0.0.1;
        #

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \\.php$ 
        #       root           /usr/local/openresty/nginx/html/test2;
        #       fastcgi_pass   127.0.0.1:9000;
        #       fastcgi_index  index.php;
        #       fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #       include        fastcgi_params;
        #

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\\.ht 
        #    deny  all;
        #

6.重启Openresty

[root@localhost ~]# /usr/local/openresty/nginx/sbin/nginx -s reload
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/nginx.conf:21

7.postman访问

访问灰度环境代码

访问灰度正式代码

以上是关于Openresty+Lua 实现项目灰度发布的主要内容,如果未能解决你的问题,请参考以下文章

Openresty+Lua+Redis灰度发布

Openresty+Lua+Redis灰度发布

Openresty+Lua+Redis灰度发布

Nginx + Lua + Cookie 控制灰度发布

Openresty+redis实现灰度发布

使用Lua+Redis+OpenResty实现电商首页并发优化