1.拓扑图
2.原理:
浏览器发送的报文头部带TOKEN,在openresty会被拼凑成:TOKEN_MyToken。
在redis中每个用户的TOKEN_MyToken对应一个Id.
lua脚本拿Id跟redis的IdStart和IdStop对比,判断是否进灰度
3.nginx.conf的配置
[root@VM_0_7_centos conf]# cat nginx.conf worker_processes 1; error_log logs/error.log; events { worker_connections 1024; } [root@VM_0_7_centos conf]# cat nginx.conf|egrep -v "^$" worker_processes 1; error_log logs/error.log; events { worker_connections 1024; } # 通过 redis 获取灰度用户区间,执行灰度操作 # 灰度测试完毕后,所有用户切换到灰度服务器,更新正式服务器 # 清空灰度服务器列表 http { # 灰度服务器 upstream grayscale.server{ server 10.0.0.7:44444; } # 正式服务器 upstream prod.server{ server 10.0.0.7:55555; } server { listen 22222; location /test { expires 5s; # 默认为生产环境 set $target ‘prod.server‘; default_type text/html; access_by_lua_file /Users/wangjie/data/nginx/lua/grayscale.lua; # proxy_pass $scheme://$target$request_uri; proxy_pass $scheme://$target$request_uri; } } }
4.lua脚本
[root@VM_0_7_centos conf]# cat /Users/wangjie/data/nginx/lua/grayscale.lua|egrep -v "^$" local cjson = require(‘cjson‘) local redis = require(‘resty.redis‘) local red = redis:new() red:connect("10.0.0.205",6379) red:auth("test") local IdStart = red:get("idStart") local IdEnd = red:get("idEnd") local UserToken = ngx.req.get_headers()["TOKEN"] local UserId = red:get("TOKEN_"..UserToken) -- ngx.say(UserId) if (UserToken ~= nil) then if ((UserId > IdStart) and (UserId < IdEnd)) then ngx.var.target = "grayscale.server" end else ngx.var.target = "prod.server" end
5.redis的设置 -- 通过调整idEnd的值来决定用户是否进灰度
6.浏览器的TOKEN
参考:https://blog.csdn.net/weixin_42085428/article/details/104898500
原文:https://www.cnblogs.com/hixiaowei/p/12783738.html