一、架构

环境:

192.168.189.131:tomcat服务

192.168.189.132:tomcat服务

192.168.189.130:OpenResty服务、redis服务

流程:

请求到达openresty,openresty从redis获取白名单,然后判断请求地址是否再白名单,在白名单转到192.168.189.132服务否则转到192.168.189.131服务

在redis中动态设置白名单,实现服务切换

二、配置(openresty、redis、tomcat安装忽略)

1、在openresty根目录创建目录gray(作为工作空间),在gray目录创建conf(存放nginx配置文件nginx.conf)、logs(存放日志文件)、lua(存放lua脚本)

2、配置nginx.conf

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;;"; upstream prod1 {
server 192.168.189.131:8080;
} upstream prod2 {
server 192.168.189.132:8080;
} 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;
} } }

3、配置gray.lua

local redis=require "resty.redis";

local red=redis:new();

red:set_timeout(1000);
--redis连接
local ok,err=red:connect("192.168.189.130", 6379); if not ok then
ngx.say("failed to connect redis ",err);
return;
end
--获取请求ip
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_ip=ngx.var.remote_addr;

--redis中获取白名单
local ip_lists=red:get("gray");

--判断是否在白名单然后转到对应服务
if string.find(ip_lists,local_ip) == nil then
ngx.exec("@prod1");
else
ngx.exec("@prod2");
end local ok,err=red:close();

注意:

redis配置注释掉bind 127.0.0.1、设置protected-mode 为no;否则通过lua连接redis出错

#bind 127.0.0.1

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
# "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode no

4、启动openresty

在openresty/nginx/sbin执行:./nginx -p /root/data/program/openresty/gray  (-p表示指定空间)

5、演示效果:

访问192.168.189.131服务:

访问192.168.189.132服务:

redis中白名单gray:

请求地址192.168.189.130不在白名单,因此lua脚本执行@prod1,对应server 192.168.189.131:8080

redis设置白名单gray:

请求地址192.168.189.130在白名单,lua脚本执行@prod2,对应server 192.168.189.132:8080

Openresty+redis实现灰度发布的更多相关文章

  1. nginx+lua+redis实现灰度发布_test

    nginx+lua+redis实现灰度发布: 灰度发布是指在黑白之间能够平滑过渡的一种方式 AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见, ...

  2. Openresty+Lua+Redis灰度发布

    灰度发布,简单来说,就是根据各种条件,让一部分用户使用旧版本,另一部分用户使用新版本.百度百科中解释:灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式.AB test就是一种灰度发布方式,让一部分 ...

  3. 01 . OpenResty简介部署,优缺点,压测,适用场景及用Lua实现服务灰度发布

    简介 OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极高的动态 ...

  4. Nginx配之负载均衡、缓存、黑名单和灰度发布

    一.Nginx安装(基于CentOS 6.5) 1.yum命令安装 yum install nginx –y(若不能安装,执行命令yum install epel-release) 2. 启动.停止和 ...

  5. Nginx配置之负载均衡、限流、缓存、黑名单和灰度发布

    一.Nginx安装(基于CentOS 6.5) 1.yum命令安装 yum install nginx –y(若不能安装,执行命令yum install epel-release) 2. 启动.停止和 ...

  6. 【Nginx】实现负载均衡、限流、缓存、黑白名单和灰度发布,这是最全的一篇了!

    写在前面 在<[高并发]面试官问我如何使用Nginx实现限流,我如此回答轻松拿到了Offer!>一文中,我们主要介绍了如何使用Nginx进行限流,以避免系统被大流量压垮.除此之外,Ngin ...

  7. 使用Nginx实现灰度发布

    灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式.AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B ...

  8. springcloud灰度发布实现方案

    Nepxion Discovery是一款对Spring Cloud Discovery服务注册发现.Ribbon负载均衡.Feign和RestTemplate调用.Hystrix或者阿里巴巴Senti ...

  9. 使用Nginx实现灰度发布(转)

    灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式.AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B ...

随机推荐

  1. Git学习笔记 第二章

    文件相关操作 修改readme.txt文件,执行 git status 命令查看当前仓库状态 git status 位于分支 master 尚未暂存以备提交的变更: (使用 "git add ...

  2. 第一个Sprint

    项目名字:四则运算APP 开发环境:java 团队名称:会飞的小鸟 团队成员:陈志棚  李炫宗   刘芮熔  徐侃  罗伟业 一.经过宿舍世纪讨论后我们剔除了一些不合理的设计,比如网站管理员这一部分在 ...

  3. [2017BUAA软工]个人项目:数独

    一.项目地址 https://github.com/Slontia/Sudoku 附加作业(GUI):https://github.com/Slontia/SudokuGUI 二.开发时间 PSP2. ...

  4. RBAC权限管理及使用原生PHP实现

    关于RBAC的原理讲解在网上可以找到很多,推荐:编程浪子的RBAC讲解,本篇博客就不再累述RBAC的原理到底是什么样的. 传统的权限控制有ACL和RBAC方式,ACL的耦合度很高,扩展性不佳,RBAC ...

  5. PAT 1021 个位数统计

    https://pintia.cn/problem-sets/994805260223102976/problems/994805300404535296 给定一个k位整数N = d~k-1~*10^ ...

  6. ubuntu解压zip文件

    step1 # 安装解压软件 sudo apt-get install unzip step # 2 解压文件 unzip xxxxx.zip

  7. springMVC 使用WebApplicationContext获取ApplicationContext对象

    主要用于从application中获取bean 1.applicationContext 在web.xml中使用listener配置 <context-param> <param-n ...

  8. jquery 加載

    load(url,data,callback)從遠程服務器加載數據,并放入到被選中元素上: url是必須有的,希望加載的url: data是可選的,表示和請求一起發送的鍵值對數據: callback是 ...

  9. solr string类型表示不支持分词

    solr string类型表示不支持分词

  10. BZOJ3772精神污染——可持久化线段树+出栈入栈序

    题目描述 兵库县位于日本列岛的中央位置,北临日本海,南面濑户内海直通太平洋,中央部位是森林和山地,与拥有关西机场的大阪府比邻而居,是关西地区面积最大的县,是集经济和文化于一体的一大地区,是日本西部门户 ...