openresty灰度发布

灰度发布

也叫金丝雀发布

为什么叫金丝雀发布呢,是因为金丝雀对矿场中的毒气比较敏感,所以在矿场开工前工人们会放一只金丝雀进去,以验证矿场是否存在毒气,这便是金丝雀发布名称的由来。

灰度发布利用openresty+redis实现

openresty和redis安装忽略

部署

1、配置nginx文件

nginx配置文件如下

user root;
worker_processes 1;
error_log logs/error.log;

events {
    worker_connections 1024;
}

http {
  #添加;;标识默认路径下的lualib
  lua_package_path "$prefix/lualib/?.lua;;";
  lua_package_cpath "$prefix/lualib/?.so;;";
#prod1为灰度页面
  upstream prod1 {
    server 172.16.10.201:5001;
  }
#prod2为正式页面
  upstream prod2 {
    server 172.16.10.201:5000;
  }

  server {
    listen 80;
    server_name localhost;
    location / {
      #为每一个请求执行gray.lua脚本
      content_by_lua_file lua/gray.lua;
    }
    location @prod1 {
      proxy_pass http://prod1;
    }
    location @prod2 {
      proxy_pass http://prod2;
    }
    
  }

}

2、添加lua脚本

在nginx文件夹下新建目录lua。添加lua脚本gray.lua

local redis = require "resty.redis" -- 引入 resty.redis 模块
local cache = redis.new() -- 创建一个新的 redis 实例
cache:set_timeout(60000) -- 设置超时时间为 60000 毫秒

local ok, err = cache.connect(cache, '127.0.0.1', 6379)  -- 连接到本地的 Redis 服务器
if not ok then 
  ngx.say("failed to connect:", err) -- 如果连接失败,打印错误信息并返回
  return 
end 

local red, err = cache:auth("fm9HqV25") -- 进行身份验证

if not red then
  ngx.say("failed to authenticate: ", err) -- 如果身份验证失败,打印错误信息并返回
  return
end

local local_ip = ngx.req.get_headers()["X-Real-IP"] -- 获取客户端真实 IP 地址
if local_ip == nil then
  local_ip = ngx.req.get_headers()["x_forwarded_for"] -- 如果 X-Real-IP 不存在,尝试获取 x_forwarded_for 的 IP 地址
end

if local_ip == nil then
  local_ip = ngx.var.remote_addr -- 如果 x_forwarded_for 也不存在,获取远程地址
end


local intercept = cache:get(local_ip)  -- 从缓存中获取 IP 地址

if intercept == local_ip then
  ngx.exec("@prod1") -- 如果缓存中包含该 IP 地址,重定向到 "@prod1"
  return 
end

ngx.exec("@prod2") -- 如果缓存中不包含该 IP 地址,重定向到 "@prod2"

local ok, err = cache:close()  -- 关闭与 redis 的连接

if not ok then 
  ngx.say("failed to close:", err) -- 如果关闭连接失败,打印错误信息并返回
  return 
end

3、添加redis的key

/homee/jht/projects/redis/bin/redis/cli
>auth fm9HqV25
>set 172.16.10.218 172.16.10.218

访问

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值