环境:

192.168.2.30 openrestry

192.168.2.31 redis

192.168.2.32 tomcat1 生产环境

jsp实现lua一文看懂灰度宣布基于NginxLuaRedis SQL

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 -vnginx version: openresty/1.15.8.3

[root@aly openssl-1.0.2n]# /fxkj/openresty/nginx/sbin/nginx -Vnginx version: openresty/1.15.8.3built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)built with OpenSSL 1.0.2n 7 Dec 2017TLS SNI support enabledconfigure 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 {#添加;;标识默认路径下的lualiblua_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 thenngx.say("failed to connect redis ",err);return;end--获取要求iplocal local_ip = ngx.req.get_headers()["X-Real-IP"];if local_ip == nil thenlocal_ip = ngx.req.get_headers()["x_forwarded_for"];endif local_ip == nil thenlocal_ip = ngx.var.remote_addr;endlocal_ip=ngx.var.remote_addr;--redis中获取白名单local ip_lists=red:get("gray");--判断是否在白名单然后转到对应做事if string.find(ip_lists,local_ip) == nil thenngx.exec("@prod1");elsengx.exec("@prod2-grey");endlocal ok,err=red:close();

1.7 检测语法是否精确

[root@aly openresty]# /fxkj/openresty/nginx/sbin/nginx -tnginx: the configuration file /fxkj/openresty/nginx/conf/nginx.conf syntax is oknginx: 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 6379192.168.2.31:6379> set gray 192.168.2.165OK192.168.2.31:6379> get gray"192.168.2.165"

用客户端 : 192.168.2.165 去访问

再次打开浏览器: http://192.168.2.30

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

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