openresty-lua-redis 实现简单的灰度发布 lua-openresty的最佳案例 - 如何利用 nginx 实现生产和灰度环境流量切换?
1.灰度发布拓扑图,公司本地访问服务进灰度环境,其他的访问原来生产。
2.nginx.conf的配置
[root@VM_0_7_centos conf]# cat nginx.conf worker_processes 1; error_log logs/error.log; events { worker_connections 1024; } http { include vhost/*.conf; server { listen 22222; server_name 10.0.0.7 www.a.com localhost; #charset koi8-r; #access_log logs/host.access.log main; default_type 'text/plain'; location /test { content_by_lua_file /Users/chenguowei/local/openresty/nginx/lua_conf/huidu.lua; } location @client1 { proxy_pass http://client1; } location @client2 { proxy_pass http://client2; } } }
3.nginx的lua脚本
当来源的IP是myip时,就进灰度,其他的进生产环境。如果有密码加一句 在red:auth("密码")
即可。 参考:https://blog.csdn.net/weixin_42085428/article/details/104898500
[root@VM_0_7_centos ~]# cat /Users/chenguowei/local/openresty/nginx/lua_conf/huidu.lua local redis = require "resty.redis" local cache = redis.new() cache:set_timeout(60000) local ok, err = cache.connect(cache, "10.0.0.205", 6379)
cache:auth("密码") if not ok then ngx.say("failed to connect: redis", err) return end local local_ip = ngx.req.get_headers()["X-Real-IP"] if local_ip == nil then local_ip = ngx.req.get_headers()["x_forwarded_for"] end if local_ip == nil then local_ip = ngx.var.remote_addr end local intercept = cache:get("myip") if intercept == local_ip then ngx.exec("@client2") return end ngx.exec("@client1") --之前不能有任何的ngx.say()函数执行过,否则请求会出错 local ok, err = cache:close() if not ok then ngx.say("failed to close: ", err) return end
4.redis设置我的IP
[root@VM_0_42_centos src]# ./redis-cli -h 10.0.0.205 -p 6379 10.0.0.205:6379> set myip 10.0.0.7 OK 10.0.0.205:6379>
参考: https://blog.csdn.net/gochenguowei/article/details/85041578
用一个例子来演示会更加清晰
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
2019-04-21 nginx 配置文件的结构 + 502是nginx返回的错误