Nginx与Lua灰度发布的实现
2020-03-31 12:05
906 查看
安装 memcached
yum install -y memcached #启动memcached服务,端口11211,以nobody身份启动,-d后台模式运行 memcached -p11211 -u nobody -d #将自己的IP设为灰度测试的IP telnet 127.0.0.1 11211 set 119.32.216.122 0 0 1 1 quit
安装 memcached for lua
wget https://github.com/openresty/lua-resty-memcached/archive/v0.11.tar.gz tar -zxvf v0.11.tar.gz mkdir -p /usr/local/share/lua/5.1 cp -r lua-resty-memcached-0.11/lib/resty /usr/local/share/lua/5.1
创建 test.lua 文件
clientIP=ngx.req.get_headers()["X-Real-IP"] if clientIP == nil then clientIP=ngx.req.get_headers()["x_forwarded_for"] end if clientIP == nil then clientIP=ngx.var.remote_addr end local memcached=require "resty.memcached" local memc,err=memcached:new() if not memc then ngx.say("failed to instantiate memc:",err) return end local ok,err =memc:connect("127.0.0.1",11211) if not ok then ngx.say("failed to connect:",err) return end local res,flags,err =memc:get(clientIP) --ngx.say("value key: ",res,clientIP) if err then ngx.say("failed to get clientIP",err) return end if res == "1" then ngx.exec("@server_test") return end ngx.exec("@server_product")
nginx配置
server { listen 8867; server_name www.siguoya.name; root /usr/local/nginx/1.12.1/html; location / { content_by_lua_file /etc/nginx/test.lua; } location /hello { default_type text/plain; content_by_lua 'ngx.say("hello lua")'; } location /ua { default_type text/plain; content_by_lua ' clientIP=ngx.req.get_headers()["USER-AGENT"] ngx.say("IP:",clientIP) '; } location @server_product { default_type text/plain; return 200 'product'; } location @server_test { default_type text/plain; return 200 'test'; } }
由于我们将 119.32.216.122 这个IP添加进了memcached中,因此访问 http://www.siguoya.name:8867/ 的时候,看到的内容就是 test
而开启全局代理之后,发现看到的内容就是 product,这样就实现了针对部分用户进行灰度测试的效果
到此这篇关于Nginx与Lua灰度发布的实现的文章就介绍到这了,更多相关Nginx与Lua灰度发布内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
相关文章推荐
- 使用nginx+lua实现web项目的灰度发布
- 基于cookie在nginx实现业务灰度发布 推荐
- 使用 nginx 实现 灰度发布
- 灰度发布-基于nginx的cookie实现
- Istio 太复杂?KubeSphere基于Ingress-Nginx实现灰度发布
- nginx根据cookie实现重定向式灰度(AB)发布需求
- Istio 太复杂?KubeSphere基于Ingress-Nginx实现灰度发布
- 借住Nginx实现负载均衡和灰度发布的功能
- Istio 太复杂?KubeSphere基于Ingress-Nginx实现灰度发布
- 使用nginx实现的灰度发布思路研究(待实践)
- nginx-ingress controller简单实现基于http header、cookie、arg的灰度发布
- 使用Nginx+Lua+Redis构建灰度发布环境
- Nginx配之负载均衡、缓存、黑名单和灰度发布
- nginx+lua+kafka实现日志统一收集汇总
- 利用nginx+lua+memcache实现灰度发布
- Nginx + Lua + GraphicsMagick 实现切图服务
- OpenResty(Nginx)+Lua+GraphicsMagick实现缩略图功能
- NGINX + LUA实现复杂的控制
- nginx+lua+redis构建灰度服务
- Nginx嵌入Lua语言实现redis的高性能http接口