OpenResty 实现项目的灰度发布

1、安装 openresty 依赖模块:

[[email protected] opt]# yum -y install pcre-devel openssl openssl-devel postgresql-devel

2、编译安装 openresty:

[[email protected] opt]# tar -zxvf openresty-1.15.8.2.tar.gz
...(略去内容)...
[[email protected] opt]# cd openresty-1.15.8.2/
[[email protected] openresty-1.15.8.2]# ll
总用量 112
drwxrwxr-x. 47 hacker 1003  4096 11月 12 23:35 build
drwxrwxr-x. 46 hacker 1003  4096 8月  29 13:33 bundle
-rwxrwxr-x.  1 hacker 1003 52432 8月  29 13:32 configure
-rw-rw-r--.  1 hacker 1003 22924 8月  29 13:32 COPYRIGHT
-rw-r--r--.  1 root   root  5980 11月 12 23:36 Makefile
drwxrwxr-x.  2 hacker 1003   203 8月  29 13:32 patches
-rw-rw-r--.  1 hacker 1003  4689 8月  29 13:32 README.markdown
-rw-rw-r--.  1 hacker 1003  8972 8月  29 13:32 README-windows.txt
drwxrwxr-x.  2 hacker 1003    52 8月  29 13:32 util
[[email protected] openresty-1.15.8.2]# ./configure --prefix=/usr/local/openresty --with-luajit --without-http_redis2_module --with-http_iconv_module --with-http_postgres_module
...(略去内容)...
[[email protected] openresty-1.15.8.2]# gmake && gmake install
...(略去内容)...

3、编辑 nginx.conf 文件,编辑后内容为:

http {
    include       mime.types;
    default_type  application/octet-stream;
    
    sendfile        on;
    keepalive_timeout  65;
    
    lua_shared_dict ups_zone 1m; # 定义upstream共享内存空间

    upstream web-cluster {
        server 127.0.0.1:8087;
        server 127.0.0.1:8088;
    }

    upstream web-8087 {
        server 127.0.0.1:8087;
    }

    upstream web-8088 {
        server 127.0.0.1:8088;
    }


    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /forward {
            default_type text/html;
            content_by_lua_block {
               local host = ngx.req.get_uri_args()["host"]
               local key = "switchKey"
               ngx.shared.ups_zone:set(key, host)
               local forward_ip = ngx.shared.ups_zone:get(key)
               ngx.say("Successfully, forwarded host is: ", forward_ip)
            }
        }
        
        location ~ /web/(.*) {
           set_by_lua_block $my_ups {
               local key = ngx.shared.ups_zone:get("switchKey")
               if key == nil or key == "default" then
                  return "web-cluster"
               else
                  local port = string.sub(key, -4)
                  if port == "8087" then
                     return "web-8087"
                  elseif port == "8088" then
                     return "web-8088"
                  end
               end

           }

           proxy_pass http://$my_ups/$1;

        }

    }
}

4、测试是否生效:

(1)默认负载均衡模式:

[[email protected] conf]# curl http://127.0.0.1/forward?host=default
Successfully, forwarded host is: default
[[email protected] conf]# curl http://127.0.0.1/web/lua
8088 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8087 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8088 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8087 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8088 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8087 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8088 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8087 server
[[email protected] conf]# 

(2)将所有请求转移到 8087 server:

[[email protected] conf]# curl http://127.0.0.1/forward?host=127.0.0.1:8087
Successfully, forwarded host is: 127.0.0.1:8087
[[email protected] conf]# curl http://127.0.0.1/web/lua
8087 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8087 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8087 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8087 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8087 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8087 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8087 server
[[email protected] conf]# 

(3)将所有请求转移到 8088 server:

[[email protected] conf]# curl http://127.0.0.1/forward?host=127.0.0.1:8088
Successfully, forwarded host is: 127.0.0.1:8088
[[email protected] conf]# curl http://127.0.0.1/web/lua
8088 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8088 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8088 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8088 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8088 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8088 server
[[email protected] conf]# curl http://127.0.0.1/web/lua
8088 server
[[email protected] conf]# 

参考书籍:《OpenResty最佳实践》PDF版,《OpenResty完全开发指南》

猜你喜欢

转载自www.cnblogs.com/d0usr/p/11869962.html