首页 > 其他 > 详细

openresty-lua-redis 实现简单的灰度发布

时间:2020-04-22 01:08:40      阅读:128      评论:0      收藏:0      [点我收藏+]

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时,就进灰度,其他的进生产环境。

[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)
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   

             https://www.jb51.net/article/114804.htm

openresty-lua-redis 实现简单的灰度发布

原文:https://www.cnblogs.com/hixiaowei/p/12749023.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!